Calculating the distance between two points on the Earth's surface is a common problem in many fields, including GPS, navigation, and geography applications. Geographic coordinates are represented in terms of latitude and longitude, and the challenge is to calculate the distance between these points accurately.
In this article, we will see how to use Python to calculate the distance between two geographic coordinates, expressed in kilometers. We will use several methodologies, including the Haversine formula, one of the most widely used methods for calculating spherical distances.
The haversine formula is an equation that allows you to calculate the distance of the shortest path (the "orthodromic distance") between two points on a sphere, specifically on the Earth's surface, considering the curvature of the planet.
The formula is as follows:
To calculate the distance between two coordinates with the Haversine formula, we can use the Python library math
to perform the necessary trigonometric calculations.
import math
def haversine(coord1, coord2):
# Radius of the Earth in kilometers
R = 6371.0
# Coordinates (latitude and longitude) in radians
lat1, lon1 = math.radians(coord1[0]), math.radians(coord1[1])
lat2, lon2 = math.radians(coord2[0]), math.radians(coord2[1])
# Differences between coordinates
dlat = lat2 - lat1
dlon = lon2 - lon1
# Haversine formula
a = math.sin(dlat / 2)**2 + math.cos(lat1) * math .cos(lat2) * math.sin(dlon / 2)**2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
# Final distance in kilometers
distance = R * c
return distance
# Example usage
coord1 = (41.9028, 12.4964) # Coordinates of Rome (latitude, longitude)
coord2 = (48.8566, 2.3522) # Coordinates of Paris (latitude, longitude)
distance = haversine(coord1, coord2)
print(f"The distance between Rome and Paris is about {distance:.2f} km")
Code explanation:
Importing the
math
library: this library is needed for trigonometric functions such as sine, cosine and arctangent.Conversion to radians: since trigonometric functions in Python work in radians, we convert coordinates from degrees to radians using the
math.radians()
function.Calculating the difference in latitude and longitude: we calculate the difference in latitude and longitude between the two points.
Haversine formula application: The formula is applied to calculate the distance in kilometers. The variable
a
represents a part of the formula that is used in the arctangent to obtain the final result.Distance calculation: The final distance is obtained by multiplying the radius of the Earth by the calculated angle.
There are also external libraries that simplify the calculation of geographic distances without having to manually implement the Haversine formula. One of the most used is geopy
, which provides a set of tools for geographic operations.
Here is how to use geopy
to calculate the distance between two coordinates:
from geopy.distance import geodesic
# Coordinates of Rome and Paris
coord1 = (41.9028, 12.4964)
coord2 = (48.8566, 2.3522)
# Calculating the distance in kilometers
distance = geodesic(coord1, coord2).kilometers
print(f"The distance between Rome and Paris is approximately {distance:.2f} km")
geopy
uses an ellipsoidal model for the Earth, which provides a more accurate estimate than the formula of Haversine.
Conclusion
Calculating the distance between two geographic coordinates is one of the most common applications in navigation systems and mapping applications. Python, with the use of the Haversine formula or libraries like geopy
, makes this calculation easy and precise.