Browse Source

Merge pull request #27 from willnoonan/multi

Improvements to defAirport.py
pull/8/merge
Jack Sweeney 3 years ago committed by GitHub
parent
commit
c6aaff10df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .gitignore
  2. 12
      defAirport.py

2
.gitignore vendored

@ -1,3 +1,5 @@
# PyCharm
.idea
.vscode/settings.json
pythonenv3.8/
__pycache__

12
defAirport.py

@ -1,5 +1,7 @@
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:
@ -8,19 +10,19 @@ def add_airport_region(airport_dict):
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)
closest_airport_dict = dict()
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:
if not closest_airport_dict or airport_dist < closest_airport_dist:
closest_airport_dict = airport
closest_airport_dist = airport_dist
closest_airport_dict['distance_mi'] = closest_airport_dist
@ -28,6 +30,8 @@ def getClosestAirport(latitude, longitude, allowed_types):
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:
airport_csv_reader = csv.DictReader(filter(lambda row: row[0] != '#', airport_csv))

Loading…
Cancel
Save