使用OSMnx包时遇ValueError:图无边缘的路由代码问题
问题排查:OSMnx 报错 "ValueError: graph contains no edges"
问题背景
参考路由查找文章调整代码后,运行时触发ValueError: graph contains no edges错误,代码及错误信息如下:
运行代码
import osmnx as ox import networkx as nx ox.config(log_console=True, use_cache=True) # define the start and end locations in latlng start_latlng = (37.78497,-122.43327) end_latlng = (37.78071,-122.41445) # location where you want to find your route place = 'San Francisco, California, United States' # find shortest route based on the mode of travel mode = 'walk' # 'drive', 'bike', 'walk' # find shortest path based on distance or time optimizer = 'time' # 'length','time' # create graph from OSM within the boundaries of some # geocodable place(s) graph = ox.graph_from_place(place, network_type = mode) # find the nearest node to the start location orig_node = ox.distance.nearest_nodes(graph,37.78497,-122.43327 ) # find the nearest node to the end location dest_node = ox.distance.nearest_nodes(graph,37.78071,-122.41445) # find the shortest path shortest_route = nx.shortest_path(graph, orig_node, dest_node, weight=optimizer) shortest_route_map = ox.plot_route_folium(graph, shortest_route) shortest_route_map
错误信息
ValueError: graph contains no edges
错误原因及解决方案
1. 核心原因:路网数据未成功获取
graph_from_place依赖地理编码返回的区域边界,若边界匹配异常(如范围过大/过小、地理编码歧义),会导致无法获取有效路网;此外缓存机制可能加载旧的空数据。
2. 针对性解决步骤
禁用缓存,重新获取数据
将ox.config(use_cache=True)改为use_cache=False,或手动删除OSMnx缓存目录(默认路径~/.osmnx/cache),避免加载旧的空缓存。替换区域定义方式,使用包围盒确保覆盖目标点
放弃graph_from_place,改用graph_from_bbox基于起点终点的经纬度生成精准包围盒,确保路网覆盖目标区域:# 计算包围盒,添加0.01度缓冲确保覆盖起点终点 north = max(start_latlng[0], end_latlng[0]) + 0.01 south = min(start_latlng[0], end_latlng[0]) - 0.01 east = max(start_latlng[1], end_latlng[1]) + 0.01 west = min(start_latlng[1], end_latlng[1]) - 0.01 # 从包围盒获取路网 graph = ox.graph_from_bbox(north, south, east, west, network_type=mode)修正
nearest_nodes参数顺序
原代码中nearest_nodes的参数顺序错误,该函数要求经度在前,纬度在后,正确调用方式:orig_node = ox.distance.nearest_nodes(graph, start_latlng[1], start_latlng[0]) dest_node = ox.distance.nearest_nodes(graph, end_latlng[1], end_latlng[0])验证路网数据有效性
获取图后,添加以下代码检查节点和边数量,确认路网加载成功:print(f"节点数: {len(graph.nodes)}, 边数: {len(graph.edges)}")
3. 修正后的完整代码
import osmnx as ox import networkx as nx # 禁用缓存,避免加载旧的空数据 ox.config(log_console=True, use_cache=False) # 起点终点坐标(纬度, 经度) start_latlng = (37.78497, -122.43327) end_latlng = (37.78071, -122.41445) # 生成覆盖目标点的包围盒 north = max(start_latlng[0], end_latlng[0]) + 0.01 south = min(start_latlng[0], end_latlng[0]) - 0.01 east = max(start_latlng[1], end_latlng[1]) + 0.01 west = min(start_latlng[1], end_latlng[1]) - 0.01 mode = 'walk' optimizer = 'time' # 从包围盒获取步行路网 graph = ox.graph_from_bbox(north, south, east, west, network_type=mode) # 验证路网加载状态 print(f"节点数: {len(graph.nodes)}, 边数: {len(graph.edges)}") # 获取起点终点对应的最近路网节点(注意参数顺序:经度, 纬度) orig_node = ox.distance.nearest_nodes(graph, start_latlng[1], start_latlng[0]) dest_node = ox.distance.nearest_nodes(graph, end_latlng[1], end_latlng[0]) # 查找时间最优的最短路径 shortest_route = nx.shortest_path(graph, orig_node, dest_node, weight=optimizer) # 生成交互式路径地图 shortest_route_map = ox.plot_route_folium(graph, shortest_route) shortest_route_map
内容的提问来源于stack exchange,提问作者RandoM123
相关产品推荐
相关产品推荐

