【问题标题】:Find nearest road and distance to it for a point为一个点找到最近的道路和距离
【发布时间】:2021-12-14 09:55:10
【问题描述】:

我有一个包含数千个坐标(纬度、经度)的列表。我想为每个点找到最近的道路和距离。我尝试使用 OSMNX:从 osmnx 加载所有道路并计算每个点到每条道路的距离(代码如下)。但是计算需要很长时间。

roads = gdf[["geometry", "u", "v","ref","name","highway","lanes"]].values.tolist()

# calculate and attach distance
roads_with_distances = [(road, Point(tuple(reversed((59.961517, 30.340880)))).distance(road[0])) for road in roads] #ox

# sort by distance
roads_with_distances = sorted(roads_with_distances, key=lambda x: x[1])
# Select closest road
closest_road = roads_with_distances[0]
# Check whether you are actually "on" the road
if round(closest_road[1],4) < 0.0001: print('Hit the road, Jack!')

我的数据示例(坐标列):

OSMNX 道路数据示例(road==Linestring):

有没有找到最近的道路和距离(以米为单位)的有效方法? 我正在寻找一种 Python 方法。

【问题讨论】:

  • 请添加一些随机/样本数据和一个最小工作示例。可能一棵树可以很好地完成这项工作scikit-learn.org/stable/modules/generated/… 它支持你想要的haversine
  • @WillemHendriks 我在帖子中添加了更多信息。上面的代码给了我最近的道路信息,例如名称和距离。但我什至不需要路名,只需要一段距离。
  • 您能否将测试数据添加为代码生成和/或样本?

标签: gis geospatial geo osmnx kdtree


【解决方案1】:

是的,您可以使用 OSMnx 直接(并且快速)执行此操作。您没有提供完整的可重现代码示例,但目前您似乎只是在运行一个大的 for 循环,每次迭代都会调用 shapely 几何的距离方法。这会很慢。

相反,请查看 OSMnx documentation 和用法 examples。 OSMnx 的nearest_edges 函数可以实现您的目标:最近的街道到它的距离(以米为单位)。

import osmnx as ox

# get a street network and randomly sample 10,000 points
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')
G_proj = ox.project_graph(G)
points = ox.utils_geo.sample_points(ox.get_undirected(G_proj), 10000)

%time ne1 = ox.nearest_edges(G_proj, X=points.x, Y=points.y, return_dist=True)
# wall time: 2.91 s

%time ne2 = ox.nearest_edges(G_proj, X=points.x, Y=points.y, interpolate=10, return_dist=True)
# wall time: 302 ms

第一种方法使用 r-tree 来找到精确的最近边。第二种方法使用 k-d 树来查找近似(通过 10 米点插值)最近的边缘。第二种方法比第一种方法快约 10 倍。请注意,您没有必须投影您的图形:如果它未投影,nearest_edges 函数将自动使用 BallTree 和 hasrsine 进行最近边求解。另见this answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 2021-11-05
    • 1970-01-01
    • 2014-07-21
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多