Browse Source

Auto download airports.dat

pull/2/head
Jack Sweeney 4 years ago committed by GitHub
parent
commit
1953a87a08
  1. 5
      NotifyBotMulti.py
  2. 71
      defAirport.py
  3. 6
      planeClass.py

5
NotifyBotMulti.py

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

71
defAirport.py

@ -1,27 +1,48 @@
#https://www.geeksforgeeks.org/python-calculate-distance-between-two-places-using-geopy/
#https://openflights.org/data.html
def getAirport(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( 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
def DownloadAirports():
import os
if not os.path.isfile('airports.dat'):
print("No airports.dat file, downloading now")
try:
import requests
url = 'https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat'
airports = requests.get(url)
open('airports.dat', '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:
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
print("Closest Airport:", closest_airport_dict['icao'], closest_airport_dict['name'], closest_airport_dist, "Miles Away")
return closest_airport_dict

6
planeClass.py

@ -47,7 +47,7 @@ class Plane:
import platform
from datetime import datetime
from tabulate import tabulate
from defAirport import getAirport
from defAirport import getClosestAirport
if self.config.get('MAP', 'OPTION') == "GOOGLESTATICMAP":
from defMap import getMap
elif self.config.get('MAP', 'OPTION') == "ADSBX":
@ -304,7 +304,7 @@ class Plane:
raise Exception("Map option not set correctly in this planes conf")
#Discord
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()
sendDis(self.dis_message, self.map_file_name, self.config)
#PushBullet
@ -346,7 +346,7 @@ class Plane:
raise Exception("Map option not set correctly in this planes conf")
#Discord
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()
sendDis(self.dis_message, self.map_file_name, self.config)
#PushBullet

Loading…
Cancel
Save