Calculate the distance between two GPS-Coordinates
September 17th, 2007 by exhuma.twn
This function uses the Haversine formula to calculate the distance which takes into account the spherical nature of the earth.
As the earth is not a perfect sphere, this function approximates this by using the average radius.
from math import sin, cos, radians, sqrt, asin
def lldistance(a, b):
"""
Calculates the distance between two GPS points (decimal)
@param a: 2-tuple of point A
@param b: 2-tuple of point B
@return: distance in m
"""
r = 6367442.5 # average earth radius in m
dLat = radians(a[0]-b[0])
dLon = radians(a[1]-b[1])
x = sin(dLat/2) ** 2 + \
cos(radians(a[0])) * cos(radians(b[0])) *\
sin(dLon/2) ** 2
y = 2 * asin(sqrt(x))
d = r * y
return d
def lldistance(a, b):
"""
Calculates the distance between two GPS points (decimal)
@param a: 2-tuple of point A
@param b: 2-tuple of point B
@return: distance in m
"""
r = 6367442.5 # average earth radius in m
dLat = radians(a[0]-b[0])
dLon = radians(a[1]-b[1])
x = sin(dLat/2) ** 2 + \
cos(radians(a[0])) * cos(radians(b[0])) *\
sin(dLon/2) ** 2
y = 2 * asin(sqrt(x))
d = r * y
return d
Posted in Python | 1 Comment »