Auto download airports.dat

This commit is contained in:
Jack Sweeney 2020-11-10 01:19:33 +00:00 committed by GitHub
parent 7cb7c538d6
commit 1953a87a08
3 changed files with 52 additions and 30 deletions

View File

@ -9,7 +9,9 @@ if platform.system() == "Windows":
init(convert=True) init(convert=True)
from planeClass import Plane from planeClass import Plane
from datetime import datetime from datetime import datetime
from defAirport import DownloadAirports
import pytz import pytz
DownloadAirports()
main_config = configparser.ConfigParser() main_config = configparser.ConfigParser()
main_config.read('./configs/mainconf.ini') main_config.read('./configs/mainconf.ini')
import os import os
@ -82,5 +84,4 @@ while True:
sys.stdout.flush() sys.stdout.flush()
time.sleep(1) time.sleep(1)
sys.stdout.write(Back.RED + ('\x1b[1K\r' +"Slept for " +str(sleep_sec)) + Style.RESET_ALL) sys.stdout.write(Back.RED + ('\x1b[1K\r' +"Slept for " +str(sleep_sec)) + Style.RESET_ALL)
print() print()

View File

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

View File

@ -47,7 +47,7 @@ class Plane:
import platform import platform
from datetime import datetime from datetime import datetime
from tabulate import tabulate from tabulate import tabulate
from defAirport import getAirport from defAirport import getClosestAirport
if self.config.get('MAP', 'OPTION') == "GOOGLESTATICMAP": if self.config.get('MAP', 'OPTION') == "GOOGLESTATICMAP":
from defMap import getMap from defMap import getMap
elif self.config.get('MAP', 'OPTION') == "ADSBX": elif self.config.get('MAP', 'OPTION') == "ADSBX":
@ -304,7 +304,7 @@ class Plane:
raise Exception("Map option not set correctly in this planes conf") raise Exception("Map option not set correctly in this planes conf")
#Discord #Discord
if self.config.getboolean('DISCORD', 'ENABLE'): if self.config.getboolean('DISCORD', 'ENABLE'):
nearest = getAirport(self.latitude, self.longitude) nearest = getClosestAirport(self.latitude, self.longitude)
self.dis_message = (self.dis_title + " " + self.tookoff_message + nearest['icao'] + ", " + nearest["name"]).strip() self.dis_message = (self.dis_title + " " + self.tookoff_message + nearest['icao'] + ", " + nearest["name"]).strip()
sendDis(self.dis_message, self.map_file_name, self.config) sendDis(self.dis_message, self.map_file_name, self.config)
#PushBullet #PushBullet
@ -346,7 +346,7 @@ class Plane:
raise Exception("Map option not set correctly in this planes conf") raise Exception("Map option not set correctly in this planes conf")
#Discord #Discord
if self.config.getboolean('DISCORD', 'ENABLE'): if self.config.getboolean('DISCORD', 'ENABLE'):
nearest = getAirport(self.last_latitude, self.last_longitude) nearest = getClosestAirport(self.last_latitude, self.last_longitude)
self.dis_message = (self.dis_title + " " +self.landed_message + nearest['icao'] + ", " + nearest["name"]).strip() self.dis_message = (self.dis_title + " " +self.landed_message + nearest['icao'] + ", " + nearest["name"]).strip()
sendDis(self.dis_message, self.map_file_name, self.config) sendDis(self.dis_message, self.map_file_name, self.config)
#PushBullet #PushBullet