简体   繁体   中英

OSMnx: Is there a way to find an accurate shortest path between 2 coordinates?

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

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM