Jack Sweeney
3 years ago
committed by
GitHub
2 changed files with 43 additions and 37 deletions
@ -1,6 +1,8 @@ |
|||||||
|
# PyCharm |
||||||
|
.idea |
||||||
.vscode/settings.json |
.vscode/settings.json |
||||||
pythonenv3.8/ |
pythonenv3.8/ |
||||||
__pycache__ |
__pycache__ |
||||||
dependencies |
dependencies |
||||||
testing |
testing |
||||||
lookup_route.py |
lookup_route.py |
||||||
|
@ -1,41 +1,45 @@ |
|||||||
import csv |
import csv |
||||||
import math |
import math |
||||||
|
|
||||||
|
|
||||||
def add_airport_region(airport_dict): |
def add_airport_region(airport_dict): |
||||||
#Get full region/state name from iso region name |
# Get full region/state name from iso region name |
||||||
with open('./dependencies/regions.csv', 'r', encoding='utf-8') as regions_csv: |
with open('./dependencies/regions.csv', 'r', encoding='utf-8') as regions_csv: |
||||||
regions_csv = csv.DictReader(filter(lambda row: row[0]!='#', regions_csv)) |
regions_csv = csv.DictReader(filter(lambda row: row[0] != '#', regions_csv)) |
||||||
for region in regions_csv: |
for region in regions_csv: |
||||||
if region['code'] == airport_dict['iso_region']: |
if region['code'] == airport_dict['iso_region']: |
||||||
airport_dict['region'] = region['name'] |
airport_dict['region'] = region['name'] |
||||||
return airport_dict |
return airport_dict |
||||||
|
|
||||||
|
|
||||||
def getClosestAirport(latitude, longitude, allowed_types): |
def getClosestAirport(latitude, longitude, allowed_types): |
||||||
from geopy.distance import geodesic |
from geopy.distance import geodesic |
||||||
plane = (latitude, longitude) |
plane = (latitude, longitude) |
||||||
with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv: |
closest_airport_dict = dict() |
||||||
airport_csv_reader = csv.DictReader(filter(lambda row: row[0]!='#', airport_csv)) |
with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv: |
||||||
for airport in airport_csv_reader: |
airport_csv_reader = csv.DictReader(filter(lambda row: row[0] != '#', airport_csv)) |
||||||
if airport['type'] in allowed_types: |
for airport in airport_csv_reader: |
||||||
airport_coord = float(airport['latitude_deg']), float(airport['longitude_deg']) |
if airport['type'] in allowed_types: |
||||||
airport_dist = float((geodesic(plane, airport_coord).mi)) |
airport_coord = float(airport['latitude_deg']), float(airport['longitude_deg']) |
||||||
if "closest_airport_dict" not in locals(): |
airport_dist = float((geodesic(plane, airport_coord).mi)) |
||||||
closest_airport_dict = airport |
if not closest_airport_dict or airport_dist < closest_airport_dist: |
||||||
closest_airport_dist = airport_dist |
closest_airport_dict = airport |
||||||
elif airport_dist < closest_airport_dist: |
closest_airport_dist = airport_dist |
||||||
closest_airport_dict = airport |
closest_airport_dict['distance_mi'] = closest_airport_dist |
||||||
closest_airport_dist = airport_dist |
# Convert indent key to icao key as its labeled icao in other places not ident |
||||||
closest_airport_dict['distance_mi'] = closest_airport_dist |
closest_airport_dict['icao'] = closest_airport_dict.pop('gps_code') |
||||||
#Convert indent key to icao key as its labeled icao in other places not ident |
closest_airport_dict = add_airport_region(closest_airport_dict) |
||||||
closest_airport_dict['icao'] = closest_airport_dict.pop('gps_code') |
return closest_airport_dict |
||||||
closest_airport_dict = add_airport_region(closest_airport_dict) |
|
||||||
return closest_airport_dict |
|
||||||
def get_airport_by_icao(icao): |
def get_airport_by_icao(icao): |
||||||
with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv: |
with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv: |
||||||
airport_csv_reader = csv.DictReader(filter(lambda row: row[0]!='#', airport_csv)) |
airport_csv_reader = csv.DictReader(filter(lambda row: row[0] != '#', airport_csv)) |
||||||
for airport in airport_csv_reader: |
for airport in airport_csv_reader: |
||||||
if airport['gps_code'] == icao: |
if airport['gps_code'] == icao: |
||||||
matching_airport = airport |
matching_airport = airport |
||||||
#Convert indent key to icao key as its labeled icao in other places not ident |
# Convert indent key to icao key as its labeled icao in other places not ident |
||||||
matching_airport['icao'] = matching_airport.pop('gps_code') |
matching_airport['icao'] = matching_airport.pop('gps_code') |
||||||
break |
break |
||||||
matching_airport = add_airport_region(matching_airport) |
matching_airport = add_airport_region(matching_airport) |
||||||
return matching_airport |
return matching_airport |
||||||
|
Loading…
Reference in new issue