New Airport Lookup
-New Database Source -Airport types per plane in config
This commit is contained in:
parent
dc4b0e240b
commit
a8608f5c54
|
@ -2,4 +2,5 @@
|
|||
pythonenv3.8/
|
||||
Roboto-Regular.ttf
|
||||
airports.dat
|
||||
__pycache__
|
||||
__pycache__
|
||||
airports.csv
|
||||
|
|
|
@ -7,8 +7,12 @@ ICAO = icaohere
|
|||
#Enter GOOGLESTATICMAP or 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]
|
||||
ENABLE = FALSE
|
||||
TITLE =
|
||||
|
@ -27,5 +31,5 @@ CHANNEL_TAG = channeltag
|
|||
ENABLE = TRUE
|
||||
#WEBHOOK URL https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
|
||||
URL = webhookurl
|
||||
Title =
|
||||
Title =
|
||||
USERNAME = plane-notify
|
||||
|
|
|
@ -2,48 +2,63 @@
|
|||
#https://openflights.org/data.html
|
||||
def download_airports():
|
||||
import os
|
||||
if not os.path.isfile('airports.dat'):
|
||||
print("No airports.dat file, downloading now")
|
||||
if not os.path.isfile('airports.csv'):
|
||||
print("No airports.csv file, downloading now")
|
||||
try:
|
||||
import requests
|
||||
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat'
|
||||
url = 'https://ourairports.com/data/airports.csv'
|
||||
airports = requests.get(url)
|
||||
|
||||
open('airports.dat', 'wb').write(airports.content)
|
||||
open('airports.csv', 'wb').write(airports.content)
|
||||
except:
|
||||
raise("Error getting airports.dat or storing")
|
||||
else:
|
||||
#Writes current date to airports.dat to show when it was aqquired
|
||||
import datetime
|
||||
date = datetime.datetime.now()
|
||||
with open('airports.dat', 'a') as airports:
|
||||
with open('airports.csv', 'a') as airports:
|
||||
airports.write("#" + str(date))
|
||||
print("Successfully got airports.dat")
|
||||
elif os.path.isfile('airports.dat'):
|
||||
print("Already Have airports.dat, continuing")
|
||||
def getClosestAirport(latitude, longitude):
|
||||
import json
|
||||
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"]
|
||||
airports = []
|
||||
first_run = True
|
||||
with open('airports.dat', encoding='utf-8') as csvf:
|
||||
reader = csv.DictReader(filter(lambda row: row[0]!='#', csvf), header)
|
||||
#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))
|
||||
if first_run:
|
||||
closest_airport_dict = airport
|
||||
closest_airport_dist = airport_dist
|
||||
first_run = False
|
||||
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
|
||||
print("Successfully got airports.csv")
|
||||
elif os.path.isfile('airports.csv'):
|
||||
print("Already Have airports.csv, continuing")
|
||||
#OLD Airport lookup
|
||||
# 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
|
||||
from geopy.distance import geodesic
|
||||
plane = (latitude, longitude)
|
||||
with open('airports.csv', 'r') 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'] = 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
|
|
@ -223,11 +223,11 @@ class Plane:
|
|||
if self.landed or self.tookoff:
|
||||
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}"
|
||||
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
|
||||
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}"
|
||||
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
|
||||
else:
|
||||
print (Fore.RED + 'No Location')
|
||||
|
|
Loading…
Reference in New Issue