**If you are interested to learn about the Python Multiprocessing **

The **geopy** is a Python library which helps to calculate geographical distance. It makes it easier for developers to retrieve coordinates of various locations using third-party geocoders, as well as other data sources. In this tutorial, we will discuss different methods of how the user can calculate the distance between two places on the earth. First, the user has to install the **geopy** by using the following command:

pip install geopy

After successful installation, we are ready to work with the geopy library. We can calculate or find the distance between two positions using the following methods in Geopy:

- Geodesic measure.
- The great circle distance.
- Herversine formula.

### Geodesic measure

Geodesic measure is used to determine the shortest path between any two points on the globe. However, it’s not exactly the same as *the shortest curves* between any two given locations, despite their likeness. We’ll illustrate how to calculate the Geodesic distance using latitude and longitude data in the following example.

Example 1:

# Import the geodesic module from geopy library from geopy.distance import geodesic as GD # For the specified locations, load their latitude and longitude data. Abuja =(9.072264 , 7.491302) Dakar =(14.716677 , -17.467686) #Finally, print the distance between the two sites in kilometers. print("The distance between Abuja and Dakar is: ", GD(Abuja,Dakar).km)

Output:

The distance between Abuja and Dakar is: 2787.8012928541466

Example 2:

# Geopy distance module is first imported for computation from geopy.distance import geodesic as GD # Next, input the latitude and longitude data for Nairobi and Cairo. Nairobi=(36.817223,-1.286389 ) Cairo=( 31.233334,30.033333, ) # Finally, print the distance between the two locations in kilometers. print("The distance between Nairobi and Cairo is :",GD(Nairobi,Cairo).km)

Output:

The distance between Nairobi and Cairo is: 2944.261368793268

### The great circle distance formula

The great circle is regarded as the shortest path between any two places or points on the sphere or the earth’s surface. In this example, it is assumed that the globe is a perfect sphere. The following example shows how to compute great circle distance using longitude and latitude data from two locations. Some of the problems with great-circle navigation include the computation of *azimuths* at endpoints and intermediate waypoints. A great circle is formed by any two points on a sphere that are not directly opposite to each other. The great circle is divided into two arcs by these two points. The shorter arc that is between any two locations equals the great-circle distance.

Example 3:

# First, import the geopy library's great circle module. from geopy.distance import great_circle as GRC # Abuja and Dakar latitude and longitude data. Abuja=(9.072264 , 7.491302) Dakar=(14.716677 , -17.467686) # Finally print the distance between the two points in km print("The distance between Abuja and Dakar is:", GRC(Abuja,Dakar).km)

Output:

The distance between Abuja and Dakar is: 2785.186971064666

### The Haversine formula for distance calculation

The Haversine formula calculates the great-circle distance between any two locations on a sphere using their longitudes and latitudes. The Haversine method gives an accurate way of determining the distance between any specified longitude and latitude. It also serves as a realignment of the spherical law of cosines. However, it’s more useful for tiny angles and distances. The user must have the coordinates of two points (X and Y) to utilize this method. They must convert degrees of latitude and longitude to radians using the `180/π`

formula.

### Formulas

To convert longitudes and latitudes to radians, we use the following formula:

In radians, the latitude’s value is:

`Latitude (LaA) = LaA / (180/π ) or Latitude (LaA) = LaA / 57.29577.`

The longitude’s value will be:

`Longitude (LoA) = LoA / (180/π ) or Longitude (LoA) = LoA / 57.29577.`

We use the formula below to calculate the distance in miles:

`Distance (D) = 3963.0 * arccos[(sin(LaA) * sin(LaB)) + cos(LaA) * cos(LaB) * cos(LoB - LoA)]`

To calculate the distance in kilometers:

`Distance (D) = 3963.0 * arccos[(sin(LaA) * sin(LaB)) + cos(LaA) * cos(LaB) * cos(LoB - LoA)]`

Example 4:

from math import radians, cos, sin, asin, sqrt # Implement the formula below def distance_d(LaA, LaB, LoA, LoB): # The function "radians" is found in the math module, It's also used to convert radians to degrees. LoA = radians(LoA) LoB = radians(LoB) LaA= radians(LaA) LaB = radians(LaB) # The "Haversine formula" is used. D_Lo = LoB - LoA D_La = LaB - LaA P = sin(D_La / 2)**2 + cos(LaA) * cos(LaB) * sin(D_Lo / 2)**2 Q = 2 * asin(sqrt(P)) # The earth's radius in kilometers. R_km = 6371 # Then we'll compute the outcome. return(Q * R km). LaA = 9.072264 LaB = 14.716677 LoA = 7.491302 LoB = -17.467686 print ("The distance between Abuja and Dakar is: ", distance_d(LaA, LaB, LoA, LoB), "K.M")

Output:

The distance between Abuja and Dakar is: 2785.183036572855 K.M

## Calculate Distance between Two Points

Below are the important methods that used to calculate the distance between two points.

### Method 1: By using Geodesic Distance

The geodesic distance is the length of the shortest path between two points on any surface of Earth. In the following example, we will show how the user 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 great circle distance is the shortest path between two points on the sphere. In this case, we will assume the earth is the perfect sphere. The following example shows how the user can calculate great circle distance by 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 points on the earth’s surface. Using this method, the user needs to have the coordinates of two points **(P and Q).**

First, they have to convert the values of latitude and longitude points from decimal degrees to radians and then divide the values of latitude and longitude by (180/π). The user should use the value of “π = 22/7”. Then, the value of (180/π) will be “57.29577”. If the user wants to calculate the distance in miles, they can use the value 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 value “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 point and Q points in terms of longitude and latitude, then using the above formula for converting them into radians. Now, calculate the distance between two points by using the following 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 by using 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
```