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 116fc618a8
commit c4f7023c2f
4 changed files with 59 additions and 39 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
pythonenv3.8/ 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 =
@ -27,5 +31,5 @@ CHANNEL_TAG = channeltag
ENABLE = TRUE ENABLE = TRUE
#WEBHOOK URL https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks #WEBHOOK URL https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
URL = webhookurl URL = webhookurl
Title = Title =
USERNAME = plane-notify USERNAME = plane-notify

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 # 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"] # header = ["id", "name", "city", "country", "iata", "icao", "lat", "lng", "alt", "tz", "dst", "tz_db", "type", "source"]
airports = [] # with open('airports.dat', encoding='utf-8') as airport_dat:
first_run = True # airport_dat_reader = csv.DictReader(filter(lambda row: row[0]!='#', airport_dat), header)
with open('airports.dat', encoding='utf-8') as csvf: # for airport in airport_dat_reader:
reader = csv.DictReader(filter(lambda row: row[0]!='#', csvf), header) # airport_coord = float(airport['lat']), float(airport['lng'])
#for row in reader: # airport_dist = float((geodesic(plane, airport_coord).mi))
# airports.append(row) # if "closest_airport_dict" not in locals():
for row in reader: # closest_airport_dict = airport
airport = row # closest_airport_dist = airport_dist
airport_coord = float(airport['lat']), float(airport['lng']) # elif airport_dist < closest_airport_dist:
airport_dist = float((geodesic(plane, airport_coord).mi)) # closest_airport_dict = airport
if first_run: # closest_airport_dist = airport_dist
closest_airport_dict = airport # closest_airport_dict['distance'] = closest_airport_dist
closest_airport_dist = airport_dist # print("Closest Airport:", closest_airport_dict['icao'], closest_airport_dict['name'], closest_airport_dist, "Miles Away")
first_run = False # return closest_airport_dict
elif airport_dist < closest_airport_dist: def getClosestAirport(latitude, longitude, allowed_types):
closest_airport_dict = airport import csv
closest_airport_dist = airport_dist from geopy.distance import geodesic
closest_airport_dict['distance'] = closest_airport_dist plane = (latitude, longitude)
print("Closest Airport:", closest_airport_dict['icao'], closest_airport_dict['name'], closest_airport_dist, "Miles Away") with open('airports.csv', 'r') as airport_csv:
return closest_airport_dict 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'] = 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")
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')