Description
当使用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
My Questions:
- 这个问题的根本原因是什么?
- 我可以做些什么来防止这个问题?
- 如果在某些情况下这是无法预防的,那么建议我使用哪些备份/替代方案来保持我的代码在合理的路由/距离上运行?
Minimal reproducible example
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
找到了有效的源 node 和目的地 node ,但ox.shortest_path
失败了.
Expected behavior
当我将查询稍微更改为
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]