![]() |
VOOZH | about |
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.
Here, the below function uses an R-tree spatial index and minimizes the Euclidean distance from each point to the possible matches.
osmnx.distance.nearest_edges(G, X, Y, interpolate=None, return_dist=False)
Parameters
- G (networkx.MultiDiGraph) โ graph in which to find nearest edges
- X (float or list) โ pointsโ x (longitude) coordinates, in same CRS/units as graph and containing no nulls
- Y (float or list) โ pointsโ y (latitude) coordinates, in same CRS/units as graph and containing no nulls
- interpolate (float) โ deprecated, do not use
- return_dist (bool) โ optionally also return distance between points and nearest edges
Returns : ne or (ne, dist) โ nearest edges as (u, v, key) or optionally a tuple where dist contains distances between the points and their nearest edges.
Return Type: tuple or list
To find the nearest edge to a point, we need to pass a multidigraph and x-y coordinates as parameters. The functionality returns the nearest edge from the multidigraph w.r.t the x-y coordinate. here, the step-by-step example of how to find the nearest edge to a point using OSMnx distance module in Python:
Let's start by creating a multidigraph that has 4 nodes and 2 edges. Here we will consider osmid of 4 coordinates as nodes: Thiruvanathapuram, Kollam, Palakkad and Bangalore and the edges as: (Thiruvananthapuram - Bangalore) and (Kollam - Palakkad). Consider Kochi coordinates as the X-Y points and identify the nearest edge w.r.t Kochi. The coordinates are as follows:
As a next step, let's get the osmid (nodes) for the corresponding coordinates. Using Geopy's Nominatim module, the code retrieves the OpenStreetMap (OSM) ID for a location based on its latitude and longitude coordinates (here, for Bangalore).
Output
38773287
Similarly you can fetch the osmid for the remaining coodinates. OpenStreetMap (OSM) IDs assigned to the geographical coordinates for Thiruvananthapuram, Kollam, Bangalore, Palakad.
Now, its time to create the multidigraph that has the necessary nodes and edges. Using NetworkX library, a multidigraph is initialized with EPSG:4326 coordinate reference system. Nodes are added with OSM IDs and corresponding coordinates (latitude and longitude).
To get accurate distance from the nearest edge, it is better to use projected coordinates. Let's convert multidigraph to projected coordinates using geodataframe. Using OSMnx library, the multidigraph is converted into two GeoDataFrames: one for nodes and one for edges. The node coordinates are projected to EPSG:7781.
Output
x y geometry
osmid
955820326 1.105863e+06 7.796105e+05 POINT (1105863.471 779610.506)
38773287 1.173015e+06 1.273927e+06 POINT (1173015.423 1273927.376)
464139803 1.071608e+06 1.031786e+06 POINT (1071608.015 1031786.480)
281828280 1.065496e+06 8.217651e+05 POINT (1065495.736 821765.127)
geometry
u v key
955820326 38773287 0 LINESTRING (1105863.471 779610.506, 1173015.42...
281828280 464139803 0 LINESTRING (1065495.736 821765.127, 1071608.01...
The edges on map as shown below:
Now we need to convert projected geodataframe back to multidigraph. Using OSMnx library, the GeoDataFrames containing nodes and edges are converted back into a multidigraph with the specified CRS attribute (EPSG:7781).
We have multidigraph which has projected coordinates. Since every parameter must be of same CRS unit, we need to convert the X-Y coordinates (Kochi) to projected coordinates. A GeoDataFrame is created for the Kochi node using GeoPandas, with a point geometry representing its coordinates.
We have projected geometry for the parameters. Its time to make use of 'nearest_edge' functionality to find the nearest edge. Using OSMnx library, the code finds the nearest edges to the Kochi node within the projected multidigraph, providing the distances from the node to the nearest edge.
Output
((281828280, 464139803, 0), 39525.24413890172)
The output has the nearest edge and the distance. The nearest edge is (281828280, 464139803, 0) - (Kollam - Palakkad) and the distance is 39525.244 meters