feat(gpsjam): GPS interference hex overlay API (GPSJAM/ADS-B Exchange)

GET /api/map/gpsjam?date=YYYY-MM-DD (default yesterday UTC) fetches the
daily gpsjam.org H3 resolution-4 CSV, converts hexes to GeoJSON polygons,
and tags each with level low|medium|high (0-2% / 2-10% / >10%) via GPSJAM's
denoise formula. Whole world once, 1h TTL, graceful unavailable fallback.

- overlay_catalog: add kind=geojson gpsjam stub
- tests: mocked-HTTP unit + API contract (9 new)
This commit is contained in:
Sirius DevOps 2026-08-29 14:14:08 -04:00
commit 42ca6295f0
4 changed files with 280 additions and 3 deletions

View file

@ -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",
},
}
@ -1283,3 +1296,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)