Skip to content

Index

utilities

function_helper

calculate_distance_m(lat1, lon1, lat2, lon2)

Returns distance, in kilometers, between one set of longitude/latitude coordinates and another

Source code in src/common/utilities/function_helper.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def calculate_distance_m(lat1, lon1, lat2, lon2):
    """Returns distance, in kilometers, between one set of longitude/latitude
    coordinates and another"""
    lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])

    newlon = lon2 - lon1
    newlat = lat2 - lat1

    haver_formula = (
        np.sin(newlat / 2.0) ** 2
        + np.cos(lat1) * np.cos(lat2) * np.sin(newlon / 2.0) ** 2
    )

    dist = 2 * np.arcsin(np.sqrt(haver_formula))
    m = (6367 * dist) * 1000  # 6367 for distance in m for miles use 3958
    return m