Open In App

Calculate Euclidean Distance Using Python OSMnx Distance Module

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads