-
Notifications
You must be signed in to change notification settings - Fork 0
/
haversine.py
35 lines (27 loc) · 945 Bytes
/
haversine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Using Python 3.6."""
from math import atan2, cos, radians, sin, sqrt
def haversine(
initial_lat: float,
initial_long: float,
destination_lat: float,
destination_long: float,
) -> float:
"""Return the delta betwen to points in km.
It returns the delta betwen two points on earth using haversine formula.
"""
EARTH_RADIUS_KM = 6371
# Convert all the coordinates to radians
initial_lat, initial_long, destination_lat, destination_long = map(
radians, [initial_lat, initial_long, destination_lat, destination_long]
)
delta_lat = destination_lat - initial_lat
delta_long = destination_long - initial_long
a = (
sin(delta_lat / 2) ** 2
+ cos(initial_lat) * cos(destination_lat) * sin(delta_long / 2) ** 2
)
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = round(EARTH_RADIUS_KM * c, ndigits=2)
return distance
if __name__ == "__main__":
pass