New Airport Lookup

-New Database Source
-Airport types per plane in config
This commit is contained in:
Jack Sweeney 2020-11-21 03:58:43 +00:00 committed by GitHub
parent dc4b0e240b
commit a8608f5c54
4 changed files with 59 additions and 39 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ pythonenv3.8/
Roboto-Regular.ttf Roboto-Regular.ttf
airports.dat airports.dat
__pycache__ __pycache__
airports.csv

View File

@ -7,8 +7,12 @@ ICAO = icaohere
#Enter GOOGLESTATICMAP or ADSBX #Enter GOOGLESTATICMAP or ADSBX
OPTION = ADSBX OPTION = ADSBX
#TITLE for Twitter, PB and Discord are Just text added to the front of each message/tweet sent [AIRPORT]
#Requires a list of airport types, this plane could land/takeoff at
#Choices: small_airport, medium_airport, large_airport, heliport, seaplane_base
TYPES = [small_airport, medium_airport, large_airport]
#TITLE for Twitter, PB and Discord are Just text added to the front of each message/tweet sent
[TWITTER] [TWITTER]
ENABLE = FALSE ENABLE = FALSE
TITLE = TITLE =

View File

@ -2,48 +2,63 @@
#https://openflights.org/data.html #https://openflights.org/data.html
def download_airports(): def download_airports():
import os import os
if not os.path.isfile('airports.dat'): if not os.path.isfile('airports.csv'):
print("No airports.dat file, downloading now") print("No airports.csv file, downloading now")
try: try:
import requests import requests
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat' url = 'https://ourairports.com/data/airports.csv'
airports = requests.get(url) airports = requests.get(url)
open('airports.dat', 'wb').write(airports.content) open('airports.csv', 'wb').write(airports.content)
except: except:
raise("Error getting airports.dat or storing") raise("Error getting airports.dat or storing")
else: else:
#Writes current date to airports.dat to show when it was aqquired #Writes current date to airports.dat to show when it was aqquired
import datetime import datetime
date = datetime.datetime.now() date = datetime.datetime.now()
with open('airports.dat', 'a') as airports: with open('airports.csv', 'a') as airports:
airports.write("#" + str(date)) airports.write("#" + str(date))
print("Successfully got airports.dat") print("Successfully got airports.csv")
elif os.path.isfile('airports.dat'): elif os.path.isfile('airports.csv'):
print("Already Have airports.dat, continuing") print("Already Have airports.csv, continuing")
def getClosestAirport(latitude, longitude): #OLD Airport lookup
import json # def getClosestAirport(latitude, longitude):
# import csv
# from geopy.distance import geodesic
# plane = (latitude, longitude)
# header = ["id", "name", "city", "country", "iata", "icao", "lat", "lng", "alt", "tz", "dst", "tz_db", "type", "source"]
# with open('airports.dat', encoding='utf-8') as airport_dat:
# airport_dat_reader = csv.DictReader(filter(lambda row: row[0]!='#', airport_dat), header)
# for airport in airport_dat_reader:
# airport_coord = float(airport['lat']), float(airport['lng'])
# 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'] = closest_airport_dist
# print("Closest Airport:", closest_airport_dict['icao'], closest_airport_dict['name'], closest_airport_dist, "Miles Away")
# return closest_airport_dict
def getClosestAirport(latitude, longitude, allowed_types):
import csv import csv
from geopy.distance import geodesic from geopy.distance import geodesic
plane = (latitude, longitude) plane = (latitude, longitude)
header = ["id", "name", "city", "country", "iata", "icao", "lat", "lng", "alt", "tz", "dst", "tz_db", "type", "source"] with open('airports.csv', 'r') as airport_csv:
airports = [] airport_csv_reader = csv.DictReader(filter(lambda row: row[0]!='#', airport_csv))
first_run = True for airport in airport_csv_reader:
with open('airports.dat', encoding='utf-8') as csvf: if airport['type'] in allowed_types:
reader = csv.DictReader(filter(lambda row: row[0]!='#', csvf), header) airport_coord = float(airport['latitude_deg']), float(airport['longitude_deg'])
#for row in reader:
# airports.append(row)
for row in reader:
airport = row
airport_coord = float(airport['lat']), float(airport['lng'])
airport_dist = float((geodesic(plane, airport_coord).mi)) airport_dist = float((geodesic(plane, airport_coord).mi))
if first_run: if "closest_airport_dict" not in locals():
closest_airport_dict = airport closest_airport_dict = airport
closest_airport_dist = airport_dist closest_airport_dist = airport_dist
first_run = False
elif airport_dist < closest_airport_dist: elif airport_dist < closest_airport_dist:
closest_airport_dict = airport closest_airport_dict = airport
closest_airport_dist = airport_dist closest_airport_dist = airport_dist
closest_airport_dict['distance'] = closest_airport_dist closest_airport_dict['distance'] = 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('ident')
print("Closest Airport:", closest_airport_dict['icao'], closest_airport_dict['name'], closest_airport_dist, "Miles Away") print("Closest Airport:", closest_airport_dict['icao'], closest_airport_dict['name'], closest_airport_dist, "Miles Away")
return closest_airport_dict return closest_airport_dict

View File

@ -223,11 +223,11 @@ class Plane:
if self.landed or self.tookoff: if self.landed or self.tookoff:
if self.trigger_type == "now on ground" or "data acquisition" and self.longitude != None and self.latitude != None: if self.trigger_type == "now on ground" or "data acquisition" and self.longitude != None and self.latitude != None:
self.combined = f"{self.latitude} , {self.longitude}" self.combined = f"{self.latitude} , {self.longitude}"
nearest_airport_dict = getClosestAirport(self.latitude, self.longitude) nearest_airport_dict = getClosestAirport(self.latitude, self.longitude, self.config.get("AIRPORT", "TYPES"))
self.has_coords = True self.has_coords = True
elif self.trigger_type == "data loss" or "no longer on ground" and self.last_longitude != None and self.last_latitude != None: elif self.trigger_type == "data loss" or "no longer on ground" and self.last_longitude != None and self.last_latitude != None:
self.combined = f"{self.last_latitude}, {self.last_longitude}" self.combined = f"{self.last_latitude}, {self.last_longitude}"
nearest_airport_dict = getClosestAirport(self.last_latitude, self.last_longitude) nearest_airport_dict = getClosestAirport(self.last_latitude, self.last_longitude, self.config.get("AIRPORT", "TYPES"))
self.has_coords = True self.has_coords = True
else: else:
print (Fore.RED + 'No Location') print (Fore.RED + 'No Location')