You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OSMNX未返回预期更短路径的技术问题咨询

OSMNX路径规划问题:预期的短路径未出现在k最短路径结果中

我使用OSMNX在图中两个节点间进行路径规划,但一条明显更短的路径未被纳入前30条最短路径结果。这条预期的最优路径位于图上半部分南北向长路的右侧,且视觉上节点是连通的。

相关代码如下:

start = (42.73843806388065, -84.51908658324757) 
distance_max = 3163.834987247283
G = ox.graph_from_point(start, network_type="drive", dist=distance_max, simplify=False)#, truncate_by_edge=True)
G = ox.utils_graph.get_undirected(G)

lng_orig = -84.51858078986406
lat_orig = 42.73524265064318
lng_dest = -84.51910484455361
lat_dest = 42.753847179060145

orig = ox.distance.nearest_nodes(G, X=lng_orig, Y=lat_orig)
dest = ox.distance.nearest_nodes(G, X=lng_dest, Y=lat_dest)

k_routes = ox.k_shortest_paths(G, orig, dest, 30, weight="distance")

fig, ax = ox.plot_graph_routes(G, list(k_routes), route_colors='r', route_linewidth=5, node_size=20)
plt.show()

可能的原因及解决步骤

  • 道路通行限制未被识别:network_type="drive"虽过滤了非机动道,但部分道路可能存在单向通行、禁止转弯等限制,视觉连通但实际不可通行。

    • 操作:检查目标路径边的属性,验证通行权限:
      # 筛选目标区域的边,查看通行属性
      for u, v, data in G.edges(data=True):
          if data['geometry'] and (data['geometry'].centroid.y > 42.75):
              print(f"边{u}-{v}: 单向={data.get('oneway')}, 通行权限={data.get('access')}")
      
  • 路网截断导致不连通:默认按节点距离截断路网,可能把预期路径的部分边/节点排除在外,看起来连通但不在当前图中。

    • 操作:启用truncate_by_edge=True参数,保留完整边:
      G = ox.graph_from_point(start, network_type="drive", dist=distance_max, simplify=False, truncate_by_edge=True)
      
  • 视觉距离判断误差:关闭simplify后路网节点冗余,可能预期路径的实际累加距离并不比当前结果更短。

    • 操作:计算当前最短路径的实际距离,对比视觉路径:
      shortest_route = ox.shortest_path(G, orig, dest, weight="distance")
      shortest_dist = sum(G[u][v][0]['distance'] for u, v in zip(shortest_route[:-1], shortest_route[1:]))
      print(f"当前最短路径距离: {shortest_dist}米")
      
    • 若确认视觉路径更短,尝试开启simplify=True简化路网后重新计算。
  • k值设置过小:Yen's算法生成k最短路径时,若目标路径与已生成路径共享大量边,需要更大的k值才能被纳入结果。

    • 操作:增大k值(如50或100)后重新生成路径:
      k_routes = ox.k_shortest_paths(G, orig, dest, 50, weight="distance")
      

内容的提问来源于stack exchange,提问作者Mike Dombrowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 19:01:51