Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
How to Calculate Distance between Two Points using GEOPY - How to Calculate Distance between Two Points using GEOPY

How to Calculate Distance between Two Points using GEOPY

Posted on November 30, 2024December 21, 2024 By Rishabh saini No Comments on How to Calculate Distance between Two Points using GEOPY

How to Calculate Distance between Two Points using GEOPY

The Geopy library in Python makes it simple to calculate the geographical distance between two locations on Earth. Whether you’re working on logistics, mapping, or travel-based applications, this tutorial will guide you through multiple methods to determine the distance between two points using latitude and longitude.

Let’s dive in and explore the key techniques!

Download New Real Time Projects :-Click here

How to Calculate Distance between Two Points using GEOPY.

How to Calculate Distance between Two Points using GEOPY.


Installing Geopy

Use the following command to install Geopy before using it:

pip install geopy

Once installed, you’re ready to use its powerful features for geographical calculations.


Methods to Calculate Distance Between Two Points

Geopy offers different ways to calculate distances, each suited to specific needs. Let’s go over each of these approaches individually:


Method 1: Using Geodesic Distance

The shortest path between two locations on the surface of the Earth is known as the geodesic distance. It takes the Earth’s ellipsoidal shape into account, ensuring high accuracy.

Example:

# Importing the geodesic module from geopy
from geopy.distance import geodesic as GD

# New York and Texas' latitudes and longitudes

New_York = (40.7128, -74.0060)
Texas = (31.9686, -99.9018)

# Calculating the geodesic distance in kilometers
print("The distance between New York and Texas is:", GD(New_York, Texas).km, "KM")

Output:

The distance between New York and Texas is: 2507.14797665193 KM


Method 2: Using Great Circle Distance

Assuming that Earth is a perfect sphere, the great circle distance is computed. While slightly less accurate than geodesic, it’s faster and often sufficient for many applications.

Example:

# Importing the great_circle module from geopy
from geopy.distance import great_circle as GC

# Latitude and longitude of New York and Texas
New_York = (40.7128, -74.0060)
Texas = (31.9686, -99.9018)

# Calculating the great circle distance in kilometers
print("The distance between New York and Texas is:", GC(New_York, Texas).km, "KM")

Output:

The distance between New York and Texas is: 2503.045970189156 KM


Method 3: Using the Haversine Formula

The Haversine formula calculates the orthodromic distance (shortest path) between two points on a sphere, based on their latitudes and longitudes. This approach gives precise results, especially for small distances.

How It Works:

  1. Convert degrees of latitude and longitude to radians.
  2. To find the central angle between two points, use the Haversine formula.
  3. To find the distance, multiply by the radius of the Earth.

Example:

from math import radians, sin, cos, sqrt, asin

# Function to calculate distance in kilometers
def haversine_km(lat1, lon1, lat2, lon2):
    # Convert degrees to radians
    lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
    
    # Haversine formula
    dlat = lat2 - lat1
    dlon = lon2 - lon1
    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * asin(sqrt(a))
    
    # Earth's radius in kilometers
    r_km = 6371.0
    return c * r_km

# Function to calculate distance in miles
def haversine_miles(lat1, lon1, lat2, lon2):
    # Convert degrees to radians
    lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
    
    # Haversine formula
    dlat = lat2 - lat1
    dlon = lon2 - lon1
    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * asin(sqrt(a))
    
    # Earth's radius in miles
    r_mi = 3963.0
    return c * r_mi

# Coordinates of New York and Texas
lat1, lon1 = 40.7128, -74.0060  # New York
lat2, lon2 = 31.9686, -99.9018  # Texas

# Calculate distances
print("The distance between New York and Texas is:", haversine_km(lat1, lon1, lat2, lon2), "KM")
print("The distance between New York and Texas is:", haversine_miles(lat1, lon1, lat2, lon2), "Miles")

Output:

The distance between New York and Texas is: 2503.04243426357 KM
The distance between New York and Texas is: 1556.985899699659 Miles


Comparison of Methods

MethodAccuracyUse Case
Geodesic DistanceHigh (most accurate)Applications requiring precision
Great Circle DistanceModerateGeneral-purpose calculations
Haversine FormulaHighSuitable for both precision and speed


  1. PHP PROJECT:- CLICK HERE
  2. INTERVIEW QUESTION:-CLICK HERE
  3. Complete Advance AI topics:- CLICK HERE
  4. Complete Python Course with Advance topics:- CLICK HERE


  • python calculate distance between two coordinates latitude/longitude
  • How to Calculate Distance between Two Points using GEOPY
  • calculate distance between two coordinates formula
  • geopy.distance python
  • calculate distance between two latitude/longitude points
  • geopy distance meters
  • How to Calculate Distance between Two Points
  • How to Calculate Distance between Two Points using GEOPY
  • geopandas distance between two points
  • from geopy.distance import geodesic
  • geopy distance vectorized
  • geopy distance python
  • How to Calculate Distance between Two Points
  • calculate distance between two latitude longitude points
  • How to Calculate Distance between Two Points using GEOPY

Post Views: 1,064
Python Tags:calculate distance between two city, calculate distance between two city using python, distance between two points, find distance between two city using python, find distance between two locations using python, how to calculate distance in map, python distance between two addresses, python distance between two points, python distance between two points 3d, python distance between two vectors, python distance between two zip codes, python distance from point to line

Post navigation

Previous Post: Online Ticket Reservation System Using PHP With Source Code
Next Post: User Login & Registration System Using PHP and MySQL Free Code

More Related Articles

Python List Comprehension: A Complete Guide - Python List Comprehension Python List Comprehension: A Complete Guide Python
Read Operation in Python with MySQL - Read Operation in Python with MySQL Read Operation in Python with MySQL Python
Python statistics Module Essential Functions for Data Analysis - Python statistics Module Python statistics Module Essential Functions for Data Analysis Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. Python Decorators: A Comprehensive Guide
  2. Python High-Order Functions: A Comprehensive Guide
  3. Finding the Second Largest Number in Python
  4. Python Constructor: A Guide to Initializing Objects in Python
  5. Weather Information App
  6. How to Install Django: Step-by-Step Guide

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,614)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,215)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,869)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme