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


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

PHP PROJECT:- CLICK HERE

INTERVIEW QUESTION:-CLICK HERE

Complete Advance AI topics:- CLICK HERE

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

See also  Web Scraping Using Python

Post Comment