geopy
is a Python library that simplifies the process of calculating distances between geographical points on the Earth’s surface. It supports various distance calculations and geocoding operations. The most common use case is to compute the distance between two latitude/longitude coordinates.
1. Installing Geopy
First, you’ll need to install the geopy
library. You can do this using pip
:
pip install geopy
2. Calculating Distance Between Two Points
The geopy.distance
module provides different classes for calculating the distance between two points. The most commonly used class is geopy.distance.distance
, which calculates the geodesic distance (shortest distance between two points on the Earth’s surface).
2.1. Example: Calculating Geodesic Distance
from geopy.distance import geodesic
# Define the coordinates of the two points
point_1 = (40.748817, -73.985428) # Example: New York City (Empire State Building)
point_2 = (34.052235, -118.243683) # Example: Los Angeles (Downtown)
# Calculate the geodesic distance between the two points
distance = geodesic(point_1, point_2).miles
# Print the distance
print(f"The distance between New York City and Los Angeles is {distance:.2f} miles.")
Output:
The distance between New York City and Los Angeles is approximately 2445.56 miles.
3. Using Other Distance Calculation Methods
Besides geodesic distance, Geopy also supports other distance calculations like great-circle distance, Vincenty distance (deprecated), and more. Here’s how you can use some of these methods:
3.1. Great-Circle Distance
The great-circle distance is the shortest path between two points on the surface of a sphere. It is often used in navigation.
from geopy.distance import great_circle
# Calculate the great-circle distance between the two points
distance = great_circle(point_1, point_2).miles
# Print the distance
print(f"The great-circle distance between New York City and Los Angeles is {distance:.2f} miles.")
3.2. Vincenty Distance (Deprecated)
The Vincenty method was used to calculate the distance between two points on the ellipsoid. It is now deprecated in favor of more accurate and robust methods like geodesic.
from geopy.distance import geodesic # Vincenty is deprecated
# Calculate the geodesic distance (preferred over Vincenty)
distance = geodesic(point_1, point_2).miles
# Print the distance
print(f"The geodesic distance between New York City and Los Angeles is {distance:.2f} miles.")
4. Calculating Distance in Kilometers
By default, geopy
returns distances in kilometers if you use the .km
attribute instead of .miles
:
from geopy.distance import geodesic
# Calculate the distance in kilometers
distance_km = geodesic(point_1, point_2).km
# Print the distance in kilometers
print(f"The distance between New York City and Los Angeles is {distance_km:.2f} kilometers.")
5. Handling Errors and Exceptions
When calculating distances, you might encounter errors if the coordinates are invalid or if there are network issues when geocoding addresses. Always handle exceptions to make your code more robust:
from geopy.distance import geodesic
from geopy.exc import GeopyError
try:
# Attempt to calculate the distance
distance = geodesic(point_1, point_2).miles
print(f"The distance is {distance:.2f} miles.")
except GeopyError as e:
print(f"An error occurred: {e}")
The geopy
library is a powerful tool for calculating distances between geographical points. Whether you’re working with geodesic distances, great-circle distances, or other methods, geopy
makes it easy to perform these calculations with just a few lines of code. It’s widely used in geographic data analysis, travel planning, and many other applications where spatial distance matters.