Geopy is a Python library designed to facilitate the calculation of geographical distances. In this tutorial, we will explore various techniques that users can employ to determine the distance between two locations on the planet.
Initially, the user needs to install the geopy library by executing the command below:
pip install geopy
Once the installation has been completed successfully, we can begin utilizing the geopy library.
Calculate Distance between Two Points
Here are the key techniques employed to determine the distance between two points.
Method 1: By using Geodesic Distance
The geodesic distance refers to the measurement of the shortest route connecting two points on the surface of the Earth. In the subsequent example, we will demonstrate how a user can compute the Geodesic Distance using latitude and longitude coordinates.
Example:
# First, import the geodesic module from the geopy library
from geopy.distance import geodesic as GD
# Then, load the latitude and longitude data for New York & Texas
New_York = (40.7128, 74.0060)
Texas = (31.9686, 99.9018)
# At last, print the distance between two points calculated in kilo-metre
print ("The distance between New York and Texas is: ", GD(New_York, Texas).km)
Output:
The distance between New York and Texas is: 2507.14797665193
Method 2: By using Great Circle Distance
The great circle distance represents the shortest route connecting two locations on the surface of a sphere. For the purpose of this discussion, we will consider the Earth to be an ideal sphere. The example below demonstrates how a user can compute the great circle distance utilizing the longitude and latitude coordinates of two distinct points.
Example:
# First, import the great_circle module from the geopy library
from geopy.distance import great_circle as GC
# Then, load the latitude and longitude data for New York & Texas
New_York = (40.7128, 74.0060)
Texas = (31.9686, 99.9018)
# At last, print the distance between two points calculated in kilo-metre
print ("The distance between New York and Texas is: ", GC(New_York, Texas).km)
Output:
The distance between New York and Texas is: 2503.045970189156
Method 3: By using Haversine Formula
The orthodromic distance is utilized to determine the minimal distance between two points specified by their latitude and longitude coordinates on the surface of the Earth.
By employing this approach, the individual must possess the coordinates for two distinct points, labeled as P and Q.
Initially, it is necessary to transform the latitude and longitude coordinates from decimal degrees into radians, which involves dividing these values by (180/π). The user should apply the approximation of "π = 22/7". Consequently, the calculation of (180/π) yields "57.29577". For those interested in computing the distance in miles, the Earth's radius can be taken as "3,963". Conversely, if the distance is to be determined in kilometers, the appropriate value is "6,378.80".
Formulas:
How to calculate the value of latitude in radians:
The value of Latitude in Radian: Latitude (La1) = La1 / (180/?)
OR
The value of Latitude in Radian: Latitude (La1) = La1 / 57.29577
How to calculate the value of longitude in radians:
The value of Longitude in Radian: Longitude (Lo1) = Lo1 / (180/?)
OR
The value of Longitude in Radian: Longitude (Lo1) = Lo1 / 57.29577
The user is required to obtain the coordinates for point P and point Q, expressed in terms of longitude and latitude. Following this, the previously mentioned formula should be applied to convert these coordinates into radians.
Next, determine the distance separating two points by applying the formula below.
Formula:
For miles:
Distance (D) = 3963.0 * arccos[(sin(La1) * sin(La2)) + cos(La1) * cos(La2) * cos(Lo2 - Lo1)]
For kilometre:
Distance (D) = 3963.0 * arccos[(sin(La1) * sin(La2)) + cos(La1) * cos(La2) * cos(Lo2 - Lo1)]
As a result, an individual can determine the minimum distance between two specified locations on the Earth's surface by employing the Haversine Formula.
Example:
from math import radians, cos, sin, asin, sqrt
# For calculating the distance in Kilometres
def distance_1(La1, La2, Lo1, Lo2):
# The math module contains the function name "radians" which is used for converting the degrees value into radians.
Lo1 = radians(Lo1)
Lo2 = radians(Lo2)
La1 = radians(La1)
La2 = radians(La2)
# Using the "Haversine formula"
D_Lo = Lo2 - Lo1
D_La = La2 - La1
P = sin(D_La / 2)**2 + cos(La1) * cos(La2) * sin(D_Lo / 2)**2
Q = 2 * asin(sqrt(P))
# The radius of earth in kilometres.
R_km = 6371
# Then, we will calculate the result
return(Q * R_km)
# driver code
La1 = 40.7128
La2 = 31.9686
Lo1 = -74.0060
Lo2 = -99.9018
print ("The distance between New York and Texas is: ", distance_1(La1, La2, Lo1, Lo2), "K.M")
# For calculating the distance in Miles
def distance_2(La1, La2, Lo1, Lo2):
# The math module contains the function name "radians" which is used for converting the degrees value into radians.
Lo1 = radians(Lo1)
Lo2 = radians(Lo2)
La1 = radians(La1)
La2 = radians(La2)
# Using the "Haversine formula"
D_Lo = Lo2 - Lo1
D_La = La2 - La1
P = sin(D_La / 2)**2 + cos(La1) * cos(La2) * sin(D_Lo / 2)**2
Q = 2 * asin(sqrt(P))
# The radius of earth in Miles.
R_Mi = 3963
# Then, we will calculate the result
return(Q * R_Mi)
print ("The distance between New York and Texas is: ", distance_2(La1, La2, Lo1, Lo2), "Miles")
Output:
The distance between New York and Texas is: 2503.04243426357 K.M
The distance between New York and Texas is: 1556.985899699659 Miles
Conclusion
In this guide, we have explored multiple techniques for determining the distance between two locations on the Earth's surface utilizing the geopy library. We have provided demonstrations for each technique.