How to Calculate Distance between Two Points using Geopy in Python

Feb 25, 2022


Geopy is a Python library that simplifies the calculation of geographic distances between two points.

It makes it easier for developers to retrieve coordinates of various locations using third-party geocoders, as well as other data sources.

In this article, we’ll look at alternative ways of calculating the distance between any two points.

We will explain how this process is possible using geopy. This is the primary Python library for calculating distance.

There is a series of steps that are followed before installing geopy:

First, open your terminal and make sure it is running as an administrator. Ensure that you are operating from the root by running cd.. command.

You can now run the command below to install geopy on your computer.

pip install geopy

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)


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)


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) 


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.


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


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


In this tutorial, we have discussed how to use the geopy package to determine the distance between two points on the earth’s surface.

You can, therefore, use this knowledge to craft other high-quality applications.

Try Launching a Free Project in CloudFlow Today!


How to Create a Reusable React Form component

How to Create a Reusable React Form component

Prerequisites In this tutorial, one ought to have the following: Basic React and Javascript knowledge. Understanding of npm and how to install from npm Atom or Visual studio code and npm installed on a pc. Goal To create a reusable form which can...

read more
Working with Bash Arrays

Working with Bash Arrays

As a programmer, you may have come across or used the command line when building a tool, or for scripting. Bash is one of the most common command-line interpreters used for writing scripts. You can create variables, run for loops, work with arrays,...

read more