Python Implementation and Common Issues in Calculating Distance Between Two Points Based on Latitude and Longitude

Nov 12, 2025 · Programming · 15 views · 7.8

Keywords: Python | Latitude Longitude Calculation | Haversine Formula | Geographic Distance | Degree Radian Conversion

Abstract: This article provides an in-depth exploration of methods for calculating distances between two points on Earth using Python, with a focus on Haversine formula implementation. By comparing user code with correct implementations, it reveals the critical issue of degree-to-radian conversion and offers complete solutions. The article also introduces professional libraries like geopy and compares the accuracy differences of various computational models, providing comprehensive technical guidance for geospatial calculations.

Problem Background and Core Challenges

In geographic information systems and location-based services, accurately calculating distances between two points is a fundamental and critical requirement. Users often encounter significant discrepancies between calculated results and expected values when using the Haversine formula for latitude-longitude distance calculations. This article analyzes a typical case study to explore the root causes and solutions.

Haversine Formula Principle Analysis

The Haversine formula is a spherical trigonometry-based method for calculating distances on the Earth's surface. Its mathematical expression is:

a = sin²(Δφ/2) + cos(φ1) * cos(φ2) * sin²(Δλ/2)
c = 2 * atan2(√a, √(1-a))
distance = R * c

Where φ represents latitude, λ represents longitude, Δφ and Δλ represent the differences in latitude and longitude respectively, and R is the Earth's radius (typically 6371 km or 6373 km).

Common Error Analysis: Degree to Radian Conversion

The main issue in the user's original code is the lack of degree-to-radian conversion. Python's mathematical trigonometric functions (sin, cos, atan2, etc.) use radians by default, while latitude and longitude coordinates are typically expressed in degrees. Using degree values directly in calculations leads to significant errors.

Incorrect code example:

from math import sin, cos, sqrt, atan2

R = 6373.0
lat1 = 52.2296756  # Degree values
lon1 = 21.0122287
lat2 = 52.406374
lon2 = 16.9251681

# Direct calculation using degrees, incorrect result
dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = R * c  # Incorrect result: 5447.05546147

Correct Implementation Method

The correct implementation requires converting degrees to radians:

from math import sin, cos, sqrt, atan2, radians

# Approximate Earth radius (km)
R = 6373.0

# Coordinate points definition
lat1 = 52.2296756
lon1 = 21.0122287
lat2 = 52.406374
lon2 = 16.9251681

# Degree to radian conversion
lat1_rad = radians(lat1)
lon1_rad = radians(lon1)
lat2_rad = radians(lat2)
lon2_rad = radians(lon2)

# Calculate latitude and longitude differences (radians)
dlon = lon2_rad - lon1_rad
dlat = lat2_rad - lat1_rad

# Haversine formula calculation
a = sin(dlat / 2)**2 + cos(lat1_rad) * cos(lat2_rad) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))

# Final distance
distance = R * c

print("Calculation result: ", distance)  # Correct result: 278.545589351
print("Expected result: ", 278.546, "km")

Professional Library Usage

For production environments, professional geospatial calculation libraries like geopy are recommended:

import geopy.distance

# Define coordinate points
coords_1 = (52.2296756, 21.0122287)
coords_2 = (52.406374, 16.9251681)

# Calculate distance using geodesic method
distance = geopy.distance.geodesic(coords_1, coords_2).km
print("geopy calculation result: ", distance)  # 279.352901604 km

Comparison of Different Computational Models

The Haversine formula assumes Earth is a perfect sphere, while the actual Earth is an ellipsoid. geopy's geodesic method is based on the WGS-84 ellipsoid model, providing higher accuracy:

Alternative Implementation Solutions

Besides standard implementations, there are convenient third-party libraries:

# Using mpu library
import mpu
dist = mpu.haversine_distance((lat1, lon1), (lat2, lon2))

# Custom function implementation
def haversine_distance(origin, destination):
    """
    Calculate Haversine distance between two points
    
    Parameters:
    origin: starting coordinates (latitude, longitude)
    destination: ending coordinates (latitude, longitude)
    
    Returns:
    distance in kilometers
    """
    lat1, lon1 = origin
    lat2, lon2 = destination
    radius = 6371  # Earth radius (km)
    
    dlat = radians(lat2 - lat1)
    dlon = radians(lon2 - lon1)
    
    a = (sin(dlat / 2) * sin(dlat / 2) +
         cos(radians(lat1)) * cos(radians(lat2)) *
         sin(dlon / 2) * sin(dlon / 2))
    c = 2 * atan2(sqrt(a), sqrt(1 - a))
    
    return radius * c

Performance and Accuracy Considerations

When choosing calculation methods, balance accuracy and performance:

Practical Application Recommendations

In real-world projects:

  1. For general applications, Haversine formula provides sufficient accuracy
  2. For high-precision requirements, use professional libraries like geopy
  3. Always pay attention to degree-to-radian conversion
  4. Consider caching optimizations for repeated calculations

Conclusion

Accurate calculation of latitude-longitude distances requires attention to degree-to-radian conversion issues. The Haversine formula provides a good balance, while professional libraries like geopy offer higher precision. Developers should choose appropriate calculation methods based on specific requirements and pay attention to coordinate system conversions during implementation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.