Merge pull request 'feat(gpsjam): GPS interference hex overlay API (GPSJAM/ADS-B Exchange)' (#29) from feat/gpsjam-interference-overlay into master
Some checks failed
build-and-deploy / build-push-deploy (push) Failing after 1h14m25s
Some checks failed
build-and-deploy / build-push-deploy (push) Failing after 1h14m25s
Reviewed-on: #29
This commit is contained in:
commit
0406eb6b7b
4 changed files with 280 additions and 3 deletions
|
|
@ -54,6 +54,13 @@ TITILER_COG_TILES = f"{TITILER_PUBLIC_BASE}/cog/tiles/WebMercatorQuad/{{z}}/{{x}
|
|||
SENTINEL1_TTL = 20 * 60 # 15–30 min quota-friendly window
|
||||
SENTINEL1_ATTRIBUTION = "Copernicus Sentinel-1 / Microsoft Planetary Computer"
|
||||
|
||||
# GPSJAM (John Wiseman / ADS-B Exchange): daily H3 hexes of aircraft nav
|
||||
# accuracy. Hexes are published as a gzip CSV at a stable per-date URL, soon
|
||||
# after midnight UTC. Red/yellow != proven jamming.
|
||||
GPSJAM_BASE = "https://gpsjam.org"
|
||||
GPSJAM_RES = 4
|
||||
GPSJAM_TTL = 3600.0 # 1h — whole-world layer, fetched once per day effectively
|
||||
|
||||
IEM_NEXRAD = "https://mesonet.agron.iastate.edu/cache/tile.py/1.0.0/nexrad-n0q/{z}/{x}/{y}.png"
|
||||
GIBS_THERMAL = (
|
||||
"https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/"
|
||||
|
|
@ -135,6 +142,12 @@ def overlay_catalog() -> dict:
|
|||
"vessels": {"id": "vessels", "kind": "points", "endpoint": "/api/vessels"},
|
||||
"trains": {"id": "trains", "kind": "points", "endpoint": "/api/trains"},
|
||||
"storms": {"id": "storms", "kind": "points", "endpoint": "/api/storms"},
|
||||
"gpsjam": {
|
||||
"id": "gpsjam",
|
||||
"kind": "geojson",
|
||||
"endpoint": "/api/map/gpsjam",
|
||||
"attribution": "GPSJAM / John Wiseman / ADS-B Exchange",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1289,3 +1302,79 @@ async def fetch_sentinel1(bbox: str) -> dict | None:
|
|||
}
|
||||
|
||||
return await _ttl_get(key, float(SENTINEL1_TTL), _load)
|
||||
# ── GPSJAM ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def gpsjam_level(percent_bad: float) -> str:
|
||||
"""Map a denoised bad-aircraft percentage to GPSJAM's three tiers."""
|
||||
if percent_bad > 10.0:
|
||||
return "high"
|
||||
if percent_bad > 2.0:
|
||||
return "medium"
|
||||
return "low"
|
||||
|
||||
|
||||
def gpsjam_csv_to_geojson(text: str) -> dict:
|
||||
"""Convert a GPSJAM daily CSV to a world FeatureCollection of hex polygons.
|
||||
|
||||
Rows are ``hex,count_good_aircraft,count_bad_aircraft``. The interference
|
||||
percentage uses GPSJAM's published denoise formula
|
||||
``100 * (bad - 1) / (good + bad)``; hexes with zero bad aircraft are the
|
||||
"normal" background and are dropped (the base map already shows nothing).
|
||||
"""
|
||||
import csv
|
||||
import io
|
||||
|
||||
import h3
|
||||
|
||||
features = []
|
||||
for row in csv.DictReader(io.StringIO(text)):
|
||||
hex_id = (row.get("hex") or "").strip()
|
||||
if not hex_id:
|
||||
continue
|
||||
try:
|
||||
good = int(row.get("count_good_aircraft") or 0)
|
||||
bad = int(row.get("count_bad_aircraft") or 0)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if bad < 1:
|
||||
continue
|
||||
denom = good + bad
|
||||
percent = 100.0 * (bad - 1) / denom if denom > 0 else 0.0
|
||||
try:
|
||||
# h3 returns (lat, lng); GeoJSON needs (lng, lat) closed rings.
|
||||
ring = [[lng, lat] for lat, lng in h3.cell_to_boundary(hex_id)]
|
||||
except Exception: # malformed/unknown cell id — skip
|
||||
continue
|
||||
ring.append(ring[0])
|
||||
features.append({
|
||||
"type": "Feature",
|
||||
"geometry": {"type": "Polygon", "coordinates": [ring]},
|
||||
"properties": {
|
||||
"level": gpsjam_level(percent),
|
||||
"percent_bad": round(percent, 2),
|
||||
"good": good,
|
||||
"bad": bad,
|
||||
"hex": hex_id,
|
||||
},
|
||||
})
|
||||
return {"type": "FeatureCollection", "features": features}
|
||||
|
||||
|
||||
async def fetch_gpsjam(date: str) -> dict:
|
||||
"""Fetch + convert one GPSJAM daily hex layer (whole world, 1h TTL)."""
|
||||
url = f"{GPSJAM_BASE}/data/{date}-h3_{GPSJAM_RES}.csv"
|
||||
|
||||
async def _load():
|
||||
if _http is None:
|
||||
async with httpx.AsyncClient(
|
||||
timeout=_HTTP_TIMEOUT, follow_redirects=True, headers=_headers(),
|
||||
) as client:
|
||||
resp = await client.get(url)
|
||||
resp.raise_for_status()
|
||||
return gpsjam_csv_to_geojson(resp.text)
|
||||
resp = await _http.get(url)
|
||||
resp.raise_for_status()
|
||||
return gpsjam_csv_to_geojson(resp.text)
|
||||
|
||||
return await _ttl_get(f"gpsjam:{date}", GPSJAM_TTL, _load)
|
||||
|
|
|
|||
43
app/main.py
43
app/main.py
|
|
@ -14,6 +14,7 @@ from __future__ import annotations
|
|||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from decimal import Decimal
|
||||
|
|
@ -21,6 +22,7 @@ from pathlib import Path
|
|||
from typing import NoReturn
|
||||
from uuid import UUID
|
||||
|
||||
import httpx
|
||||
import structlog
|
||||
from fastapi import BackgroundTasks, FastAPI, HTTPException, Query, WebSocket, WebSocketDisconnect
|
||||
from fastapi.middleware.gzip import GZipMiddleware
|
||||
|
|
@ -53,9 +55,9 @@ from keystore import KeyFormatError, delete_key, list_keys, set_key
|
|||
from settings_store import SettingsError, get_app_settings, list_models, set_summary_model
|
||||
from live_layers import (
|
||||
fetch_aircraft, fetch_fire_incidents, fetch_fire_perimeters,
|
||||
fetch_planespotters_photo, fetch_radar_meta, fetch_sentinel1, fetch_storms,
|
||||
fetch_trains, fetch_vessels, fetch_weather_alerts, overlay_catalog,
|
||||
parse_bbox, UpstreamRateLimited,
|
||||
fetch_gpsjam, fetch_planespotters_photo, fetch_radar_meta, fetch_sentinel1,
|
||||
fetch_storms, fetch_trains, fetch_vessels, fetch_weather_alerts,
|
||||
overlay_catalog, parse_bbox, UpstreamRateLimited,
|
||||
)
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
|
@ -1698,6 +1700,41 @@ async def list_storms():
|
|||
_upstream_or_502(exc, "storms")
|
||||
|
||||
|
||||
_GPSJAM_DATE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
|
||||
|
||||
|
||||
@app.get("/api/map/gpsjam")
|
||||
async def map_gpsjam(
|
||||
date: str | None = Query(None, description="YYYY-MM-DD (default: yesterday UTC)"),
|
||||
):
|
||||
"""GPSJAM daily GPS-interference hex layer (whole world, GeoJSON).
|
||||
|
||||
Red/yellow hexes correlate with suspected jamming but are NOT proof of it.
|
||||
Fetched once per day from gpsjam.org (ADS-B Exchange data) and cached 1h.
|
||||
"""
|
||||
target = date
|
||||
if target is None:
|
||||
target = (datetime.now(timezone.utc) - timedelta(days=1)).strftime("%Y-%m-%d")
|
||||
if not _GPSJAM_DATE.match(target):
|
||||
raise HTTPException(422, "date must be YYYY-MM-DD")
|
||||
try:
|
||||
fc = await fetch_gpsjam(target)
|
||||
except httpx.HTTPStatusError as exc:
|
||||
if exc.response.status_code == 404:
|
||||
return JSONResponse(
|
||||
{"error": "unavailable", "href": "https://gpsjam.org/",
|
||||
"date": target},
|
||||
)
|
||||
_upstream_or_502(exc, "gpsjam")
|
||||
except Exception as exc:
|
||||
_upstream_or_502(exc, "gpsjam")
|
||||
if not fc.get("features"):
|
||||
return JSONResponse(
|
||||
{"error": "unavailable", "href": "https://gpsjam.org/", "date": target},
|
||||
)
|
||||
return overlay_json(fc, 3600)
|
||||
|
||||
|
||||
@app.get("/api/map/times")
|
||||
async def map_layer_times(
|
||||
layer: str = Query(..., description="GIBS layer identifier, e.g. VIIRS_SNPP_CorrectedReflectance_TrueColor"),
|
||||
|
|
|
|||
|
|
@ -12,3 +12,4 @@ python-dateutil>=2.9
|
|||
structlog>=24.4
|
||||
websockets>=14
|
||||
cachetools>=5.5
|
||||
h3>=4.0
|
||||
|
|
|
|||
Loading…
Reference in a new issue