0

In OSMnx the streets are directed in order to preserve one-way directionality and therefore, when I try to find the shortest path using Networkx I get NetworkXNoPath: No path to (osmid). How do I fix this issue? I need to find the shortest path in a network with one-way streets.

See code below:

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)
malik
  • 1
  • 3
  • Do you have any runnable code so we can see where the problem is? – MarcusRenshaw Apr 29 '20 at 07:20
  • Hi @MarcusRenshaw, please see above for runnable code. When you run this code you will notice a NetworkXNoPath: No path to 42443044. – malik Apr 29 '20 at 12:21
  • Have you evaluated, if a path exists in the undirected graph? To check that you need to convert the graph `G=nx.Graph(G)`. – Sparky05 Apr 29 '20 at 14:50
  • @Sparky05 if I change it to an undirected graph I will not retain the one-way directionality which is indeed what I want in the shortest path solution. And, yes the path exists but then the one-way directions are not retained. Is there any other way to find the path without losing the one-way directions? – malik Apr 30 '20 at 06:37
  • You could check whether the target node is such a node from where all the edges are outgoing and none are incoming. That could be a reason for no path. I checked your code with some other target node ids (part of one-way edges), and they seem to be working fine (eg. node id 42444820 and 42445236). – Debjit Bhowmick Apr 30 '20 at 08:21
  • For example, if the target node id is set to 42443029, which is again part of a one-way edge, the shortest path in a directed graph and undirected graph are different, 2223.86 metres and 2058.324 metres respectively. Note that the undirected graph shortest path length is lesser than the directed graph one, upholding the theory. – Debjit Bhowmick Apr 30 '20 at 08:25
  • @DebjitBhowmick, if you inspect the target node you'd see that there are indeed incoming/outgoing edges. What confuses me is that when I increase the distance to e.g. distance=2000 in line (G= ox.graph_from_point((centreLat,centreLon), distance=1000, network_type='drive',simplify = True) ) then a path is found. – malik Apr 30 '20 at 10:44
  • Hmm. This is confusing. I saw your github issue. Maybe you could raise this issue again as per your previous comment. – Debjit Bhowmick May 01 '20 at 01:39
  • @DebjitBhowmick I was able to solve the issue by creating a strongly connected component graph, see below. Thanks! – malik May 01 '20 at 11:07

1 Answers1

0

This issue was fixed by creating the strongly connected components of the directed graph in order to make sure that every vertex is accessible from every other vertex.

G=ox.geo_utils.get_largest_component(G,strongly=True)
malik
  • 1
  • 3