-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreatCircle.py
40 lines (33 loc) · 1.17 KB
/
GreatCircle.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
'''
'''
import math
EARTH_CIRCUMFERENCE = 6378137 # earth circumference in meters
def great_circle_distance(latlong_a, latlong_b):
"""
>>> coord_pairs = [
... # between eighth and 31st and eighth and 30th
... [(40.750307,-73.994819), (40.749641,-73.99527)],
... # sanfran to NYC ~2568 miles
... [(37.784750,-122.421180), (40.714585,-74.007202)],
... # about 10 feet apart
... [(40.714732,-74.008091), (40.714753,-74.008074)],
... # inches apart
... [(40.754850,-73.975560), (40.754851,-73.975561)],
... ]
>>> for pair in coord_pairs:
... great_circle_distance(pair[0], pair[1]) # doctest: +ELLIPSIS
83.325362855055...
4133342.6554530...
2.7426970360283...
0.1396525521278...
"""
lat1, lon1 = latlong_a
lat2, lon2 = latlong_b
dLat = math.radians(lat2 - lat1)
dLon = math.radians(lon2 - lon1)
a = (math.sin(dLat / 2) * math.sin(dLat / 2) +
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
math.sin(dLon / 2) * math.sin(dLon / 2))
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = EARTH_CIRCUMFERENCE * c
return d