Browse Source

Nearest Airport IMG, coordinates use fix

-Append airport to map
-Import pull data modules only when needed
-Move when nearest airport is calculated
-Fix use of incorrect coordinates for location lookup and nearest airport #216
pull/2/head
Jack Sweeney 4 years ago committed by GitHub
parent
commit
1c15623b6d
  1. 67
      AppendAirport.py
  2. 12
      NotifyBotMulti.py
  3. 3
      defAirport.py
  4. 20
      planeClass.py

67
AppendAirport.py

@ -0,0 +1,67 @@
def download_font():
import os
fontfile = "Roboto-Regular.ttf"
if not os.path.isfile(fontfile):
print("No font file, downloading now")
try:
import requests
url = 'https://github.com/google/fonts/raw/master/apache/roboto/static/Roboto-Regular.ttf'
font = requests.get(url)
open(fontfile, 'wb').write(font.content)
except:
raise("Error getting font or storing")
else:
print("Successfully got font", fontfile)
elif os.path.isfile(fontfile):
print("Already have font, continuing")
def append_airport(filename, icao, airport, distance_mi):
from PIL import Image, ImageDraw, ImageFont
distance_km = distance_mi * 1.609
# create Image object with the input image
image = Image.open(filename)
# initialise the drawing context with
# the image object as background
draw = ImageDraw.Draw(image)
#Setup fonts
fontfile = "Roboto-Regular.ttf"
font = ImageFont.truetype(fontfile, 14)
mini_font = ImageFont.truetype(fontfile, 12)
head_font = ImageFont.truetype(fontfile, 16)
#Setup Colors
black = 'rgb(0, 0, 0)' # Black
white = 'rgb(255, 255, 255)' # White
navish = 'rgb(0, 63, 75)'
whitish = 'rgb(248, 248, 248)'
#Info Box
draw.rectangle(((316, 760), (605, 800)), fill= white, outline=black)
#Header Box
draw.rectangle(((387, 738), (535, 760)), fill= navish)
#Create Text
#Nearest Airport Header
(x, y) = (408, 740)
text = "Nearest Airport"
draw.text((x, y), text, fill=white, font=head_font)
#ICAO
(x, y) = (320, 765)
text = icao
draw.text((x, y), text, fill=black, font=font)
#Distance
(x, y) = (432, 765)
text = str(round(distance_mi, 2)) + "mi / " + str(round(distance_km, 2)) + "km away"
draw.text((x, y), text, fill=black, font=font)
#Full name
(x, y) = (320, 783)
text = airport[0:56]
draw.text((x, y), text, fill=black, font=mini_font)
# save the edited image
image.save(filename)

12
NotifyBotMulti.py

@ -1,7 +1,5 @@
import configparser
import time
from defADSBX import pullADSBX
from defOpenSky import pullOpenSky
from colorama import Fore, Back, Style
import platform
if platform.system() == "Windows":
@ -9,14 +7,16 @@ if platform.system() == "Windows":
init(convert=True)
from planeClass import Plane
from datetime import datetime
from defAirport import DownloadAirports
from defAirport import download_airports
from AppendAirport import download_font
import pytz
DownloadAirports()
download_airports()
download_font()
main_config = configparser.ConfigParser()
main_config.read('./configs/mainconf.ini')
import os
import sys
#Setup Plane Objects off of Plane configs
#Setup plane objects from plane configs
planes = {}
for filename in os.listdir("./configs"):
if filename.endswith(".ini") and filename != "mainconf.ini":
@ -38,6 +38,7 @@ while True:
start_time = time.time()
print (Back.GREEN, Fore.BLACK, "--------", running_Count, "--------", datetime_tz.strftime("%I:%M:%S %p"), "-------------------------------------------------------", Style.RESET_ALL)
if main_config.get('DATA', 'SOURCE') == "ADSBX":
from defADSBX import pullADSBX
data, failed = pullADSBX(planes)
if failed == False:
if data['ac'] != None:
@ -54,6 +55,7 @@ while True:
for obj in planes.values():
obj.run(None)
elif main_config.get('DATA', 'SOURCE') == "OPENS":
from defOpenSky import pullOpenSky
planeData, failed = pullOpenSky(planes)
if failed == False:
if planeData.states != []:

3
defAirport.py

@ -1,6 +1,6 @@
#https://www.geeksforgeeks.org/python-calculate-distance-between-two-places-using-geopy/
#https://openflights.org/data.html
def DownloadAirports():
def download_airports():
import os
if not os.path.isfile('airports.dat'):
print("No airports.dat file, downloading now")
@ -44,5 +44,6 @@ def getClosestAirport(latitude, longitude):
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

20
planeClass.py

@ -47,6 +47,7 @@ class Plane:
import platform
from datetime import datetime
from tabulate import tabulate
from AppendAirport import append_airport
from defAirport import getClosestAirport
if self.config.get('MAP', 'OPTION') == "GOOGLESTATICMAP":
from defMap import getMap
@ -213,11 +214,13 @@ class Plane:
print("Tookoff by", self.trigger_type)
#Lookup Location of coordinates
if self.landed or self.tookoff:
if self.landed and self.last_longitude != None and self.last_latitude != None:
self.combined = f"{self.last_latitude}, {self.last_longitude}"
self.has_coords = True
elif self.tookoff 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}"
nearest_airport_dict = getClosestAirport(self.latitude, self.longitude)
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)
self.has_coords = True
else:
print (Fore.RED + 'No Location')
@ -300,12 +303,12 @@ class Plane:
getMap((self.aera_hierarchy + ", " + self.state + ", " + self.country_code), self.icao)
elif self.config.get('MAP', 'OPTION') == "ADSBX":
getSS(self.icao)
append_airport(self.map_file_name, nearest_airport_dict['icao'], nearest_airport_dict['name'], nearest_airport_dict['distance'])
else:
raise Exception("Map option not set correctly in this planes conf")
#Discord
if self.config.getboolean('DISCORD', 'ENABLE'):
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_airport_dict['icao'] + ", " + nearest_airport_dict["name"]).strip()
sendDis(self.dis_message, self.map_file_name, self.config)
#PushBullet
if self.config.getboolean('PUSHBULLET', 'ENABLE'):
@ -342,12 +345,13 @@ class Plane:
getMap((self.aera_hierarchy + ", " + self.state + ", " + self.country_code), self.icao)
elif self.config.get('MAP', 'OPTION') == "ADSBX":
getSS(self.icao)
append_airport(self.map_file_name, nearest_airport_dict['icao'], nearest_airport_dict['name'], nearest_airport_dict['distance'])
else:
raise Exception("Map option not set correctly in this planes conf")
#Discord
if self.config.getboolean('DISCORD', 'ENABLE'):
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_airport_dict['icao'] + ", " + nearest_airport_dict["name"]).strip()
sendDis(self.dis_message, self.map_file_name, self.config)
#PushBullet
if self.config.getboolean('PUSHBULLET', 'ENABLE'):

Loading…
Cancel
Save