-
Notifications
You must be signed in to change notification settings - Fork 1
/
legs.py
234 lines (195 loc) · 6.4 KB
/
legs.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'''
legs.py
Finds the shortest route between two airports such that each leg is within
the maximum range of one's aircraft.
'''
import sys
from rich.console import Console
from rich.table import Column, Table, box
from rich.markdown import Markdown
from rich.progress import track
from igrf.magvar import Magvar
from utils import db, globenav
console = Console()
MV = Magvar()
def distance(a,b):
aLat = float(a['latitude_deg'])
aLon = float(a['longitude_deg'])
bLat = float(b['latitude_deg'])
bLon = float(b['longitude_deg'])
return globenav.dist_coord(aLat, aLon, bLat, bLon)
# bearing from A -> B
def bearing(a,b):
aLat = float(a['latitude_deg'])
aLon = float(a['longitude_deg'])
bLat = float(b['latitude_deg'])
bLon = float(b['longitude_deg'])
midLat = (aLat + bLat) / 2
midLon = (aLon + bLon) / 2
return globenav.wrap_brg(globenav.brg_coord(aLat, aLon, bLat, bLon) - MV.declination(midLat, midLon, 0))
if len(sys.argv) > 2:
src = sys.argv[1].upper()
dst = sys.argv[2].upper()
if src == dst:
sys.exit("Your source and destination are the same!")
if len(sys.argv) > 3:
try:
maxRange = float(sys.argv[3])
except TypeError:
sys.exit("You must provide a numeric maxium range!")
else:
sys.exit("You must provide a maximum range!")
else:
sys.exit("You must provide two ICAO airport codes and a maximum range!")
possibleSources=[]
possibleDests=[]
Q = []
dist = {}
prev = {}
def matchICAOCodesAndPrep(element):
ident = element['ident']
if ident == src:
possibleSources.append(element)
elif ident == dst:
possibleDests.append(element)
element['dist'] = 99999
element['prev'] = None
Q.append(element)
db.execute('airports.csv', matchICAOCodesAndPrep)
if len(possibleSources) == 0 or len(possibleDests) == 0:
if len(possibleSources) == 0:
print("Can't find " + src + "!")
if len(possibleDests) == 0:
print("Can't find " + dst + "!")
sys.exit()
refSource = None
refDest = None
refSource = possibleSources[0]
def minSortdist(e):
return e['sortdist']
for dest in possibleDests:
dest['sortdist'] = distance(refSource,dest)
possibleDests.sort(key=minSortdist)
refDest = possibleDests[0]
srcName = refSource['name']
dstName = refDest['name']
srcLat = float(refSource['latitude_deg'])
srcLong = float(refSource['longitude_deg'])
dstLat = float(refDest['latitude_deg'])
dstLong = float(refDest['longitude_deg'])
refSource['dist'] = 0
basic = ['closed', 'seaplane_base']
smallAp = ['small_airport']
mediumAp = ['medium_airport']
largeAp = ['large_airport']
# Large Airports only by default
filterTypes = basic + smallAp + mediumAp
print("")
print("Enter number of airport filter (or press Return for 1):\n")
print(" [1] - Large airports only")
print(" [2] - Medium airports only")
print(" [3] - Small airports only")
print(" [4] - Large and medium airports")
print(" [5] - Small and medium airports")
print(" [6] - All airports (very slow)")
print("")
routeSelect = input("> ").rstrip()
if routeSelect == "2":
filterTypes = basic + smallAp + largeAp
elif routeSelect == "3":
filterTypes = basic + mediumAp + largeAp
elif routeSelect == "4":
filterTypes = basic + smallAp
elif routeSelect == "5":
filterTypes = basic + largeAp
elif routeSelect == "6":
filterTypes = basic
extremeDist = globenav.dist_coord(srcLat,srcLong,dstLat,dstLong)
midLat = (srcLat + dstLat) / 2
midLon = (srcLong + dstLong) / 2
# Eliminate heliports or airports that are wildly out of range
Z = []
while Q:
e = Q.pop()
allow = True
if e != refSource and e != refDest:
eLat = float(e['latitude_deg'])
eLon = float(e['longitude_deg'])
if globenav.dist_coord(midLat,midLon,eLat,eLon) > extremeDist:
allow=False
else:
eType = e['type'].rstrip()
for fType in filterTypes:
if eType == fType:
allow=False
break
if eType.find("heli") != -1:
allow=False
if allow:
Z.append(e)
Q = Z
print("\nPlease wait... considering " + str(len(Q)) + " possible airports.\n")
def min_dist(e):
return e['dist']
while len(Q) > 0:
Q.sort(key=min_dist)
u = Q.pop(0)
if u==refDest:
break
uLat = float(u['latitude_deg'])
uLon = float(u['longitude_deg'])
for v in Q:
vLat = float(v['latitude_deg'])
vLon = float(v['longitude_deg'])
nodeDist = globenav.dist_coord(uLat,uLon,vLat,vLon)
# consider v a neighbor of u if u within max range
if nodeDist <= maxRange:
alt = u['dist'] + nodeDist
if alt < v['dist']:
v['dist'] = alt
v['prev'] = u
S = []
u = refDest
if u['prev'] != None or u == refSource:
while not (u == None):
S.insert(0, u)
u = u['prev']
if len(S) == 0:
sys.exit("Can't find a valid route! Try searching more airports, or use a larger maximum range.\n")
# remove source and destination
S.remove(S[0])
S.remove(S[len(S)-1])
naTable = Table(show_header=True, box=box.SIMPLE)
naTable.add_column("Leg", justify="center")
naTable.add_column("ID", justify="center")
naTable.add_column("Name", justify="left")
naTable.add_column("Distance", justify="right")
naTable.add_column("Heading", justify="right")
prevNode = refSource
totalDist = 0
naTable.add_row("", refSource['ident'], refSource['name'], "", "")
for i in range(len(S)):
navaid = S[i]
nDist = distance(prevNode, navaid)
nBrg = int(round(bearing(prevNode, navaid)))
naTable.add_row(str(i+1), navaid['ident'], navaid['name'], str(round(nDist,1)) + " nm", str(nBrg) + "°")
prevNode = navaid
totalDist += nDist
nDist = distance(prevNode, refDest)
totalDist += nDist
brgStr = ""
if nDist > 0:
brgStr = str(int(round(bearing(prevNode, refDest)))) + "°"
naTable.add_row(str(len(S)+1), refDest['ident'], refDest['name'], str(round(nDist,1)) + " nm", brgStr)
print("")
console.print(Markdown("# Route from " + src + " (" + srcName + ") to " + dst + " (" + dstName + "), max leg distance of " + str(maxRange) + " nm"))
console.print(naTable)
console.print("[b]Total distance:[/b] " + str(int(round(totalDist))) + " nm")
print("")
routeStr = refSource['ident']
print("Route string for flight plan:")
for na in S:
routeStr = routeStr + " " + na['ident']
routeStr = routeStr + " " + refDest['ident']
print(routeStr)
print("")