Have you wondered how we calculate distance using longitude and latitude in python? Well, let’s figure it out. In this article, we explore four methods to calculate the distance between two points using latitude and longitude in Python. These methods include the Haversine formula, Math module, Geodesic distance, and Great Circle formula. Each method has its own implementation and advantages in various applications.
Code Implementation to Find Distance Between Two Locations using Latitude and Longitude
To calculate the distance between two points based on latitude and longitude in Python, you can use various methods such as the Haversine formula, math module, geodesic distance, or the great circle distance. These calculations are useful in applications involving location-based data, such as navigation, mapping, geolocation, tracking, logistics, delivery, outdoor activities, and research.
Let’s get right into the implementation now.
Example 1: Using Haversine Formula
The Haversine formula is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical triangles. It’s used to calculate the shortest distance between two points on the Earth’s surface. It provides good accuracy for small distances.
import haversine as hs from haversine import Unit loc1=(19.0760, 72.8777) loc2=(18.5204, 73.8567) result=hs.haversine(loc1,loc2,unit=Unit.KILOMETERS) print("The distance calculated is:",result)
We would be using
haversine module to use the haversine formula which would be utilized to calculate the distance between two locations. This module is imported by its alias hs with that we also import Unit to later change the unit of result according to our preference eg meters, kilometers, or miles. In
loc1 we mention Mumbai’s coordinates and in
loc2 Pune’s coordinates. After calculating the distance using
hs.haversine() we store it in variable
result .And later print it.
Note: To install the haversine module use this command in the command prompt :
pip install haversine
The distance calculated is : 120.15246793062427
Example 2: Using Math Module
This method uses the spherical law of cosines, which is a trigonometric formula that calculates the great-circle distance between two points on the Earth’s surface. It’s simpler than Haversine but may be less accurate for long distances due to floating-point rounding errors.
from math import radians, sin, cos, acos print("Input coordinates of two points:") mlat = radians(float(input("Location 1 latitude: "))) mlon = radians(float(input("Location 2 longitude: "))) plat = radians(float(input("Location 1latitude: "))) plon = radians(float(input("Location 2 longitude: "))) dist = 6371.01 * acos(sin(mlat)*sin(plat) + cos(mlat)*cos(plat)*cos(mlon - plon)) print("The distance is %.2fkm." % dist)
In this method, we use the math module to calculate the distance between two points using their latitude and longitude coordinates.
We convert the coordinates from degrees to radians and use the sine and cosine functions along with the Earth’s mean radius (6371.01 km) to calculate the distance. The acos() function is used to compute the arccosine of the central angle between the two locations.
sin(mlat)is the sine of the latitude of a location
sin(plat)is the sine of the latitude of another location
cos(mlat)is the cosine of the latitude of location
cos(plat)is the cosine of the latitude of location
cos(mlon - plon)is the cosine of the difference between the longitudes of the two locations
- The expression
(sin(mlat)*sin(plat) + cos(mlat)*cos(plat)*cos(mlon - plon))calculates the cosine of the central angle between the two locations
acos()is used to calculate the arccosine of the central angle
6371.01is the mean radius of the Earth in kilometers.
Example 3: Using Geodesic Distance
The geodesic method uses the geodesic distance, which is the shortest path between two points along the Earth’s surface. It is more accurate than the Haversine and spherical law of cosines methods, as it accounts for the Earth’s ellipsoidal shape. The Geopy library provides an easy-to-use implementation of this method.
from geopy.distance import geodesic as GD Mumbai =(19.0760, 72.8777) Pune =(18.5204, 73.8567) print("The distance between Mumbai and Pune is: ", GD(Mumbai,Pune).km)
geodesic module from
geopy library to assist us in calculating the distance. After mentioning the coordinates of locations we use GD() function to calculate the distance.
Example 4: Using Great Circle Formula
The great-circle distance is the shortest distance between two points on the surface of a sphere. The great-circle formula is derived from the spherical law of cosines, but with some optimizations for better accuracy. It is a simpler alternative to the geodesic method but may not be as accurate for very long distances or locations near the poles. The Geopy library also provides an implementation of the great-circle distance calculation through the
from geopy.distance import great_circle as GRC Mumbai =(19.0760, 72.8777) Pune =(18.5204, 73.8567) print("The distance between Mumbai and Pune is: ", GRC(Mumbai,Pune).km)
This code is similar to the one before used the only difference is the function used, here we use
We’ve explored four methods to calculate distances between two points using latitude and longitude in Python. These techniques have numerous applications in navigation, mapping, geolocation, logistics, outdoor activities, and research. Which method do you find most suitable for your use case?
Read more interesting articles: