DEV Community

cycy
cycy

Posted on

Accurate Distance Calculations in Python: Why geopy.geodesic Stands Out

When working with geographic data in Python, calculating the distance between two points on Earth is a common task. But not all methods are created equal!

Here’s a quick guide to the most popular approaches and why geopy.geodesic is often the best choice — especially if you're working on Django, GIS, or logistics platforms.


🚫 1. The Pitfall: Euclidean Distance

Some libraries (or naive code) use the Pythagorean theorem to compute distance between lat/lng pairs:

# ❌ Not recommended for geographic coordinates!
from math import sqrt

def euclidean_distance(coord1, coord2):
    return sqrt((coord1[0] - coord2[0])**2 + (coord1[1] - coord2[1])**2)
Enter fullscreen mode Exit fullscreen mode

🔍 Problem:

The Earth is not flat. This method ignores the planet’s curvature, so it becomes wildly inaccurate as distances grow.


🌍 2. The Spherical Law of Cosines & Haversine Formula

These formulas treat the Earth as a perfect sphere:

# ✅ Haversine example
from math import radians, sin, cos, sqrt, atan2

def haversine(coord1, coord2):
    R = 6371000  # Earth radius in meters
    lat1, lon1 = map(radians, coord1)
    lat2, lon2 = map(radians, coord2)
    dlat = lat2 - lat1
    dlon = lon2 - lon1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    return R * c
Enter fullscreen mode Exit fullscreen mode

✅ Pros:

  • Fast and easy to implement
  • Useful for short distances or internal tools

⚠️ Cons:

  • Assumes a spherical Earth — slight errors (~0.5%) creep in, especially on longer distances

🥇 3. The Gold Standard: geopy.geodesic

from geopy.distance import geodesic

coord1 = (40.748817, -73.985428)  # NYC
coord2 = (34.052235, -118.243683) # LA

distance = geodesic(coord1, coord2).meters
print(f"Distance: {distance:.2f} meters")
Enter fullscreen mode Exit fullscreen mode

🌟 Pros:

  • Uses WGS-84 ellipsoid, same as GPS
  • Very accurate, used in professional mapping tools
  • Simple, human-friendly API

⚠️ Cons:

  • Slightly slower than Haversine, but the difference is usually negligible

🧼 4. Cleaner Code with Point Objects (e.g. in Django)

If you're working with Django GIS or GEOS, you’ll likely deal with Point objects:

from django.contrib.gis.geos import Point
from geopy.distance import geodesic

point1 = Point(-73.985428, 40.748817)  # (lng, lat)
point2 = Point(-118.243683, 34.052235)

# geopy expects (lat, lng)
coord1 = (point1.y, point1.x)
coord2 = (point2.y, point2.x)

distance = geodesic(coord1, coord2).meters
Enter fullscreen mode Exit fullscreen mode

Tip: Always check coordinate order!

Point stores as (longitude, latitude)

geopy.geodesic expects (latitude, longitude)


✅ When Should You Use Each?

Method Earth Model Accuracy Use Case
Euclidean Flat ❌ Poor Never
Haversine/Cosines Sphere ✅ Good Quick estimates
geopy.geodesic WGS-84 Ellipsoid ✅✅✅ Excellent Logistics, GPS apps, GIS
Point + geopy WGS-84 Ellipsoid ✅✅✅ Excellent Django, cleaner code

🚀 TL;DR

  • 🌍 Earth is not flat — ditch Euclidean!
  • ✈️ Use Haversine for fast, rough estimates
  • 🧭 Use geopy.geodesic for production-grade accuracy
  • 🧼 Combine it with Point objects for cleaner, Django-friendly code

💬 Have you used geopy or built a GPS-based feature?

💡 Got a bug story or tip on coordinate quirks? Drop it in the comments!

Top comments (0)