How to Calculate Distance between Two Points using GEOPY
Calculating the geographical distance between two locations is a common requirement in mapping, logistics, delivery, transportation, and travel applications. Python provides the Geopy library, which makes it easier to calculate the distance between two points using their latitude and longitude coordinates.
This tutorial, we will learn how to calculate distance between two points using GEOPY. We will explore three approaches: Geodesic Distance, Great Circle Distance, and the Haversine Formula, along with Python examples and outputs.
Table of Contents

Installing Geopy in Python
Open Command Prompt, PowerShell, or the VS Code terminal and execute:
pip install geopy
Once the installation is complete, you can import the required distance functions and start working with geographical coordinates.
Methods to Calculate Distance Between Two Points
There are different ways to calculate the distance between two geographical coordinates. The appropriate method depends on how the Earth’s shape is represented and the level of accuracy required.
In this tutorial, we will use the following methods:
- Geodesic Distance
- Great Circle Distance
- Haversine Formula
1. Calculating Distance Using Geodesic Distance
Geodesic distance calculates the shortest path between two points while considering the Earth’s ellipsoidal shape. Because the Earth is not a perfect sphere, an ellipsoidal model can provide a more accurate geographical distance.
Geopy provides the geodesic function for performing this calculation.
Example
# Importing geodesic from geopy
from geopy.distance import geodesic as GD
# Coordinates of New York and Texas
New_York = (40.7128, -74.0060)
Texas = (31.9686, -99.9018)
# Calculating geodesic distance in kilometers
distance = GD(New_York, Texas).km
print(
"The distance between New York and Texas is:",
distance,
"KM"
)
Output
The distance between New York and Texas is: 2507.14797665193 KM
Explanation
The coordinates for New York and Texas are stored as tuples containing latitude and longitude values:
New_York = (40.7128, -74.0060)
Texas = (31.9686, -99.9018)
These coordinates are passed to GD(). The .km property is then used to obtain the calculated distance in kilometers.
GD(New_York, Texas).km
This method is useful when the calculation needs to account for the ellipsoidal shape of the Earth.
2. Calculating Distance Using Great Circle Distance
The second method is the Great Circle Distance. Unlike the geodesic approach, this method treats the Earth as a sphere.
Because of this assumption, its result may differ slightly from the geodesic calculation. Geopy provides the great_circle function for this method.
Example
# Importing great_circle from geopy
from geopy.distance import great_circle as GC
# Coordinates of New York and Texas
New_York = (40.7128, -74.0060)
Texas = (31.9686, -99.9018)
# Calculating great circle distance
distance = GC(New_York, Texas).km
print(
"The distance between New York and Texas is:",
distance,
"KM"
)
Output
The distance between New York and Texas is: 2503.045970189156 KM
Explanation
First, great_circle is imported and given the shorter name GC. The latitude and longitude coordinates of the two locations are then passed to it.
GC(New_York, Texas).km
The .km property returns the calculated distance in kilometers. Since the great circle method assumes a spherical Earth, its result is slightly different from the geodesic result.
3. Calculating Distance Using the Haversine Formula
The Haversine Formula can also be used to calculate the distance between two points when their latitude and longitude coordinates are available. It calculates the shortest distance between two points by treating the Earth as a sphere.
The calculation involves three main steps:
- Convert latitude or longitude values from degrees to radians.
- Apply the Haversine formula to calculate the central angle between the two points.
- Multiply the central angle by the Earth’s radius to obtain the distance.
Python Example
from math import radians, sin, cos, sqrt, asin
# Function to calculate distance in kilometers
def haversine_km(lat1, lon1, lat2, lon2):
# Convert coordinates from degrees to radians
lat1, lon1, lat2, lon2 = map(
radians,
[lat1, lon1, lat2, lon2]
) # Difference between coordinates dlat = lat2 – lat1 dlon = lon2 – lon1 # Haversine formula 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): lat1, lon1, lat2, lon2 = map( radians,
[lat1, lon1, lat2, lon2]
) 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 lat2, lon2 = 31.9686, -99.9018 # 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
Explanation
The radians() function converts the latitude and longitude values from degrees to radians. The differences between the two coordinates are then calculated using dlat and dlon.
The Haversine calculation determines the central angle between the locations. Finally, that angle is multiplied by the selected Earth-radius value to obtain the approximate geographical distance.
The first function returns the result in kilometers, while the second returns it in miles.
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
YT:- DecodeIT
Comparison of Distance Calculation Methods
| Method | Earth Model | Suitable For |
|---|---|---|
| Geodesic Distance | Ellipsoidal | Calculations requiring higher geographical accuracy |
| Great Circle Distance | Spherical | General geographical distance calculations |
| Haversine Formula | Spherical | Manual coordinate-based distance calculations |
Frequently Asked Questions
1. What is Geopy in Python?
Geopy is a Python library that provides tools for working with geographical locations and performing operations such as distance calculations.
2. How do I install Geopy?
You can install it using pip install geopy from your terminal or command prompt.
3. How can I calculate distance using latitude and longitude?
You can provide the latitude and longitude of both locations to Geopy’s geodesic or great_circle functions.
4. What is geodesic distance?
Geodesic distance represents the shortest path between two locations while accounting for an ellipsoidal model of the Earth.
5. What is great circle distance?
Great circle distance calculates the shortest path between two points while approximating the Earth as a sphere.
6. What is the Haversine Formula?
The Haversine Formula is a mathematical method for calculating the distance between two latitude-longitude coordinates on a spherical model.
7. Can Geopy return distance in kilometers?
Yes. A Geopy distance object provides the .km property for retrieving the calculated distance in kilometers.
8. Why are geodesic and great circle results different?
The results differ because geodesic calculations use an ellipsoidal Earth model, while great circle calculations approximate the Earth as a sphere.
Conclusion
In this tutorial, we learned how to calculate distance between two points using GEOPY and geographical coordinates. The geodesic method accounts for the Earth’s ellipsoidal shape, while the great circle method uses a spherical approximation. We also implemented the Haversine Formula manually to calculate distances in kilometers and miles.
These techniques can be useful when developing Python applications involving maps, transportation, logistics, travel, or any project that needs distance calculations between latitude and longitude coordinates.
Keywords: How to Calculate Distance between Two Points using GEOPY, python calculate distance between two coordinates latitude longitude, calculate distance between two coordinates formula, geopy distance Python, geopy.distance Python, calculate distance between two latitude longitude points, from geopy.distance import geodesic, geopy distance meters, great circle distance Python, Haversine formula Python, calculate distance between two points Python, latitude longitude distance Python