Reputation: 67
I want to ask if there is a way to find the accurate shortest path between 2 coordinates. The 2 coordinates are (-33.889606, 151.283306), (-33.889927, 151.280497) as shown in the picture. The black path is the ideal path, and the red one uses get_nearest_node. Here are the codes:
import folium
import osmnx as ox
import networkx as nx
ox.config(use_cache=True, log_console=True)
G = ox.graph_from_point((-33.889606, 151.283306), dist=3000, network_type='drive')
G = ox.speed.add_edge_speeds(G)
G = ox.speed.add_edge_travel_times(G)
orig = ox.get_nearest_node(G, (-33.889606, 151.283306))
dest = ox.get_nearest_node(G, (-33.889927, 151.280497))
route = nx.shortest_path(G, orig, dest, 'travel_time')
route_map = ox.plot_route_folium(G, route)
route_map.save('test.html')
Upvotes: 3
Views: 7003
Reputation: 51
You can try using a package called Taxicab, which was created for this exact purpose.
import osmnx as ox
import taxicab as tc
import matplotlib.pyplot as plt
G = ox.graph_from_point((-33.889606, 151.283306), dist=3000, network_type='drive')
G = ox.speed.add_edge_speeds(G)
G = ox.speed.add_edge_travel_times(G)
orig = (-33.889606, 151.283306)
dest = (-33.889927, 151.280497)
route = tc.distance.shortest_path(G, orig, dest)
fig, ax = tc.plot.plot_graph_route(G, route, node_size=30, show=False, close=False, figsize=(10,10))
padding = 0.001
ax.scatter(orig[1], orig[0], c='lime', s=200, label='orig', marker='x')
ax.scatter(dest[1], dest[0], c='red', s=200, label='dest', marker='x')
ax.set_ylim([min([orig[0], dest[0]])-padding, max([orig[0], dest[0]])+padding])
ax.set_xlim([min([orig[1], dest[1]])-padding, max([orig[1], dest[1]])+padding])
plt.show()
Which for your situation will yield the following:
Disclaimer I authored the Taxicab module...
Upvotes: 5
Reputation: 6412
If you set simplify=False
in your graph_from_point
call, you will get far more nodes in your graph, allowing you to calculate the distance to those coordinates more precisely.
Upvotes: 5