OSMnx 使用 networkx 找到有向图的最短路径

OSMnx finding the shortest path of a directed graph using networkx

在 OSMnx 中,街道是定向的,以保持单向方向性,因此,当我尝试使用 Networkx 找到最短路径时,我得到 NetworkXNoPath: No path to (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)