-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_widget.py
More file actions
466 lines (392 loc) · 15.9 KB
/
Copy pathmap_widget.py
File metadata and controls
466 lines (392 loc) · 15.9 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/env python3
"""
Slippy map widget — live moving map with aircraft position.
Supports OpenStreetMap tiles with OpenAIP overlay capability.
"""
import math
import os
import hashlib
from PyQt6.QtWidgets import QWidget
from PyQt6.QtCore import Qt, QPointF, QRectF, QUrl, QTimer
from PyQt6.QtGui import (
QPainter, QPen, QBrush, QColor, QFont, QPolygonF, QImage, QPixmap,
)
from PyQt6.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
TILE_SIZE = 256
CACHE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".tile_cache")
# Map providers — tile URL templates
PROVIDERS = {
"OpenStreetMap": {
"url": "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
"subdomains": ["a", "b", "c"],
"attribution": "OpenStreetMap",
"max_zoom": 19,
"api_key": False,
},
"OpenTopoMap": {
"url": "https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png",
"subdomains": ["a", "b", "c"],
"attribution": "OpenTopoMap",
"max_zoom": 17,
"api_key": False,
},
"OpenAIP": {
"url": "https://{s}.api.tiles.openaip.net/api/data/openaip/{z}/{x}/{y}.png?apiKey={key}",
"subdomains": ["a", "b", "c"],
"attribution": "OpenAIP",
"max_zoom": 14,
"api_key": True,
"key_name": "openaip_api_key",
},
}
DEFAULT_PROVIDER = "OpenStreetMap"
def _lat_lon_to_tile(lat, lon, zoom):
n = 2 ** zoom
x = (lon + 180.0) / 360.0 * n
lat_rad = math.radians(lat)
y = (1.0 - math.log(math.tan(lat_rad) + 1.0 / math.cos(lat_rad)) / math.pi) / 2.0 * n
return x, y
def _tile_to_lat_lon(tx, ty, zoom):
n = 2 ** zoom
lon = tx / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1.0 - 2.0 * ty / n)))
lat = math.degrees(lat_rad)
return lat, lon
class MapWidget(QWidget):
"""Slippy map centered on aircraft position with heading-up rotation."""
def __init__(self, parent=None):
super().__init__(parent)
self.setMinimumSize(200, 200)
self.setAttribute(Qt.WidgetAttribute.WA_OpaquePaintEvent)
self._lat = 0.0
self._lon = 0.0
self._heading = 0.0
self._zoom = 14
self._min_zoom = 4
self._max_zoom = 18
self._has_position = False
self._heading_up = False # False = north up, True = heading up
self._disp_heading = 0.0 # smoothed display heading for rotation
self._heading_alpha = 0.80 # smoothing for heading rotation
self._provider_name = DEFAULT_PROVIDER
self._overlay_enabled = False # OpenAIP overlay
# Pan offset (pixels) from aircraft center
self._pan_x = 0.0
self._pan_y = 0.0
self._centered = True # auto-follow aircraft
self._drag_start = None # mouse drag start point
# Tile cache (in-memory + disk)
self._tile_cache = {} # (z, x, y, provider) -> QPixmap
self._pending = set()
# Network
self._nam = QNetworkAccessManager(self)
self._nam.finished.connect(self._on_tile_loaded)
# API keys (loaded from config later)
self._api_keys = {}
# Cached font for NO DATA tiles
self._nodata_font = QFont("Monospace", 16)
self._nodata_font.setBold(True)
# Ensure cache dir
os.makedirs(CACHE_DIR, exist_ok=True)
@property
def provider_name(self):
return self._provider_name
@provider_name.setter
def provider_name(self, name):
if name in PROVIDERS:
self._provider_name = name
self._max_zoom = PROVIDERS[name]["max_zoom"]
self._zoom = min(self._zoom, self._max_zoom)
self._pending.clear()
self.update()
@property
def heading_up(self):
return self._heading_up
@heading_up.setter
def heading_up(self, val):
self._heading_up = val
if val:
self._disp_heading = self._heading
self.update()
def set_api_key(self, provider, key):
self._api_keys[provider] = key
self._pending.clear()
self.update()
def set_position(self, lat, lon, heading):
if lat == 0.0 and lon == 0.0:
return
self._lat = lat
self._lon = lon
self._heading = heading
self._has_position = True
if self._centered:
self._pan_x = 0.0
self._pan_y = 0.0
# Smooth heading for map rotation (handle 360/0 wraparound)
if self._heading_up:
diff = (heading - self._disp_heading + 180) % 360 - 180
self._disp_heading = (self._disp_heading + (1 - self._heading_alpha) * diff) % 360
self.update()
def center_on_aircraft(self):
self._pan_x = 0.0
self._pan_y = 0.0
self._centered = True
self.update()
def mousePressEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
self._drag_start = event.position()
self.setCursor(Qt.CursorShape.ClosedHandCursor)
def mouseMoveEvent(self, event):
if self._drag_start is not None:
delta = event.position() - self._drag_start
self._pan_x += delta.x()
self._pan_y += delta.y()
self._centered = False
self._drag_start = event.position()
self.update()
def mouseReleaseEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
self._drag_start = None
self.setCursor(Qt.CursorShape.ArrowCursor)
def set_zoom(self, z):
new_z = max(self._min_zoom, min(self._max_zoom, z))
if new_z != self._zoom:
# Scale pan offset: each zoom level doubles the pixel scale
scale = 2 ** (new_z - self._zoom)
self._pan_x *= scale
self._pan_y *= scale
self._zoom = new_z
self._pending.clear()
self.update()
def wheelEvent(self, event):
delta = event.angleDelta().y()
if delta > 0:
self.set_zoom(self._zoom + 1)
elif delta < 0:
self.set_zoom(self._zoom - 1)
def paintEvent(self, event):
p = QPainter(self)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
p.fillRect(self.rect(), QColor(0x0A, 0x0C, 0x10))
if not self._has_position:
self._draw_no_fix(p)
p.end()
return
w, h = self.width(), self.height()
cx, cy = w / 2.0, h / 2.0
zoom = self._zoom
map_rotation = self._disp_heading if (self._heading_up and self._centered) else 0.0
# Aircraft position in tile coordinates
ax, ay = _lat_lon_to_tile(self._lat, self._lon, zoom)
# Calculate tiles needed (viewport size + rotation margin, clamped)
diag = math.hypot(w, h) / 2.0 + TILE_SIZE
tiles_needed = min(8, int(math.ceil(diag / TILE_SIZE)) + 1)
# Shift tile center by pan offset (in tile units) to fetch correct tiles
pan_tx = self._pan_x / TILE_SIZE
pan_ty = self._pan_y / TILE_SIZE
view_cx = ax - pan_tx
view_cy = ay - pan_ty
tile_cx = int(view_cx)
tile_cy = int(view_cy)
frac_x = (ax - tile_cx) * TILE_SIZE
frac_y = (ay - tile_cy) * TILE_SIZE
provider = PROVIDERS.get(self._provider_name, PROVIDERS[DEFAULT_PROVIDER])
# Rotate canvas for heading-up mode (around aircraft position)
ac_sx = cx + self._pan_x
ac_sy = cy + self._pan_y
p.save()
p.translate(ac_sx, ac_sy)
p.rotate(-map_rotation)
p.translate(-ac_sx, -ac_sy)
# Draw tiles
for dx in range(-tiles_needed, tiles_needed + 1):
for dy in range(-tiles_needed, tiles_needed + 1):
tx = tile_cx + dx
ty = tile_cy + dy
n = 2 ** zoom
if ty < 0 or ty >= n:
continue
tx = tx % n
px = cx + (dx * TILE_SIZE) - frac_x + self._pan_x
py = cy + (dy * TILE_SIZE) - frac_y + self._pan_y
# Skip tiles completely outside viewport
if px + TILE_SIZE < -w or px > w * 2 or py + TILE_SIZE < -h or py > h * 2:
continue
pixmap = self._get_tile(zoom, tx, ty)
if pixmap:
p.drawPixmap(int(px), int(py), pixmap)
else:
ipx, ipy = int(px), int(py)
p.fillRect(ipx, ipy, TILE_SIZE, TILE_SIZE, QColor(30, 32, 40))
p.setPen(QPen(QColor(200, 50, 50), 1))
p.setBrush(Qt.BrushStyle.NoBrush)
p.drawRect(ipx, ipy, TILE_SIZE, TILE_SIZE)
p.setFont(self._nodata_font)
p.drawText(QRectF(ipx, ipy, TILE_SIZE, TILE_SIZE),
Qt.AlignmentFlag.AlignCenter, "NO DATA")
# Draw overlay tiles (OpenAIP)
if self._overlay_enabled and self._api_keys.get("OpenAIP"):
for dx in range(-tiles_needed, tiles_needed + 1):
for dy in range(-tiles_needed, tiles_needed + 1):
tx = tile_cx + dx
ty = tile_cy + dy
n = 2 ** zoom
if ty < 0 or ty >= n:
continue
tx = tx % n
px = cx + (dx * TILE_SIZE) - frac_x + self._pan_x
py = cy + (dy * TILE_SIZE) - frac_y + self._pan_y
if px + TILE_SIZE < -w or px > w * 2 or py + TILE_SIZE < -h or py > h * 2:
continue
overlay = self._get_tile(zoom, tx, ty, provider_override="OpenAIP")
if overlay:
p.drawPixmap(int(px), int(py), overlay)
p.restore() # undo map rotation
# Aircraft at screen center + pan (stays at its geographic position)
ac_x = cx + self._pan_x
ac_y = cy + self._pan_y
ac_rotation = 0.0 if (self._heading_up and self._centered) else self._heading
self._draw_aircraft(p, ac_x, ac_y, ac_rotation)
# Attribution
attr = provider.get("attribution", "")
if attr:
font = p.font()
font.setPixelSize(10)
p.setFont(font)
p.setPen(QPen(QColor(150, 150, 150, 180)))
p.drawText(4, h - 4, f"\u00A9 {attr}")
# Zoom level
font = p.font()
font.setPixelSize(11)
font.setBold(True)
p.setFont(font)
p.setPen(QPen(QColor(200, 200, 200)))
p.drawText(w - 40, h - 4, f"Z{zoom}")
p.end()
def _draw_no_fix(self, p: QPainter):
w, h = self.width(), self.height()
cx, cy = w / 2.0, h / 2.0
# Dark background with crosshatch
p.fillRect(self.rect(), QColor(0x0A, 0x0C, 0x10))
# Red X like the PFD fail flags
margin = min(w, h) * 0.3
pen = QPen(QColor(200, 50, 50, 120), 3)
p.setPen(pen)
p.drawLine(int(cx - margin), int(cy - margin), int(cx + margin), int(cy + margin))
p.drawLine(int(cx + margin), int(cy - margin), int(cx - margin), int(cy + margin))
# Message box
box_w, box_h = 220, 50
box_x = cx - box_w / 2
box_y = cy - box_h / 2
p.setPen(QPen(QColor(200, 50, 50), 2))
p.setBrush(QColor(10, 12, 16, 220))
p.drawRoundedRect(int(box_x), int(box_y), box_w, box_h, 6, 6)
font = p.font()
font.setPixelSize(16)
font.setBold(True)
p.setFont(font)
p.setPen(QPen(QColor(255, 80, 80)))
p.drawText(QRectF(box_x, box_y, box_w, box_h),
Qt.AlignmentFlag.AlignCenter, "MAP\nWaiting for GPS fix")
def _draw_aircraft(self, p: QPainter, cx, cy, heading):
p.save()
p.translate(cx, cy)
p.rotate(heading)
s = 16 # scale factor
p.setPen(QPen(QColor(0, 0, 0), 2))
p.setBrush(QColor(255, 255, 255))
# Airplane top-down silhouette
fuselage = QPolygonF([
QPointF(0, -s * 1.4), # nose
QPointF(-s * 0.15, -s * 0.8),
QPointF(-s * 0.15, s * 0.8),
QPointF(-s * 0.25, s * 1.2), # tail left
QPointF(s * 0.25, s * 1.2), # tail right
QPointF(s * 0.15, s * 0.8),
QPointF(s * 0.15, -s * 0.8),
])
p.drawPolygon(fuselage)
# Wings
wings = QPolygonF([
QPointF(0, -s * 0.15),
QPointF(-s * 1.1, s * 0.3),
QPointF(-s * 1.1, s * 0.5),
QPointF(0, s * 0.15),
QPointF(s * 1.1, s * 0.5),
QPointF(s * 1.1, s * 0.3),
])
p.drawPolygon(wings)
# Horizontal stabilizer
tail = QPolygonF([
QPointF(0, s * 0.8),
QPointF(-s * 0.5, s * 1.1),
QPointF(-s * 0.5, s * 1.2),
QPointF(0, s * 0.95),
QPointF(s * 0.5, s * 1.2),
QPointF(s * 0.5, s * 1.1),
])
p.drawPolygon(tail)
p.restore()
def _get_tile(self, z, x, y, provider_override=None):
provider = provider_override or self._provider_name
key = (z, x, y, provider)
# In-memory cache
if key in self._tile_cache:
return self._tile_cache[key]
# Disk cache
cache_path = self._cache_path(z, x, y, provider)
if os.path.exists(cache_path):
pixmap = QPixmap(cache_path)
if not pixmap.isNull():
self._tile_cache[key] = pixmap
return pixmap
# Fetch from network (don't add to pending if it will be skipped)
if key not in self._pending:
prov = PROVIDERS.get(provider, PROVIDERS[DEFAULT_PROVIDER])
if prov.get("api_key") and not self._api_keys.get(provider):
return None # no API key — don't block future retries
self._pending.add(key)
self._fetch_tile(z, x, y, provider)
return None
def _cache_path(self, z, x, y, provider):
safe_name = provider.replace(" ", "_").lower()
return os.path.join(CACHE_DIR, f"{safe_name}_{z}_{x}_{y}.png")
def _fetch_tile(self, z, x, y, provider_name):
provider = PROVIDERS.get(provider_name, PROVIDERS[DEFAULT_PROVIDER])
url_template = provider["url"]
subdomains = provider.get("subdomains", ["a"])
s = subdomains[(x + y) % len(subdomains)]
url = url_template.replace("{s}", s).replace("{z}", str(z)).replace("{x}", str(x)).replace("{y}", str(y))
if provider.get("api_key"):
key_name = provider.get("key_name", "")
api_key = self._api_keys.get(provider_name, "")
url = url.replace("{key}", api_key)
if not api_key:
return # skip if no API key configured
request = QNetworkRequest(QUrl(url))
request.setRawHeader(b"User-Agent", b"XsensMTiG-PFD/1.0")
request.setAttribute(QNetworkRequest.Attribute.User, (z, x, y, provider_name))
self._nam.get(request)
def _on_tile_loaded(self, reply: QNetworkReply):
attr = reply.request().attribute(QNetworkRequest.Attribute.User)
if attr is None:
reply.deleteLater()
return
z, x, y, provider_name = attr
key = (z, x, y, provider_name)
self._pending.discard(key)
if reply.error() == QNetworkReply.NetworkError.NoError:
data = reply.readAll().data()
pixmap = QPixmap()
if pixmap.loadFromData(data):
# Evict old entries if memory cache grows too large
if len(self._tile_cache) > 500:
keys = list(self._tile_cache.keys())
for k in keys[:200]:
del self._tile_cache[k]
self._tile_cache[key] = pixmap
# Save to disk cache
cache_path = self._cache_path(z, x, y, provider_name)
pixmap.save(cache_path, "PNG")
self.update()
reply.deleteLater()