Calculate Euclidean Distance Using Python OSMnx Distance Module
Last Updated :
18 Mar, 2024
Euclidean space is defined as the line segment length between two points. The distance can be calculated using the coordinate points and the Pythagoras theorem. In this article, we will see how to calculate Euclidean distances between Points Using the OSMnx distance module.
Syntax of osmnx.distance.euclidean() Function
The vectorized function to calculate the Euclidean distance between two points’ coordinates or between arrays of points’ coordinates is as follows:
osmnx.distance.euclidean(y1, x1, y2, x2)
Parameters:
- y1 (float or numpy.array of float) – first point’s y coordinate
- x1 (float or numpy.array of float) – first point’s x coordinate
- y2 (float or numpy.array of float) – second point’s y coordinate
- x2 (float or numpy.array of float) – second point’s x coordinate
Note: For accurate results, use projected coordinates rather than decimal degrees
Returns: dist – distance from each (x1, y1) to each (x2, y2) in coordinates’ units
Return Type: Float or numpy.array of float
Calculate Euclidean Distance Using Python OSMnx Distance Module
Below, are the example of how to calculate Euclidean distances between Points Using OSMnx distance module in Python:
Geographic Coordinate Reference System uses latitude and longitude to specify a location on Earth. Projected Coordinate System, on the other hand, employs a two-dimensional XY plane for Euclidean distance calculations, using northing (Y) and easting (X) values in meters. Northing is the distance from the equator, and easting is the distance from the central meridian (longitude). The output is the Euclidean distance in meters.
Create GeoDataFrame with Geographic Coordinates
In below code Using geopandas
and shapely
, this code creates a GeoDataFrame named gdf
from latitude and longitude coordinates for Thiruvananthapuram and Kollam, specifying the coordinate reference system as EPSG:4326.
Python3
import geopandas as gd
from shapely.geometry import Point
# location coordinates dictionary
tvm_lat, tvm_lon = 8.506, 76.96153
kol_lat, kol_lon = 8.88795, 76.59550
coordinate_dict = {'name': ['Thiruvananthapuram', 'Kollam'],
'geometry': [Point(tvm_lon, tvm_lat),
Point(kol_lon, kol_lat)]}
# convert to geodataframe
# EPSG:4326 is a geographic coordinate system
gdf = gd.GeoDataFrame(coordinate_dict, crs="EPSG:4326")
print(gdf)
Output
name geometry
0 Thiruvananthapuram POINT (76.96153 8.50600)
1 Kollam POINT (76.59550 8.88795)
Applying Coordinate Projection
As a next step, we can convert the geographic coordinates to projected coordinates using to_crs() functionality. Here we will be using EPSG:7781 Coordintae Reference System (CRS).
below, code applies a coordinate projection to transform the GeoDataFrame `gdf` from EPSG:4326 to EPSG:7781, and then prints the transformed GeoDataFrame `gdf_7781`.
Python3
# apply projection
gdf_7781 = gdf.geometry.to_crs("EPSG:7781")
Output:
0 POINT (1105863.487 779603.870)
1 POINT (1065495.736 821765.127)
Name: geometry, dtype: geometry
Calculating Euclidean Distance
In below code we uses OSMnx and GeoPandas to convert geographic coordinates of Thiruvananthapuram and Kollam to a projected coordinate system (EPSG:7781) and then calculates the Euclidean distance between the two locations using the OSMnx library.
Python3
import osmnx as ox
import geopandas as gd
from shapely.geometry import Point
# location coordinates dictionary
tvm_lat, tvm_lon = 8.506, 76.96153
kol_lat, kol_lon = 8.88795, 76.59550
coordinate_dict = {'name': ['Thiruvananthapuram', 'Kollam'],
'geometry': [Point(tvm_lon, tvm_lat),
Point(kol_lon, kol_lat)]}
# convert to geodataframe
# EPSG:4326 is a geographic coordinate system
gdf = gd.GeoDataFrame(coordinate_dict, crs="EPSG:4326")
# apply projection
gdf_7781 = gdf.geometry.to_crs("EPSG:7781")
# projected coordinates
tvm_lon_utm = gdf_7781.geometry[0].x
tvm_lat_utm = gdf_7781.geometry[0].y
kol_lon_utm = gdf_7781.geometry[1].x
kol_lat_utm = gdf_7781.geometry[1].y
# calculate eucledian distance
ox.distance.euclidean(kol_lon_utm, kol_lat_utm, tvm_lon_utm, tvm_lat_utm)
Output:
58370.59962990761
So, the eucledian distance between Kollam and Thiruvananthapuram is 58370.59 meters or 58.37 KM.
Pass as Array of Calculated Euclidean Distance
In below code we uses NumPy, OSMnx, and GeoPandas to convert latitude and longitude coordinates of Thiruvananthapuram, Mumbai, and New Delhi to a projected coordinate system (EPSG:24378) and calculates the Euclidean distance between Mumbai and Thiruvananthapuram.
Python3
import numpy as np
import osmnx as ox
import geopandas as gd
from shapely.geometry import Point
# lat-lon points
# Thiruvananthapuram
tvm_lat, tvm_lon = 8.50606, 76.96153
# Mumbai
mum_lat, mum_lon = 19.0760, 72.8777
# New Delhi
nwdelhi_lat, nwdelhi_lon = 28.6139, 77.2090
# coordinate dictionary
coordinate_dict = {'name': ['Thiruvananthapuram', 'Mumbai', 'New Delhi'],
'geometry': [Point(tvm_lon, tvm_lat),
Point(mum_lon, mum_lat),
Point(nwdelhi_lon, nwdelhi_lat)]}
# convert dictionary to geodataframe
gdf = gd.GeoDataFrame(coordinate_dict, crs="EPSG:4326")
# convert to projected coordinates
gdf_24378 = gdf.geometry.to_crs("EPSG:24378")
# projected coordinates
# Thiruvananthapuram
proj_tvm_lon = gdf_24378.geometry[0].x
proj_tvm_lat = gdf_24378.geometry[0].y
# Mumbai
proj_mum_lon = gdf_24378.geometry[1].x
proj_mum_lat = gdf_24378.geometry[1].y
# New Delhi
proj_dlh_lon = gdf_24378.geometry[2].x
proj_dlh_lat = gdf_24378.geometry[2].y
# set from and to projected points
from_lat = np.array([proj_tvm_lat, proj_dlh_lat])
from_lon = np.array([proj_tvm_lon, proj_dlh_lon])
to_lat = np.array([proj_mum_lat, proj_tvm_lat])
to_lon = np.array([proj_mum_lon, proj_tvm_lon])
# calculate the euclidean distance
ox.distance.euclidean(from_lon, from_lat, to_lon, to_lat)
Output
array([1314897.39984368, 2298605.77047159])
From the output, we can conclude that the euclidean distance between Thrivananthapuram and Mumbai is 1314897.39 meters or 1314.897 KM and the euclidean distance between New Delhi and Thiruvananthapuram is 2298605.77 meters or 2298.605 KM.
Similar Reads
Calculate the Euclidean distance using NumPy
Euclidean distance is the shortest between the 2 points irrespective of the dimensions. In this article to find the Euclidean distance, we will use the NumPy library. This library used for manipulating multidimensional array in a very efficient way. Let's discuss a few ways to find Euclidean distanc
3 min read
Calculate Great Circle Distances Between Pairs of Points Using OSMnx Module
The Great Circle Distance evaluates the shortest distance between two points considering Earth as a sphere. It is an arc linking two points on a sphere. Here, we will see how to calculate great circle distances between pairs of points using the OSMnx distance module. Syntax of osmnx.distance.great_c
3 min read
Calculate Graph Total Edge Length Using Python OSMnx Stats Module
In this article, we will see how to calculate graphs' total edge length using the OSMNx stats Module. Explore urban connectivity with precision using the OSMnx stats module, which allows us to effortlessly calculate the total edge length of graphs derived from OpenStreetMap data, providing valuable
2 min read
Calculate distance and duration between two places using google distance matrix API in Python
Google Map Distance Matrix API is a service that provides travel distance and time is taken to reach a destination. This API returns the recommended route(not detailed) between origin and destination, which consists of duration and distance values for each pair. To use this API, one must need the AP
2 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
Python | Calculate Distance between two places using Geopy
GeoPy is a Python library that makes geographical calculations easier for the users. In this article, we will see how to calculate the distance between 2 points on the earth in two ways. How to Install GeoPy ? pip install geopy Geodesic Distance: It is the length of the shortest path between 2 point
1 min read
How To Calculate Mahalanobis Distance in Python
Mahalanobis distance is defined as the distance between two given points provided that they are in multivariate space. This distance is used to determine statistical analysis that contains a bunch of variables. The user needs to install and import the following libraries for calculating Mahalanobis
4 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
Get OSM Features Within a Distance of a Point Using Python OSMnx Feature Module
In this article, we will see how we can get open street map features within a distance of a point (latitude-longitude) using the OSMnx feature module in Python. Syntax of osmnx.features.features_from_point() FunctionThe function creates a GeoDataFrame of OSM features within some distance of a point
3 min read
Calculate Compass Bearing Using Python OSMnx Bearing Module
Bearing is a significant angle for Geographic Information System (GIS) applications. In this article, we will see how to calculate the compass Bearing using the OSMnx bearing module based on lat-Ion Points. What is Bearing?Bearing represents the clockwise angle in degrees between the north and the g
3 min read