Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    Thanks to a answer on stackoverflow by Michael Dunn.
    http://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points

def haversine(lat1, lon1, lat2, lon2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    Thanks to a answer on stackoverflow by Michael Dunn.
    http://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points
    """
    if abs(lat1) > 90 or abs(lat2) > 90:
        raise InvalidCoordinate('invalid latitude')
    if abs(lon1) > 180 or abs(lon2) > 180:
        raise InvalidCoordinate('invalid longitude')
    # convert decimal degrees to radians
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * asin(sqrt(a))
    km = 6371 * c
    return km