-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairport_database.py
48 lines (37 loc) · 1.91 KB
/
airport_database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
import math
import pickle
class Airport: # Not used. Was only used to create the database
def __init__(self, icao: str, latitude: float, longitude: float, name: str):
self.icao = icao
self.latitude = latitude
self.longitude = longitude
self.name = name
def __str__(self):
print(f'ICAO: {self.icao} ({self.name})\tLatitude: {self.latitude}\tLongitude: {self.longitude}')
class AirportDatabase:
def __init__(self):
self.database = {}
def add_airport(self, icao: str, latitude: float, longitude: float, name: str):
"""Not used by the program. Was only used to create the database"""
self.database[icao] = Airport(icao, latitude, longitude, name)
def get_lat_and_long(self, icao: str):
"""Returns the latitude and longitude of an airport"""
return self.database[icao].latitude, self.database[icao].longitude
def is_in_list(self, icao: str):
"""Checks if an airport is in the database (airport_database.dat)"""
if icao in self.database:
return True
return False
def get_track(self, departure: str, arrival: str):
"""Finds the track (angle) between the departure and arrival airports"""
lat1, lat2, lon1, lon2 = self.database[departure].latitude, self.database[arrival].latitude, self.database[departure].longitude, self.database[arrival].longitude
delta_lon = lon2 - lon1
x = math.cos(math.radians(lat2)) * math.sin(math.radians(delta_lon))
y = math.cos(math.radians(lat1)) * math.sin(math.radians(lat2)) - math.sin(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.cos(math.radians(delta_lon))
track = np.degrees(np.arctan2(x, y))
return track
def get_database():
"""Loads the database from the .dat file which holds most world airports"""
with open('airport_database.dat', 'rb') as f:
return pickle.load(f)