`osmnx.shortest_path` 对于有效的起始节点和目标节点返回 `None`

问题描述 投票:0回答:1

描述

用OSMnx计算两个位置之间的最短路径时,

ox.shortest_path()
未能获取任何路线并返回
None

origin_lat=42.482, origin_lon=-70.910, dest_lat=42.472, dest_lon=-70.957

我查询的点都比较正常,距离都不是特别远/特别近,而且之间有清晰的路网。

这是我使用OSM网页时的结果: Routing Results with OSM Webpage

我的问题:

  1. 这个问题的根本原因是什么?
  2. 我可以做什么来防止这个问题?
  3. 如果在某些情况下这是无法避免的,那么建议我使用哪些备份/替代方案来保持代码以合理的路线/距离运行?

最小可重现示例

from shapely.geometry import Polygon
import osmnx as ox

region_bounds = [
    [42.49897315546415, -70.97752844338558],
    [42.497310679689555, -70.89216747227316],
    [42.45989329011355, -70.90617955621047],
    [42.457041524105065, -70.97768950182164],
]
region_bounds.append(region_bounds[-1])

region_polygon = Polygon([bounds[::-1] for bounds in region_bounds])

mode = "drive"

G = ox.graph_from_polygon(polygon=region_polygon, network_type=mode)
G = ox.add_edge_speeds(G)
G = ox.add_edge_travel_times(G)

origin_lat = 42.482
origin_lon = -70.910

dest_lat = 42.472
dest_lon = -70.957

origin_nodes = ox.distance.nearest_nodes(G, origin_lon, origin_lat)
dest_nodes = ox.distance.nearest_nodes(G, dest_lon, dest_lat)

routes = ox.shortest_path(G, origin_nodes, dest_nodes)

print(origin_nodes, dest_nodes, routes)

输出为

68758830 65236189 None
,这意味着
ox.distance.nearest_nodes
找到了有效的起始节点和目标节点,但
ox.shortest_path
失败了。

预期行为

当我将查询稍微更改为

origin_lat = 42.452
origin_lon = -70.910

dest_lat = 42.472
dest_lon = -70.957

上面的代码可以找到有效的路线

68754328 65236189 [68754328, 68752028, 68757205, 68766524, 68769796, 68777219, 68761577, 68759405, 68766786, 68747897, 68755811, 68764727, 68765868, 68755029, 2041487395, 2041487385, 68758705, 68771074, 68751303, 68770735, 68747441, 65186124, 65232064, 65258971, 65258184, 65198797, 65243553, 2041154812, 65261211, 65218821, 65210373, 65208978, 65255290, 65231546, 65190866, 65226679, 65193542, 65239462, 65225225, 2041270157, 65257919, 65186045, 2041270160, 65262590, 2041270186, 65252676, 65232296, 65242158, 65261501, 65221801, 65251183, 65190759, 65218681, 65222417, 2043144587, 65250858, 2043144592, 65247406, 65224701, 65231219, 65202428, 65242218, 65235268, 65197313, 65240735, 65207550, 2045575158, 65227845, 65229809, 65190291, 65217006, 2045610191, 9966458026, 65195913, 65214016, 65241686, 65240704, 65202519, 65201239, 65242936, 65233288, 65186829, 65199167, 65239099, 65242030, 65237992, 65236189]
python geo osmnx
1个回答
0
投票

这个问题的根本原因是什么?

原因是这种OSM方式:https://www.openstreetmap.org/way/1243001416

它被数字化为进入该社区的单向街道。没有数字化的出站方式。因此,您可以解决入小区的路由,但无法解决出小区的路由,因为是单向的。

我可以做什么来防止这个问题?

据推测,这是一个不正确的数字化,因为没有其他进出该社区的路线。它不可能只是单向的,并且不可能从这个社区没有出口。如果是这样,防止此问题的最佳方法是对 OpenStreetMap 本身进行更正。

另一种方法是编辑 OSMnx 模型以添加互惠单向边以允许双向访问。

如果在某些情况下这是无法避免的,那么建议我使用哪些备份/替代方案来保持代码以合理的路线/距离运行?

这并非无法预防。如果模型表现出这样的不可能情况,上述解决方案之一将解决它。修复 OpenStreetMap 上的底层数字化错误,或在 OSMnx 模型中修复它(例如,添加边缘)。

© www.soinside.com 2019 - 2024. All rights reserved.