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

使用OSMnX求解多点间最短路径时仅输出单个节点的问题咨询

OSMnX Plots Only Single Nodes Instead of Continuous Paths for Multi-Point Shortest Route Calculation

I'm trying to calculate the shortest paths between multiple geographic points using OSMnX, but when I run my code, only single nodes are plotted instead of the expected continuous routes. Can anyone help me figure out why this is happening?

Here's my code:

import osmnx as ox
place_name = "Hamedan"
graph = ox.graph_from_place(place_name, network_type='drive')
#graph_proj = ox.projection.project_graph(graph, to_crs='epsg:32639')
lats = [34.84008, 34.81894, 34.81746, 34.78828, 34.77877, 34.76029, 34.77038, 34.79837, 34.80043, 34.80108]
lngs = [48.54952, 48.54635, 48.51784, 48.50858, 48.51648, 48.53305, 48.50420, 48.51442, 48.50549, 48.48361]
point_list = [(lng, lat) for lng, lat in zip(lats, lngs)]
nearest_nodes = [ox.distance.nearest_nodes(graph, geographic_coordinates[0], geographic_coordinates[1], return_dist=False) for geographic_coordinates in point_list]
routes = [ox.distance.shortest_path(graph,nearest_nodes[i], nearest_nodes[i+1], weight='length', cpus=1) for i in range(len(nearest_nodes)-1)]
fig, ax = ox.plot.plot_graph_routes(graph,routes, ax=None, figsize=(8, 8), bgcolor="#111111", node_color="w", node_size=15, node_alpha=None, node_edgecolor="none", node_zorder=1, edge_color="#999999", edge_linewidth=1, edge_alpha=None, show=True, close=False, save=False, filepath=None, dpi=300, bbox=None)

Let's break down the most likely causes and how to fix them:

1. Check for Disconnected Nodes/Invalid Paths

The most common reason you're seeing only single nodes is that some of your route segments return None instead of a valid path. OSMnX's shortest_path() returns None when there's no connected driving route between the two nodes in the graph.

First, add debug prints to identify which segments are failing:

import osmnx as ox
place_name = "Hamedan"
graph = ox.graph_from_place(place_name, network_type='drive')

lats = [34.84008, 34.81894, 34.81746, 34.78828, 34.77877, 34.76029, 34.77038, 34.79837, 34.80043, 34.80108]
lngs = [48.54952, 48.54635, 48.51784, 48.50858, 48.51648, 48.53305, 48.50420, 48.51442, 48.50549, 48.48361]
point_list = [(lng, lat) for lng, lat in zip(lats, lngs)]

nearest_nodes = [ox.distance.nearest_nodes(graph, lng, lat) for lng, lat in point_list]
print("Nearest network nodes:", nearest_nodes)

routes = []
for i in range(len(nearest_nodes)-1):
    start = nearest_nodes[i]
    end = nearest_nodes[i+1]
    route = ox.distance.shortest_path(graph, start, end, weight='length')
    print(f"Route from node {start} to {end}: {route}")
    routes.append(route)

# Filter out None routes to avoid plotting issues
valid_routes = [r for r in routes if r is not None]
fig, ax = ox.plot.plot_graph_routes(graph, valid_routes, figsize=(8,8), bgcolor="#111111", node_color="w", node_size=15, edge_color="#999999")

2. Verify Network Coverage for Your Points

Some of your geographic points might lie outside the Hamedan driving network or in areas with no connected roads. To check this:

  • Plot the original graph first to see the network boundaries:
    ox.plot_graph(graph, figsize=(8,8), bgcolor="#111111", node_color="w", node_size=10)
    
  • If some points are outside the network, you can expand the graph's boundary using buffer_dist in graph_from_place():
    # Expand the graph by 5000 meters (5km) around Hamedan
    graph = ox.graph_from_place(place_name, network_type='drive', buffer_dist=5000)
    

3. Confirm Coordinate Order

You're already using the correct order (lng, lat) for nearest_nodes() (OSMnX expects longitude first, then latitude), so that's not the issue here—but it's always good to double-check!

4. Check for Isolated Nodes

Even if a point is within the network area, its nearest node might be isolated (no incoming/outgoing edges). You can clean the graph to remove isolated nodes before calculating paths:

# Remove isolated nodes from the graph
graph = ox.utils_graph.remove_isolated_nodes(graph)

After running the debug code, you'll see exactly which route segments are failing. From there, you can adjust your points, expand the network, or handle disconnected segments as needed.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 11:07:39