forked from doniks/pycom-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
haversine.py
40 lines (33 loc) · 982 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
36
37
38
39
40
# Python 3 program for the
# haversine formula
import math
# Python 3 program for the
# haversine formula
def haversine(lat1, lon1, lat2, lon2):
# distance between latitudes
# and longitudes
dLat = (lat2 - lat1) * math.pi / 180.0
dLon = (lon2 - lon1) * math.pi / 180.0
# convert to radians
lat1 = (lat1) * math.pi / 180.0
lat2 = (lat2) * math.pi / 180.0
# apply formulae
a = (pow(math.sin(dLat / 2), 2) +
pow(math.sin(dLon / 2), 2) *
math.cos(lat1) * math.cos(lat2));
rad = 6371
c = 2 * math.asin(math.sqrt(a))
return rad * c
# Driver code
if __name__ == "__main__":
# Big Ben in London (51.5007° N, 0.1246° W)
lat1 = 51.5007
lon1 = 0.1246
# The Statue of Liberty in New York (40.6892° N, 74.0445° W)
lat2 = 40.6892
lon2 = 74.0445
# is 5574.8 km.
print(haversine(lat1, lon1,lat2, lon2), "km")
# This code is contributed
# by ChitraNayal
# https://www.geeksforgeeks.org/haversine-formula-to-find-distance-between-two-points-on-a-sphere/