-
Notifications
You must be signed in to change notification settings - Fork 82
/
runways.py
288 lines (227 loc) · 8.93 KB
/
runways.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import pickle
from io import BytesIO
from pathlib import Path
from typing import Any, Dict, List, NamedTuple, Optional, Tuple
from zipfile import ZipFile
import pandas as pd
import requests
from cartopy.crs import PlateCarree
from shapely.geometry import base, shape
from shapely.ops import linemerge
import altair as alt
from tqdm.autonotebook import tqdm
from ... import cache_expiration
from ...core.geodesy import bearing, destination
from ...core.mixins import HBoxMixin, PointMixin, ShapelyMixin
__github_url = "https://raw.githubusercontent.com/"
base_url = __github_url + "ProfHoekstra/bluesky/master/data/navdata"
class ThresholdTuple(NamedTuple):
latitude: float
longitude: float
bearing: float
name: str
class Threshold(ThresholdTuple, PointMixin):
def __repr__(self):
return f"Runway {self.name}: {self.latlon}"
RunwaysType = Dict[str, List[Tuple[Threshold, Threshold]]]
class RunwayAirport(HBoxMixin, ShapelyMixin):
def __init__(self, runways: List[Tuple[Threshold, Threshold]]):
self._runways = runways
@property
def data(self) -> pd.DataFrame:
return pd.DataFrame.from_records(
self.list, columns=["latitude", "longitude", "bearing", "name"]
)
@property
def list(self) -> List[Threshold]:
return sum((list(runway) for runway in self._runways), [])
def geojson(self) -> List[Dict[str, Any]]:
return [
{
"geometry": {
"type": "LineString",
"coordinates": tuple(
(thrs.longitude, thrs.latitude) for thrs in runway
),
},
"properties": "/".join(thrs.name for thrs in runway),
"type": "Feature",
}
for runway in self._runways
]
@property
def shape(self) -> base.BaseGeometry:
return linemerge(shape(x["geometry"]) for x in self.geojson())
def plot(
self,
ax,
*,
runways: bool = True,
labels: bool = False,
shift: int = 300,
text_kw: Optional[Dict] = None,
**kwargs,
): # coverage: ignore
if runways is True:
params = {
"edgecolor": "#0e1111",
"crs": PlateCarree(),
"linewidth": 3,
**kwargs,
}
ax.add_geometries([self.shape], **params)
if labels is True:
if text_kw is None:
text_kw = dict()
text_kw = {
**dict(
transform=PlateCarree(),
fontsize=18,
horizontalalignment="center",
verticalalignment="center",
rotation_mode="anchor",
),
**text_kw,
}
for thr in self.list:
lat, lon, _ = destination(
thr.latitude, thr.longitude, thr.bearing + 180, shift
)
ax.text(
lon, lat, thr.name, rotation=360 - thr.bearing, **text_kw
)
def geoencode(
self, mode: str = "geometry"
) -> Optional[alt.Chart]: # coverage: ignore
if mode == "geometry":
return (
super().geoencode().mark_geoshape(strokeWidth=2, stroke="black")
)
elif mode == "labels":
rwy_labels = alt.Chart(self.data).encode(
longitude="longitude:Q", latitude="latitude:Q", text="name:N"
)
rwy_layers = [
rwy_labels.transform_filter(alt.datum.name == name).mark_text(
angle=bearing, baseline="middle", dy=10
)
for (name, bearing) in zip(self.data.name, self.data.bearing)
]
return alt.layer(*rwy_layers)
else:
return None
class Runways(object):
cache_dir: Optional[Path] = None
def __init__(self) -> None:
self._runways: Optional[RunwaysType] = None
assert self.cache_dir is not None
self._cache = self.cache_dir / "runways_ourairports.pkl"
@property
def runways(self) -> RunwaysType:
if self._runways is not None:
return self._runways
if not self._cache.exists():
self.download_runways()
last_modification = self._cache.lstat().st_mtime
delta = pd.Timestamp("now") - pd.Timestamp(last_modification * 1e9)
if delta > cache_expiration:
try:
self.download_runways()
except requests.ConnectionError:
pass
with self._cache.open("rb") as fh:
self._runways = pickle.load(fh)
return self._runways
def __getitem__(self, airport) -> Optional[RunwayAirport]:
if isinstance(airport, str):
from .. import airports
airport = airports[airport]
if airport is None:
return None
elt = self.runways.get(airport.icao, None)
if elt is None:
return None
return RunwayAirport(elt)
def download_runways(self) -> None: # coverage: ignore
from .. import session
self._runways = dict()
f = session.get("https://ourairports.com/data/runways.csv", stream=True)
total = int(f.headers["Content-Length"])
buffer = BytesIO()
for chunk in tqdm(
f.iter_content(1024),
total=total // 1024 + 1 if total % 1024 > 0 else 0,
desc="runways @ourairports.com",
):
buffer.write(chunk)
buffer.seek(0)
df = pd.read_csv(buffer)
for name, _df in df.groupby("airport_ident"):
cur: List[Tuple[Threshold, Threshold]] = list()
self._runways[name] = cur
for _, line in _df.iterrows():
lat0 = line.le_latitude_deg
lon0 = line.le_longitude_deg
name0 = line.le_ident
lat1 = line.he_latitude_deg
lon1 = line.he_longitude_deg
name1 = line.he_ident
if lat0 != lat0 or lat1 != lat1:
# some faulty records here...
continue
brng0 = bearing(lat0, lon0, lat1, lon1)
brng1 = bearing(lat1, lon1, lat0, lon0)
brng0 = brng0 if brng0 > 0 else 360 + brng0
brng1 = brng1 if brng1 > 0 else 360 + brng1
thr0 = Threshold(lat0, lon0, brng0, name0)
thr1 = Threshold(lat1, lon1, brng1, name1)
cur.append((thr0, thr1))
with self._cache.open("wb") as fh:
pickle.dump(self._runways, fh)
def download_bluesky(self) -> None: # coverage: ignore
from .. import session
self._runways = dict()
c = session.get(base_url + "/apt.zip")
with ZipFile(BytesIO(c.content)).open("apt.dat", "r") as fh:
for line in fh.readlines():
elems = (
line.decode(encoding="ascii", errors="ignore")
.strip()
.split()
)
if len(elems) == 0:
continue
# 1: AIRPORT
if elems[0] == "1":
# Add airport to runway threshold database
cur: List[Tuple[Threshold, Threshold]] = list()
self._runways[elems[4]] = cur
if elems[0] == "100":
# Only asphalt and concrete runways
if int(elems[2]) > 2:
continue
lat0 = float(elems[9])
lon0 = float(elems[10])
# offset0 = float(elems[11])
lat1 = float(elems[18])
lon1 = float(elems[19])
# offset1 = float(elems[20])
# threshold information:
# ICAO code airport,
# Runway identifier,
# latitude, longitude, bearing
# vertices: gives vertices of the box around the threshold
# opposite runways are on the same line.
# RWY1: 8-11, RWY2: 17-20
# Hence, there are two thresholds per line
# thr0: First lat0 and lon0, then lat1 and lat1, offset=[11]
# thr1: First lat1 and lat1, then lat0 and lon0, offset=[20]
brng0 = bearing(lat0, lon0, lat1, lon1)
brng1 = bearing(lat1, lon1, lat0, lon0)
brng0 = brng0 if brng0 > 0 else 360 + brng0
brng1 = brng1 if brng1 > 0 else 360 + brng1
thr0 = Threshold(lat0, lon0, brng0, elems[8])
thr1 = Threshold(lat1, lon1, brng1, elems[17])
cur.append((thr0, thr1))
with self._cache.open("wb") as fh:
pickle.dump(self._runways, fh)