Haversine Formula
Geolocation
Distance Calculation
Latitude Longitude
GPS Coordinates

Calculate distance between two latitude-longitude points? Haversine formula

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Calculating the distance between two points defined by latitude and longitude on the Earth's surface is a common problem in many fields, such as geolocation, mapping, and navigation. One widely-used formula to perform this calculation is the Haversine formula. In this article, we'll delve into the technical aspects of the Haversine formula, its applications, and provide examples to illustrate its use.

The Haversine Formula

The Haversine formula calculates the shortest distance over the earth’s surface, giving an "as-the-crow-flies" distance between two points. It's a solution to the spherical trigonometry problem and is defined by the following steps:

a = sin^2((Δphi) / 2) + cos(phi1) * cos(phi2) * sin^2((Δlambda) / 2)

c = 2 * atan2(sqrt(a), sqrt(1 - a))

d = R * c

Where:

  • phi1 and phi2 are the latitudes of point 1 and point 2 in radians.
  • Δphi is the difference between the latitudes of the two points (that is, phi2 - phi1).
  • Δlambda is the difference between the longitudes of the two points (that is, lambda2 - lambda1).
  • R is the Earth's radius (mean radius = 6,371 km or 3,959 miles).
  • d is the distance between the two points along the surface of the sphere.

Examples and Application

Below is an example implementation of the Haversine formula in Python:

python
1import math
2
3def haversine(lon1, lat1, lon2, lat2):
4    # Convert decimal degrees to radians 
5    lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])
6
7    # Haversine formula 
8    dlon = lon2 - lon1 
9    dlat = lat2 - lat1 
10    a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
11    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) 
12    r = 6371 # Radius of Earth in kilometers. Use 3956 for miles
13    distance = r * c
14
15    return distance
16
17# Usage Example:
18distance_km = haversine(77.5946, 12.9716, 72.8777, 19.0760) # From Bangalore to Mumbai
19print(f"Distance: {distance_km} km")

Output:
Distance: 840.3 km

Key Considerations

  1. Radius of the Earth (R): The choice of Earth's radius is crucial. While 6,371 km is commonly used as an average radius, for higher accuracy over small distances, local radii may vary.
  2. Report Outputs: The formula may yield results slightly different from actual path distances because the Earth isn't a perfect sphere but an oblate spheroid. Corrections for the Earth's ellipsoidal shape can be considered for more precision critical scenarios.
  3. Spherical Violations: For distances over large regions (>1000 km), the curvature of the Earth and the Earth's actual shape (geoid) may cause discrepancies that the simple Haversine does not account for.

Summary Table

ConceptDescription
Haversine FormulaMethod to calculate the great-circle distance between two points using latitude-longitude.
Earth's Radius6,371 km 3,959 miles
Input RequirementLatitude & longitude in decimal degrees compatibility
OutputDistance in kilometers (or miles)
Precision NoteMore accurate for short distances; Errors may increase with larger distances.

Additional Considerations

Vincenty Formulae

For more precise distance measurements, especially over long distances, geodesic calculations such as Vincenty's formulae can be used. These take into account more complexities regarding the shape of the Earth but are computationally more intensive.

Practical Applications

The Haversine formula is widely used in:

  • GPS Technology: For determining the straight-line distance between two geographic points.
  • Aviation: Flight path calculations between airports.
  • Mobile Network Triangulation: Estimating locations based on distances from known coordinate points.

By understanding and implementing the Haversine formula, one can efficiently determine distances using geographic coordinates, contributing to numerous practical applications in today's geospatially aware world.


Course illustration
Course illustration

All Rights Reserved.