import math


def latlon_distance(lon1, lat1, lon2, lat2, R=6372.8):
    """calculate the distance between two points (lon1, lat1) and (lon2, lat2)
    defined in latitude longitude coordinates (epsg 4326).
    :param R:   radius earth in local unit (R = 6373 for km)
    https://gist.github.com/rochacbruno/2883505"""

    degrees_to_radians = math.pi / 180.0

    phi1 = (90.0 - lat1) * degrees_to_radians
    phi2 = (90.0 - lat2) * degrees_to_radians

    theta1 = lon1 * degrees_to_radians
    theta2 = lon2 * degrees_to_radians

    cos = (math.sin(phi1) * math.sin(phi2) * math.cos(theta1 - theta2) + math.cos(phi1) * math.cos(
        phi2))  # multiply arc by the radius of the earth in local units to get length.

    return math.acos(cos) * R