Find Shortest Path Using Python OSMnx Routing Module
Last Updated :
18 Mar, 2024
This article demonstrates how to use the OSMnx routing module to find the shortest path, illustrated with practical examples.
Syntax of osmnx.routing.shortest_path() Function
OSMnx routing module has the 'shortest_path' functionality to find the nearest path from the origin node to the destination node. The function is as follows:
osmnx.routing.shortest_path(G, orig, dest, weight='length', cpus=1)
Parameters:
- G (networkx.MultiDiGraph) – input graph
- orig (int or list) – origin node ID, or a list of origin node IDs
- dest (int or list) – destination node ID, or a list of destination node IDs
- weight (string) – edge attribute to minimize when solving shortest path
- cpus (int) – how many CPU cores to use; if None, use all available. It allows parallel processing but make sure not to exceed the available RAM
Returns: path – list of node IDs constituting the shortest path, or, if orig and dest are lists, then a list of path lists
Return Type: List
The shortest_path functionality make use of Dijkstra's shortest path algorithm to find the shortest path between the origin node and destination node.
Find Shortest Path Using Python OSMnx Routing Module
Let's implement the functionality to find the shortest path. As a first step we need to get the origin node and destination node. We make use of Thiruvanathapuram city data using osmnx and fetch the origin and destination node w.r.t Kazhakkootam and Medical College respectively. The code as follows:
Python3
import osmnx as ox
# load thiruvananthapuram city
place = "Thiruvananthapuram, Kerala"
G = ox.graph_from_place(place, network_type="drive")
# kazhakootam coordinates
kzh_lat, kzh_lon = 8.5686, 76.8731
# medical college coordinates
mdcl_lat, mdcl_lon = 8.52202892500963, 76.926448394559
# fetch the nearest node w.r.t coordinates
kzh_node = ox.distance.nearest_nodes(G, kzh_lon, kzh_lat)
mdcl_node = ox.distance.nearest_nodes(G, mdcl_lon, mdcl_lat)
print("Kazhakkottam Node: {kzh_node}, \
Medical College Node: {mdcl_node}".format(
kzh_node=kzh_node, mdcl_node=mdcl_node))
Output:
Kazhakkottam Node: 1141007999, Medical College Node: 9992653265
Now we have the nodes. Let's calculate the shortest distance form Kazhakootam node (origin node) to Medical College node(destination node). The code as shown below:
Python3
import osmnx as ox
place = "Thiruvananthapuram, Kerala"
G = ox.graph_from_place(place, network_type="drive")
# orgin node and destination node
orig, dest = 1141007999, 9992653265
# find shortest path
route_nodes = ox.routing.shortest_path(G, orig, dest, weight="length")
# plot the shortes path
fig, ax = ox.plot_graph_route(G, route_nodes, route_color="r",
route_linewidth=6, node_size=0)
Output:
shortest path plotLet's plot it on map. We have to reconstruct the edges (u, v, k) based on the shortest route generated from above code. Fetch the graph edges from our multidigraph based on the constructed edges. The graph edges are multiindexed with (u, v, k) params. The code as follows:
Python3
def generate_multindex(route_nodes):
multiindex_list = []
# append the index to list
for u, v in zip(route_nodes[:-1], route_nodes[1:]):
multiindex_list.append((u, v, 0))
return multiindex_list
# get edges from from above multidigraph
gdf_nodes, gdf_edges = ox.graph_to_gdfs(G)
# generate multiindex based on generated shortest route
multiindex_list = generate_multindex(route_nodes)
# fetch edge details based on multi index list
shrt_gdf_edges = gdf_edges[gdf_edges.index.isin(multiindex_list)]
# plot the shortest route on map
shrt_gdf_edges.explore(color="red")
Output:
The path in red color depicts the shortest path from Kazhakkottam to Medical Colledge.
shortest path
Similar Reads
Find path to the given file using Python We can get the location (path) of the running script file .py with __file__. __file__ is useful for reading other files and it gives the current location of the running file. It differs in versions. In Python 3.8 and earlier, __file__ returns the path specified when executing the Python command. We
5 min read
Find the Nearest Edge to a Point Using Python OSMnx Distance Module OSMnx distance module can find the nearest edge to a point using the nearest_edges functionality. In this article, we will see how to find the nearest edge to a point using the OSMnx distance Module in Python. Syntax of osmnx.distance.nearest_edges() FunctionHere, the below function uses an R-tree s
5 min read
OS Path module in Python OS Path module contains some useful functions on pathnames. The path parameters are either strings or bytes. These functions here are used for different purposes such as for merging, normalizing, and retrieving path names in Python. All of these functions accept either only bytes or only string obje
2 min read
Shortest Path Problem Between Routing Terminals - Implementation in Python The famous Dijkstra's algorithm can be used in a variety of contexts - including as a means to find the shortest route between two routers, also known as Link state routing. This article explains a simulation of Dijkstra's algorithm in which the nodes (routers) are terminals. Once the shortest path
6 min read
Posixpath Module in Python The posixpath module is a built-in Python module that provides functions for manipulating file paths in a way that adheres to the POSIX standard. This module abstracts the differences in path handling between various operating systems, including Unix-like systems (e.g., Linux, macOS) and Windows. Im
3 min read
Find the Nearest Node to a Point Using OSMnx Distance Module Nearest neighbor search algorithms are crucial for robust navigation. OSMnx makes use of different types of nearest neighbor search algorithms for the projected and unprotected graph. In this article, we will see how we can find the nearest node to a point using OSMnx distance module in Python. Synt
7 min read