OSMnx利用networkx求有向图的最短路径



在OSMnx中,街道是定向的,以保持单向方向性,因此,当我试图使用Networkx找到最短路径时,我会得到NetworkXNoPath:没有通往(osmid(的路径。如何解决此问题?我需要在有单行道的网络中找到最短的路径。

参见以下代码:

import osmnx as ox
import networkx as nx
# define place
centreLat=40.771687
centreLon=-73.957233
# download osm data
G= ox.graph_from_point((centreLat,centreLon), distance=1000, network_type='drive',simplify = True)  
# plot the graph
fig,ax = ox.plot_graph(G)
# Get origin x and y coordinates
orig_xy = 40.7647425, -73.9683181
# Get target x and y coordinates
target_xy =40.7804348, -73.9498809
# Find the node in the graph that is closest to the origin point (here, we want to get the node id)
orig_node = ox.get_nearest_node(G, orig_xy, method='euclidean')

# Find the node in the graph that is closest to the target point (here, we want to get the node id)
target_node = ox.get_nearest_node(G, target_xy, method='euclidean')
# Get shortest path 
route = nx.shortest_path(G, source=orig_node, target=target_node, weight='length')
# Plot the shortest path
fig, ax = ox.plot_graph_route(G, route, origin_point=orig_xy, destination_point=target_xy)

这个问题是通过创建有向图的强连接组件来解决的,以确保每个顶点都可以从其他顶点访问。

G=ox.geo_utils.get_largest_component(G,strongly=True)

最新更新