Learn Python – How to Calculate Distance between Two Points using GEOPY.- Basic and advance

The geopy is a Python library which helps to calculate geographical distance. In this tutorial, we will discuss exceptional methods of how the consumer can calculate the distance between two locations on the earth.

First, the consumer has to install the geopy through the use of the following command:

pip install geopy  

After profitable installation, we are geared up to work with the geopy library.

Calculate Distance between Two Points

Below are the vital techniques that used to calculate the distance between two points.

Method 1: By using Geodesic Distance

The geodesic distance is the length of the shortest direction between two factors on any surface of Earth. In the following example, we will show how the consumer can calculate the Geodesic Distance from the latitude and longitude data.

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 extraordinary circle distance is the shortest route between two points on the sphere. In this case, we will assume the earth is the ideal sphere. The following instance shows how the person can calculate wonderful circle distance through using longitude and latitude data of two 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 used for calculating the shortest distance between two latitudes and longitudes factors on the earth’s surface.

Using this method, the person desires to have the coordinates of two factors (P and Q).

First, they have to convert the values of latitude and longitude factors from decimal levels to radians and then divide the values of latitude and longitude through (180/π). The user ought to use the fee of “π = 22/7”. Then, the cost of (180/π) will be “57.29577”. If the consumer wishes to calculate the distance in miles, they can use the fee of the radius of Earth, that is, “3,963”. And if the user wants to calculate the distance in Kilo-metre, they can use the price “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 needs the coordinates of P factor and Q points in phrases of longitude and latitude, then the usage of the above formulation for converting them into radians.

Now, calculate the distance between two points through the use of the following formula.

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)]  

Thus, the user can calculate the shortest distance between the two given points on Earth through the usage of 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 tutorial, we have discussed a range of techniques for calculating the distance between two points on the earth’s surface through the usage of the geopy library. We have shown examples of every method.