Skip to content

Instantly share code, notes, and snippets.

@gabesmed
Created February 14, 2012 11:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gabesmed/1826175 to your computer and use it in GitHub Desktop.
Save gabesmed/1826175 to your computer and use it in GitHub Desktop.
great circle distance in python
"""Distance helpers."""
import math
EARTH_CIRCUMFERENCE = 6378137 # earth circumference in meters
def great_circle_distance(latlong_a, latlong_b):
"""
>>> coord_pairs = [
... # between eighth and 31st and eighth and 30th
... [(40.750307,-73.994819), (40.749641,-73.99527)],
... # sanfran to NYC ~2568 miles
... [(37.784750,-122.421180), (40.714585,-74.007202)],
... # about 10 feet apart
... [(40.714732,-74.008091), (40.714753,-74.008074)],
... # inches apart
... [(40.754850,-73.975560), (40.754851,-73.975561)],
... ]
>>> for pair in coord_pairs:
... great_circle_distance(pair[0], pair[1]) # doctest: +ELLIPSIS
83.325362855055...
4133342.6554530...
2.7426970360283...
0.1396525521278...
"""
lat1, lon1 = latlong_a
lat2, lon2 = latlong_b
dLat = math.radians(lat2 - lat1)
dLon = math.radians(lon2 - lon1)
a = (math.sin(dLat / 2) * math.sin(dLat / 2) +
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
math.sin(dLon / 2) * math.sin(dLon / 2))
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = EARTH_CIRCUMFERENCE * c
return d
@kannes
Copy link

kannes commented Apr 12, 2017

joeatbayes, that is to be expected. There are different formulas based on different earth models with different accuracies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment