Navigate the World: A Guide to Calculating Distances Between Points on Earth using Python and the…
Tags: navigation, numpy, haversineformula, geolocation
Published: 2023-04-28 14:58:39

---
title: Navigate the World: A Guide to Calculating Distances Between Points on Earth using Python and the…
published: true
date: 2023-04-18 14:55:03 UTC
tags: navigation,numpy,haversineformula,geolocation
canonical_url: 
---

### Navigate the World: A Guide to Calculating Distances Between Points on Earth using Python and the Haversine Formula

**C** alculating distances between points on Earth is an important problem in many fields, including navigation, transportation, and geology. In this blog post, we will show you how to use NumPy, a popular Python library for scientific computing, to calculate distances between pairs of latitude and longitude coordinates on Earth.

### Background

To understand how to calculate distances between points on Earth, we first need to understand some basic concepts in geography and mathematics. The Earth is roughly spherical in shape, and its surface can be divided into a grid of latitude and longitude lines.

Latitude measures the north-south position of a point on the Earth’s surface, with the equator being at 0 degrees latitude, and the poles being at 90 degrees north and south latitude. Longitude measures the east-west position of a point on the Earth’s surface, with the prime meridian being at 0 degrees longitude, and the international date line being at 180 degrees east or west longitude.

To calculate the distance between two points on a sphere, we need to find the shortest distance along the surface of the sphere between the two points. This distance is called the great-circle distance, and it can be calculated using the Haversine formula, which is an approximation based on the law of cosines. The formula is as follows:

```
a = sin²(Δlat/2) + cos(lat1) * cos(lat2) * sin²(Δlon/2)
c = 2 * atan2( √a, √(1−a) )
d = R * c
```

Where:

- lat1 and lat2 are the latitudes of the two points, in radians
- lon1 and lon2 are the longitudes of the two points, in radians
- Δlat and Δlon are the differences between the latitudes and longitudes, respectively
- R is the radius of the Earth (mean radius = 6,371km)
- sin and cos are the trigonometric functions
- √ and atan2 are the square root and the two-argument inverse tangent functions, respectively

### Data representation

To use NumPy to calculate distances between points on Earth, we need to represent the latitude and longitude coordinates as arrays of numbers. We also need to convert the coordinates from degrees to radians, as required by the Haversine formula. Here’s an example of how to represent the coordinates of two points, A and B:

```
import numpy as np

# Define the coordinates of point A and B
lat_a = 48.8566 # Paris, France
lon_a = 2.3522
lat_b = 51.5072 # London, UK
lon_b = -0.1276

# Convert the coordinates to radians
lat_a, lon_a = np.radians([lat_a, lon_a])
lat_b, lon_b = np.radians([lat_b, lon_b])
```

### Calculation of distances

Once we have the latitude and longitude coordinates in radians, we can use NumPy functions to calculate the Haversine formula. Here’s an example of how to calculate the great-circle distance between points A and B:

```
# Calculate the differences in latitude and longitude
dlat = lat_b - lat_a
dlon = lon_b - lon_a
```

After we have calculated the difference in latitude and longitude between the two points, we can apply the Haversine formula to calculate the distance between the two points.

The Haversine formula is a mathematical formula that gives the distance between two points on the surface of a sphere. It takes into account the curvature of the Earth’s surface and provides more accurate results than simply calculating the Euclidean distance between two points.

Here is the implementation of the Haversine formula in Python:

```
# Apply the Haversine formula
a = np.sin(dlat/2)**2 + np.cos(lat_a) * np.cos(lat_b) * np.sin(dlon/2)**2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1-a))
distance = R * c
```

In this formula, a is the square of half the chord length between the two points, c is the angular distance between the two points, and R is the radius of the Earth (in kilometers). The np.arctan2() function returns the angle whose tangent is the quotient of two specified numbers, and distance is the final result.

Finally, we can put all the pieces together and create a function to calculate the distance between two points on Earth:

```
def calculate_distance(lat_a, lon_a, lat_b, lon_b):
    R = 6371 # Radius of the Earth in kilometers
    
    # Convert latitude and longitude to radians
    lat_a, lon_a, lat_b, lon_b = np.radians([lat_a, lon_a, lat_b, lon_b])
    
    # Calculate the difference in latitude and longitude
    dlat = lat_b - lat_a
    dlon = lon_b - lon_a
    
    # Apply the Haversine formula
    a = np.sin(dlat/2)**2 + np.cos(lat_a) * np.cos(lat_b) * np.sin(dlon/2)**2
    c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1-a))
    distance = R * c
    
    return distance
```

We can use this function to calculate the distance between two points on Earth:

```
# Calculate the distance between New York and Los Angeles
distance = calculate_distance(40.7128, -74.0060, 34.0522, -118.2437)
print(distance) # Output: 3935.3281658175647
```

The output shows that the distance between New York and Los Angeles is approximately 3935 kilometers.

### Conclusion

Calculating the distance between two points on Earth is an important task in many applications, such as navigation, geolocation, and surveying. The Haversine formula is a reliable method for calculating the distance between two points on the surface of a sphere, and the NumPy library provides a convenient way to perform the necessary calculations in Python.

### Bonus:

Below is the modularized code which includes additional code that uses Folium to visualize the calculated distances on a map.

### Demo — Code Walkthrough

{% gist https://gist.github.com/byteshiva/0e9c9523986971cca75729ea080b089a %}