我有一个简化的城市地图,其中的街道为线串,地址为点。我需要找到从每个点到任何一条街线的最近路径。我有一个执行此操作的脚本,但由于嵌套了循环,因此它在多项式时间内运行。对于15万行(形状为LineString)和10000点(形状为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
...
Run Code Online (Sandbox Code Playgroud)
然后,将结果保存在表中作为新列,该列将点到线的最短路径添加为新列。
有没有一种方法可以使该功能更快些?
例如,如果我可以为50m左右的每个点过滤出线,这将有助于加快每次迭代的速度?
有没有一种方法可以使用rtree包使其更快?我能够找到一个答案,从而使脚本可以更快地找到多边形的交点,但是我似乎无法使它适用于最接近点到线的地方。
https://pypi.python.org/pypi/Rtree/
抱歉,如果已经回答了,但是我在这里也没有在gis.stackexchange上找到答案
谢谢你的建议!