Skip to content

Instantly share code, notes, and snippets.

@signed0
Created February 2, 2012 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save signed0/1726034 to your computer and use it in GitHub Desktop.
Save signed0/1726034 to your computer and use it in GitHub Desktop.
Geographic Linestring Length
from math import sin, cos, sqrt, asin, radians
RADIUS_EARTH_M = 6371200.0 #The Earth's mean radius in meters
def geographic_length(coords):
'''Returns the length of a linestring in meters'''
coords = ((radians(x), radians(y)) for x, y in coords)
rad_dist = sum(_haversine_distance(*c) for c in pairs(coords))
return rad_dist * RADIUS_EARTH_M * 2.0
def _haversine_distance(a, b):
'''Assumes that a and b are radians and does not multiply by the diameter
of the earth'''
lng1, lat1 = a
lng2, lat2 = b
sin_dlat_over_2 = sin((lat2 - lat1) / 2.0)
sin_dlng_over_2 = sin((lng2 - lng1) / 2.0)
f = sin_dlat_over_2 * sin_dlat_over_2
f += cos(lat1) * cos(lat2) * sin_dlng_over_2 * sin_dlng_over_2
return asin(sqrt(f))
def haversine_distance(a, b):
'''Operates on lat,lngs and returns the distance in meters'''
a = map(radians, a) #lng, lat to radians
b = map(radians, b) #lng, lat to radians
return RADIUS_EARTH_M * 2.0 * _haversine_distance(a, b)
def pairs(iterable):
'''Generate pairs of items in iterable'''
i = iter(iterable)
prev = i.next()
for j in i:
yield prev, j
prev = j
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment