Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Find the Nearest Edge to a Point Using Python OSMnx Distance Module
Next article icon

Calculate Euclidean Distance Using Python OSMnx Distance Module

Last Updated : 18 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
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.


Next Article
Find the Nearest Edge to a Point Using Python OSMnx Distance Module
author
sonugeorge
Improve
Article Tags :
  • Python
  • python
Practice Tags :
  • python
  • python

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
  • 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
  • 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
  • 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
  • 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
  • Python | Distance-time GUI calculator using Tkinter
    Prerequisites : Introduction to Tkinter | Using google distance matrix API Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Pyth
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences