Calculate GPS distance using Haversine Formula in Python

In this tutorial, we will be looking into how to calculate the GPS distance using the Haversine Formula in Python.

What is Haversine?

Haversine is a formula, which is used to calculate the shortest distance between any two points on a sphere using its latitude and longitude. It is a very accurate way to compute the shortest distance.

Now considering Earth as a sphere, the places on Earth will be considered as two points. So, we can use Haversine formula to calculate the distance between 2 places using the latitude and longitude of the places. This is very useful in case of navigation.

So, the haversine formula to calculate the distance, will be:

haversine formula

where ‘r‘ is the radius, Phi_1, \Phi_2 are the latitudes and\lambda_1 ,\lambda_2are the longitudes.

This looks complex write?

You may be thinking about how to implement such huge code in Python.

Don’t worry, we have an inbuilt library using which we can find the distance easily.

Without waiting for long let’s dive into the Python code.

Implementation of Haversine Formula in Python to Calculate GPS distance

I have written the Python code to calculate the distance between any two GPS points using the Haversine distance formula.

First, you need to install the ‘Haversine library’, which is readily available.

pip install haversine

Then, we will import the haversine library using the import function of the python.

from haversine import haversine, Unit

Here we are importing Unit also, so that we can mention in which unit we want the GPS distance to be displayed.

The next step is taking the latitude and longitude of the first GPS point from the user using the input function.

print("Let's find the distance between 2 GPS distances.")
loc1 = input("Enter the latitude and longitude of the first point (separated by comma and enter only numbers): ")

Then, we will take the latitude and longitude of the second GPS point from the user.

loc2 = input("Enter the latitude and longitude of the second point (separated by comma): ")

As we get the input in string format, we need to first convert them into number format, in particular float format, i.e., in decimal form. For this, we will first split the user input based on comma and store the first number before comma as latitude and the second number as the longitude of both points separately.

loc1_latitude = float(loc1.split(',')[0])
loc1_longitude = float(loc1.split(',')[1])

loc2_latitude = float(loc2.split(',')[0])
loc2_longitude = float(loc2.split(',')[1])

Now we will combine the latitude and longitude of both points in a single variable. In this way, it will be easy for us to pass it to the haversine formula. For this, we write the following code:

loc1_loc = (loc1_latitude, loc1_longitude)
loc2_loc = (loc2_latitude, loc2_longitude)

Once we have this, we will pass this to the haversine function to get the distance in kilometers, miles, and nautical miles.

 # in kilometers
dist_in_km  = haversine(loc1_loc, loc2_loc)

# in miles
dist_in_mi = haversine(loc1_loc, loc2_loc, unit='mi')

# in nautical miles
dist_in_nau = haversine(loc1_loc, loc2_loc, unit=Unit.NAUTICAL_MILES)

Next, we will print the distance between the two points rounding it to two decimal places.

print(f"So, the distance between {loc1_latitude}° N, {loc1_longitude}° E and {loc2_latitude}° N, {loc2_longitude}° E is: ")
print(f"{round(dist_in_km, 2)} kilometers")
print(f"{round(dist_in_mi, 2)} miles")
print(f"{round(dist_in_nau, 2)} nautical miles")

Output:

Distance between two places GPS python

Also read:

Get Geolocation in Python using GeoPy

Bye! See you next time with new topic. Comment if you need any specific topic.

Leave a Reply

Your email address will not be published. Required fields are marked *