- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个城市的简化 map ,其中有街道作为线串,地址作为点。我需要找到从每个点到任何街道线的最近路径。我有一个执行此操作的工作脚本,但它在多项式时间内运行,因为它嵌套了 for 循环。对于 150 000 条线(匀称的 LineString)和 10 000 个点(匀称的 Point),在 8 GB Ram 计算机上完成需要 10 个小时。
该函数看起来像这样(抱歉没有让它完全可重现):
import pandas as pd
import shapely
from shapely import Point, LineString
def connect_nodes_to_closest_edges(edges_df , nodes_df,
edges_geom,
nodes_geom):
"""Finds closest line to points and returns 2 dataframes:
edges_df
nodes_df
"""
for i in range(len(nodes_df)):
point = nodes_df.loc[i,nodes_geom]
shortest_distance = 100000
for j in range(len(edges_df)):
line = edges_df.loc[j,edges_geom]
if line.distance(point) < shortest_distance:
shortest_distance = line.distance(point)
closest_street_index = j
closest_line = line
...
最佳答案
在这里你有一个使用 rtree
的解决方案图书馆。这个想法是建立盒子
包含对角线上的线段,并使用这些框来构建
树。这将是最耗时的操作。
稍后,您使用以点为中心的框查询 rtree。你得到几个
您需要检查最小值的点击次数,但点击次数将为
(希望) magnitud 的顺序低于检查所有段。
在 solutions
dict 你会得到,对于每个点,线 id,最近的线段,
最近的点(线段的一个点),以及到该点的距离。
代码中有一些注释可以帮助您。
考虑到您可以序列化 rtree 以备后用。事实上,我会建议构建 rtree,保存它,然后使用它。因为常量调整的异常(exception)MIN_SIZE
和 INFTY
可能会提高,并且您不想丢失构建 rtree 所做的所有计算。
太小了MIN_SIZE
将意味着您可能在解决方案中有错误,因为如果点周围的框不与线段相交,它可能与不是最近线段的线段的框相交(很容易想到一个案例)。
A 太大 MIN_SIZE
将意味着有太多的误报,在极端情况下会使代码尝试使用所有段,并且您将处于与以前相同的位置,或者最糟糕的是,因为您现在正在构建一个您没有的 rtree真的用。
如果数据是来自一个城市的真实数据,我想你知道任何地址都会与一段距离小于几个街区的段相交。这将使搜索实际上是对数的。
再一评论。我假设没有太大的段。由于我们使用段作为 rtree 中框的对角线,如果一行中有一些大段,这将意味着一个巨大的框将分配给该段,并且所有地址框都将与它相交。为避免这种情况,您始终可以通过添加更多中间点来人为地增加 LineStrins 的分辨率。
import math
from rtree import index
from shapely.geometry import Polygon, LineString
INFTY = 1000000
MIN_SIZE = .8
# MIN_SIZE should be a vaule such that if you build a box centered in each
# point with edges of size 2*MIN_SIZE, you know a priori that at least one
# segment is intersected with the box. Otherwise, you could get an inexact
# solution, there is an exception checking this, though.
def distance(a, b):
return math.sqrt( (a[0]-b[0])**2 + (a[1]-b[1])**2 )
def get_distance(apoint, segment):
a = apoint
b, c = segment
# t = <a-b, c-b>/|c-b|**2
# because p(a) = t*(c-b)+b is the ortogonal projection of vector a
# over the rectline that includes the points b and c.
t = (a[0]-b[0])*(c[0]-b[0]) + (a[1]-b[1])*(c[1]-b[1])
t = t / ( (c[0]-b[0])**2 + (c[1]-b[1])**2 )
# Only if t 0 <= t <= 1 the projection is in the interior of
# segment b-c, and it is the point that minimize the distance
# (by pitagoras theorem).
if 0 < t < 1:
pcoords = (t*(c[0]-b[0])+b[0], t*(c[1]-b[1])+b[1])
dmin = distance(a, pcoords)
return pcoords, dmin
elif t <= 0:
return b, distance(a, b)
elif 1 <= t:
return c, distance(a, c)
def get_rtree(lines):
def generate_items():
sindx = 0
for lid, l in lines:
for i in xrange(len(l)-1):
a, b = l[i]
c, d = l[i+1]
segment = ((a,b), (c,d))
box = (min(a, c), min(b,d), max(a, c), max(b,d))
#box = left, bottom, right, top
yield (sindx, box, (lid, segment))
sindx += 1
return index.Index(generate_items())
def get_solution(idx, points):
result = {}
for p in points:
pbox = (p[0]-MIN_SIZE, p[1]-MIN_SIZE, p[0]+MIN_SIZE, p[1]+MIN_SIZE)
hits = idx.intersection(pbox, objects='raw')
d = INFTY
s = None
for h in hits:
nearest_p, new_d = get_distance(p, h[1])
if d >= new_d:
d = new_d
s = (h[0], h[1], nearest_p, new_d)
result[p] = s
print s
#some checking you could remove after you adjust the constants
if s == None:
raise Exception("It seems INFTY is not big enough.")
pboxpol = ( (pbox[0], pbox[1]), (pbox[2], pbox[1]),
(pbox[2], pbox[3]), (pbox[0], pbox[3]) )
if not Polygon(pboxpol).intersects(LineString(s[1])):
msg = "It seems MIN_SIZE is not big enough. "
msg += "You could get inexact solutions if remove this exception."
raise Exception(msg)
return result
xcoords = [i*10.0/float(1000) for i in xrange(1000)]
l1 = [(x, math.sin(x)) for x in xcoords]
l2 = [(x, math.cos(x)) for x in xcoords]
points = [(i*10.0/float(50), 0.8) for i in xrange(50)]
lines = [('l1', l1), ('l2', l2)]
idx = get_rtree(lines)
solutions = get_solution(idx, points)
关于python - 在大数据集上找到最接近每个点的线,可能使用 shapely 和 rtree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46170577/
这很可能是我的语法错误,因为我对在 C++ 中使用多个文件和结构(特别是将结构传递给函数)还很陌生。这是三个文件: 主要.cpp: #include #include #include #inc
我有 TypeScript NestJS 项目。 我需要验证传入的 DTO 到我的 API。它可以被描述为“创建项目”,其中我们有建筑类型(房屋、公寓、花园),并根据该类型我们需要定义: 房屋:楼层包
是否可以从可用于泛型参数的可能类型集中排除特定类型?如果是如何。 例如 Foo() : where T != bool 将意味着除了类型 bool 之外的任何类型。 编辑 为什么? 以下代码是我尝试强
我的 WebGL 体积光线转换应用程序即将完成。但是我发现了一个问题。我必须通过 2D 纹理模拟 3D 纹理。这不是问题。我正在用小切片创建一个巨大的纹理。巨大纹理的尺寸约为 4096x4096 像素
我正在处理的网页上显示了一个返回顶部按钮。当您向下滚动时,有时单击它时,它会跳到顶部,然后跳回您在页面上的位置,然后像预期的那样平滑滚动到顶部。请记住,它并不总是这样做。这只是一个滞后或故障问题还是我
我对此还很陌生,所以请耐心等待。 我有一个类,它具有三个属性:几个整数和一个用户定义对象的集合。 public class Response { public int num1 { get;
我正在制作一款平台游戏,让玩家每 30 毫秒跳跃一次,并向上添加少量的力。我想我应该使用多线程,因为我之前已经做过一些,而且看起来很简单。无论如何,我尝试了这个: public void jump()
是否可以从可能的类型集中排除特定类型,这些类型可以在泛型参数中使用?如果是这样的话。 例如 Foo() : where T != bool 表示除 bool 类型之外的任何类型。 编辑 为什么? 以下
我正在尝试在单个查询中实现内部和外部联接,我不确定我的做法是正确还是错误,因为我不太擅长查询。 就这样吧。 我有以下表格。 hrs_residentials hrs_residential_utili
关于 my website ,有一段代码可以向页面添加几个元素。这段代码不是我可以编辑的东西,而且我对它放置这些元素的位置不满意,因为它弄乱了我的一些布局。所以我想出了一个小的 jQuery 来将它们
一位客户希望我创建一个数据集,如下所示。我不知道这是否可能或合乎逻辑。 我有表parent: id name ------- ------- 1 parent1 2
这可能吗?google 好像没有这方面的资料.. 这样,如果用户在另一个网站上播放视频或歌曲,我的音量就会自动减小 最佳答案 不,这是不可能的。 如果可能的话,它必须是特定于浏览器的,但我不认为这种情
所以我正在尝试制作响应式页面。问题是为什么它归结为移动数据需要位于列表中。 我会用一些示例代码来解释 所以这可能是桌面上的输出 option1
当您将鼠标悬停在a 元素 上时,是否可以删除url? 这就是我的意思: 最佳答案 一种选择是使用一些 JavaScript。 删除 href=来自 的属性标签,取而代之的是 onclick=...
我已经考虑了几个小时,但我无法取得太大进展。它是这样的: You have an array of size n and q queries. Each query is of the form (l
我一直在尝试编写一个脚本来强化 android。我没有成功! 我正在通过模拟器运行一个 AVD,并且已经用我加载的 android shell 和 bash shell 试过了。正如您将在下面看到的那
Private Sub Workbook_Open() Dim WBname As String WBname = ThisWorkbook.name If Not InStr(WBname, "te
Spark 2.0.0-预览版 我们有一个应用程序使用了相当大的广播变量。我们在大型 EC2 实例上运行它,因此部署处于客户端模式。广播变量是一个巨大的 Map[String, Array[Strin
我正在尝试从此link中提取摘要。但是,我无法仅提取摘要的内容。到目前为止,这是我完成的工作: url <- "http://www.scielo.br/scielo.php?script=sci_a
我的主页中有一个iframe。 iframe页面中有一个modalpopup。因此,当显示modalpopup时,modalpopup的父级是iframe主体和主页父级主体。因此,覆盖层仅覆盖ifra
我是一名优秀的程序员,十分优秀!