forked from Jxck-S/plane-notify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
defAirport.py
45 lines (45 loc) · 2.04 KB
/
defAirport.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
import csv
import math
def add_airport_region(airport_dict):
#Get full region/state name from iso region name
with open('./dependencies/regions.csv', 'r', encoding='utf-8') as regions_csv:
regions_csv = csv.DictReader(filter(lambda row: row[0]!='#', regions_csv))
for region in regions_csv:
if region['code'] == airport_dict['iso_region']:
airport_dict['region'] = region['name']
return airport_dict
def getClosestAirport(latitude, longitude, allowed_types):
from geopy.distance import geodesic
plane = (latitude, longitude)
with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv:
airport_csv_reader = csv.DictReader(filter(lambda row: row[0]!='#', airport_csv))
for airport in airport_csv_reader:
if airport['type'] in allowed_types:
airport_coord = float(airport['latitude_deg']), float(airport['longitude_deg'])
airport_dist = float((geodesic(plane, airport_coord).mi))
if "closest_airport_dict" not in locals():
closest_airport_dict = airport
closest_airport_dist = airport_dist
elif airport_dist < closest_airport_dist:
closest_airport_dict = airport
closest_airport_dist = airport_dist
closest_airport_dict['distance_mi'] = closest_airport_dist
#Convert indent key to icao key as its labeled icao in other places not ident
closest_airport_dict['icao'] = closest_airport_dict.pop('gps_code')
closest_airport_dict = add_airport_region(closest_airport_dict)
return closest_airport_dict
def get_airport_by_icao(icao):
with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv:
matching_airport = None
airport_csv_reader = csv.DictReader(filter(lambda row: row[0]!='#', airport_csv))
for airport in airport_csv_reader:
if airport['gps_code'] == icao:
matching_airport = airport
#Convert indent key to icao key as its labeled icao in other places not ident
matching_airport['icao'] = matching_airport.pop('gps_code')
break
if matching_airport:
matching_airport = add_airport_region(matching_airport)
return matching_airport
else:
return None