-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinder.py
68 lines (50 loc) · 2.05 KB
/
finder.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json
import re
import sys
from math import sin, cos, sqrt, atan2, radians
def main():
LAT_ORIGIN = radians(39.103119) # YOUR LOCATION LATITUDE IN ( )
LON_ORIGIN = radians(-84.512016) # YOUR LOCATION LONGITUDE IN ( )
radius_of_earth = 6378.0
results = []
with open("list.txt") as airports:
with open('airports.json') as json_file:
data = json.load(json_file)
for line in airports:
if line.strip():
regex = r"\((.*)\)"
matches = re.search(regex, line)
if matches:
DEST = "K" + matches.group(1)
#for airport in data:
airport = data[DEST]
#if DEST == airport:
lat2 = radians(airport["lat"])
lon2 = radians(airport["lon"])
dlon = lon2 - LON_ORIGIN
dlat = lat2 - LAT_ORIGIN
a = sin(dlat / 2)**2 + cos(LAT_ORIGIN) * \
cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
if (len(sys.argv) > 1):
if (sys.argv[1] == "-km"):
distance = radius_of_earth * c
else:
distance = radius_of_earth * c * .621371
else:
distance = radius_of_earth * c * .621371
result = {
"name": airport["name"],
"distance": distance
}
results.append(result)
results = [dict(t) for t in {tuple(d.items()) for d in results}]
results = sorted(results, key=lambda k: k['distance'])
for result in results:
print(result)
if __name__ == "__main__":
import time
start = time.time()
main()
end = time.time()
print(end-start)