Haversine Distance: Calculating Distance Between Geographic Points
June 27, 2023 • ☕️ 4 min read • 🏷 computer, software, algorithm
Translated by author into: English
Geographical computing is an area that has gained great momentum with the development of modern technology and data processing methods, and where geographic data and location-based services have an increasing importance. Geographic data plays a critical role in many industries today; Geolocation information is used not only in mapping applications, but also in air transportation, journey planning, natural disaster forecasting, environmental monitoring, healthcare, geographic information systems (GIS) and many more.
When working with this geographic data, the need to calculate the distance between two points arises. But when we consider that the Earth actually has a round shape, this calculation becomes more complex. This is where the Haversine distance comes into play. Haversine distance is a mathematical method that calculates the direct distance between two geographic locations, taking into account the surface curvature. This method allows us to more precisely determine the distance between two points, taking into account the curvature and roundness of the Earth’s surface. Therefore, it is considered an essential tool in geospatial computing and location-based services.
The Importance of Haversine Distance
Haversine distance is important when used in geolocation-based applications and alignment operations. Here are some reasons why the Haversine distance is important:
- Real World Applications: Haversine distance is used when calculating real world distances in geolocation based applications. Such applications are used in location-based services, travel planning, air transport, maritime, geodesy, and more.
- Accuracy and Precision: The Haversine distance is calculated taking into account that the Earth has a round shape. This provides an accurate and precise calculation of the distance between two points. Unlike simple straight line calculations, it takes into account the curvature of the Earth’s surface.
- Map Alignment: Map alignment is the process of placing geographic data on a map. The Haversine distance helps align these data accurately on the plane.
How is the Haversine Distance Calculated?
To calculate the Haversine distance, you follow these steps:
- Get the latitude and longitude coordinates of two points in degrees.
-
Convert the given coordinates in degrees to radians. This is done using the following formulas to convert latitude and longitude to radian units:
- Radians of Latitude = Degrees of Latitude x (π / 180)
- Longitude Radians = Longitude Degrees x (π / 180)
-
Calculate the distance between two points using the Haversine formula:
- Δlat = Lat2 - Lat1
- Δlon = Longitude2 - Longitude1
- a = sin²(Δlat/2) + cos(Latitude1) * cos(Latitude2) * sin²(Δlon/2)
- c = 2 * alan2(√a, √(1-a))
- Distance = R * c
- Here, R is the Earth’s radius (approximately 6.371 km on average) and Atan2 is the arctangent operation.
- The calculated distance refers to the shortest distance on the surface between two points.
The Haversine distance is an important mathematical method used in geolocation-based applications and map alignment. This method helps us to accurately calculate the actual distance between two points, taking into account that the Earth has a round shape. Therefore, it is an essential tool in the field of geodata processing and location-based services.
Implementation of Haversine Distance Algorithm in GoLang:
package main
import (
"fmt"
"math"
)
const earthRadius = 6371 // Average radius of the Earth (in kilometers)
func haversine(lat1, lon1, lat2, lon2 float64) float64 {
// Convert degrees to radians
lat1 = lat1 * math.Pi / 180
lon1 = lon1 * math.Pi / 180
lat2 = lat2 * math.Pi / 180
lon2 = lon2 * math.Pi / 180
// Apply the Haversine formula
dlat := lat2 - lat1
dlon := lon2 - lon1
a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Cos(lat1)*math.Cos(lat2)*math.Sin(dlon/2)*math.Sin(dlon/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
distance := earthRadius * c
return distance
}
func main() {
// Calculate the distance between two points
lat1 := 40.7128 // Latitude of New York
lon1 := -74.0060 // Longitude of New York
lat2 := 34.0522 // Latitude of Los Angeles
lon2 := -118.2437 // Longitude of Los Angeles
result := haversine(lat1, lon1, lat2, lon2)
fmt.Printf("Distance between New York and Los Angeles: %.2f km\n", result)
}
When the program is run, the output will be as follows.
Distance between New York and Los Angeles: 3935.75 km
The running version of the program can be accessed from here.
Resources
- https://en.wikipedia.org/wiki/Haversine_formula
- https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.haversine_distances.html
- https://www.vcalc.com/wiki/vcalc/haversine-distance