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

时间:2020-04-28 16:37:59

标签: networkx osmnx

在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)

1 个答案:

答案 0 :(得分:0)

此问题已通过创建有向图的牢固连接的组件而得以解决,以确保每个其他顶点均可访问每个顶点。

G=ox.geo_utils.get_largest_component(G,strongly=True)
相关问题