Providing shortest path code snippet
Here's a complete Python example using OSMnx and NetworkX showing how to download a street graph, add travel time as edge weights based on speed limits, then compute the shortest path between two GPS coordinates using Dijkstra weighted by travel time.
Below is a minimal example showing how to pull a drivable street network from OpenStreetMap with osmnx, assign edge speeds and travel times, and then run a Dijkstra search (the default for weighted shortest paths in NetworkX) between two GPS points:
import osmnx as ox
import networkx as nx
# -------------------------------------------------------------------
# 1. Get a street network (drivable roads) for a given place or area
# -------------------------------------------------------------------
place_name = "Manhattan, New York, USA"
G = ox.graph_from_place(place_name, network_type="drive")
# -------------------------------------------------------------------
# 2. Add edge speeds and travel times
# - add_edge_speeds() will look for 'maxspeed' in OSM; if missing,
# it uses typical defaults by road type.
# - add_edge_travel_times() uses length / speed to compute seconds.
# -------------------------------------------------------------------
G = ox.speed.add_edge_speeds(G) # adds 'speed_kph' attribute
G = ox.speed.add_edge_travel_times(G) # adds 'travel_time' attribute (seconds)
# -------------------------------------------------------------------
# 3. Define origin and destination as GPS coords (lat, lon)
# -------------------------------------------------------------------
orig_lat, orig_lon = 40.758896, -73.985130 # Times Square
dest_lat, dest_lon = 40.7128, -74.0060 # Lower Manhattan
# -------------------------------------------------------------------
# 4. Find the nearest graph nodes to these points
# Note: nearest_nodes expects X=lon, Y=lat
# -------------------------------------------------------------------
orig_node = ox.distance.nearest_nodes(G, X=orig_lon, Y=orig_lat)
dest_node = ox.distance.nearest_nodes(G, X=dest_lon, Y=dest_lat)
# -------------------------------------------------------------------
# 5. Compute shortest path using travel_time as the weight (Dijkstra)
# -------------------------------------------------------------------
route = nx.shortest_path(G, orig_node, dest_node, weight="travel_time")
# You can also get the total travel time and distance for this path:
total_time = nx.shortest_path_length(G, orig_node, dest_node, weight="travel_time") # seconds
total_dist = nx.shortest_path_length(G, orig_node, dest_node, weight="length") # meters
print("Route node sequence:", route)
print(f"Estimated travel time: {total_time/60:.1f} minutes")
print(f"Total distance: {total_dist/1000:.2f} km")
# -------------------------------------------------------------------
# 6. (Optional) Plot the route
# -------------------------------------------------------------------
# fig, ax = ox.plot_graph_route(G, route, node_size=0)
Notes:
nx.shortest_path(..., weight="travel_time") uses Dijkstra’s algorithm by default.
- If some edges lack a
maxspeed tag, add_edge_speeds fills in reasonable defaults according to road type.
- For other areas or bounding boxes, use
graph_from_point or graph_from_bbox instead of graph_from_place.
- To drive on foot, cycle, etc., change
network_type.