路径规划代码无向图下运行正常,MultiDiGraph输入报NoneType不可下标错误
提问:MultiDiGraph输入下OSMnx路径规划相关函数报NoneType下标错误
尊敬的Stack Overflow社区用户:
我开发了一款基于长度和高程的路径规划工具,使用两类成本因子计算时,仅输入无向图可正常得到结果,代码运行无异常,但输入MultiDiGraph时无法正常运行,而我需要使用MultiDiGraph适配高程相关的计算需求。
我遇到的报错如下:
报错1
~\anaconda3\envs\environmentwithhighestversion\lib\site-packages\osmnx\plot.py in plot_graph_route(G, route, route_color, route_linewidth, route_alpha, orig_dest_size, ax, **pg_kwargs) 292 293 # scatterplot origin and destination points (first/last nodes in route) --> 294 x = (G.nodes[route[0]]["x"], G.nodes[route[-1]]["x"]) 295 y = (G.nodes[route[0]]["y"], G.nodes[route[-1]]["y"]) 296 ax.scatter(x, y, s=orig_dest_size, c=route_color, alpha=route_alpha, edgecolor="none") TypeError: 'NoneType' object is not subscriptable
报错2
~\anaconda3\envs\environmentwithhighestversion\lib\site-packages\osmnx\utils_graph.py in get_route_edge_attributes(G, route, attribute, minimize_key, retrieve_default) 263 """ 264 attribute_values = [] --> 265 for u, v in zip(route[:-1], route[1:]): 266 # if there are parallel edges between two nodes, select the one with the lowest value of minimize_key TypeError: 'NoneType' object is not subscriptable
我的代码是在G. Boeing 2021年的开源代码基础上修改而来,可搜索OSMnx官方示例库的12-node-elevations-edge-grades.ipynb获取可复现示例。
触发第一个报错的代码:
TypeError Traceback (most recent call last) <ipython-input-3-86e03594e5bd> in <module> 7 route_by_impedance = ox.shortest_path(G7, source, target, weight="impedance") 8 print('Route Impedance:') ----> 9 fig, ax = ox.plot_graph_route(G7, route_by_impedance, node_size=3) 10 def print_route_stats(route): 11 route_grades = ox.utils_graph.get_route_edge_attributes(G7, route, "grade_abs")
触发第二个报错的代码:
ipython-input-4-4eb87f73e021> in print_route_stats(route) 9 #fig, ax = ox.plot_graph_route(G7, route_by_impedance, node_size=3) 10 def print_route_stats(route): ---> 11 route_grades = ox.utils_graph.get_route_edge_attributes(G7, route, "grade_abs") 12 msg = "The average grade is {:.1f}% and the max is {:.1f}%" 13 print(msg.format(np.mean(route_grades) * 100, np.max(route_grades) * 100))
请问有谁知道我遇到该问题的原因吗?
此致
Damiaan
解答
问题根因
两个报错的核心触发原因完全一致:调用ox.shortest_path()计算得到的route_by_impedance是None值,未找到符合要求的路径,后续调用绘图、提取边属性的函数时尝试对None进行下标访问,直接触发类型错误。
无向图可正常运行、MultiDiGraph报错的常见原因如下:
- 有向图路径连通性限制:无向图默认两点之间双向可达,但MultiDiGraph是有向多图,路径只能沿边的指定方向通行。如果高程成本计算后,两点之间不存在沿边方向的连通路径,或者所有路径的权重值为无穷大,
shortest_path就会返回None。 - 自定义权重字段异常:检查MultiDiGraph的所有边是否都正确赋值了
impedance字段,有没有边漏赋值导致权重为NaN/无穷大,或者字段名拼写错误。OSMnx会默认将无权重值的边权重设为无穷大,直接排除出可选路径。 - 起终点节点适配问题:确认
source和target两个节点确实存在于MultiDiGraph的节点列表中,有向图中如果节点不存在,或者节点只有入边没有出边(或者反过来)也会导致无路径。
排查解决步骤
- 先添加路径存在性判断,确认问题触发原因:
import networkx as nx route_by_impedance = ox.shortest_path(G7, source, target, weight="impedance") if route_by_impedance is None: print("未找到有效路径") # 检查两点在有向图中是否存在连通路径 print(nx.has_path(G7, source, target))
- 确认无连通路径后,先将
weight参数改为默认的length,测试是否能返回路径,排除权重字段配置错误的问题。 - 如使用
length可正常返回路径,逐边检查impedance字段的赋值逻辑,确认没有异常值。 - 若确实需要允许双向通行计算高程成本,可在计算路径时临时将图转为无向图,计算完成后再对应回有向图的边属性,或者在构造MultiDiGraph时为每一条原边添加反向边,同时赋值对应的高程成本。
内容的提问来源于stack exchange,提问作者Damiaan van Harteveld
相关产品推荐
相关产品推荐

