feat(sentinel1): Sentinel-1 SAR STAC -> self-hosted TiTiler tile template

Add GET /api/map/sentinel1?bbox=... which queries Planetary Computer STAC
(sentinel-1-grd, last 7d, most recent), signs the vv/hh COG with a SAS token,
and returns a same-origin /titiler/... XYZ tile template. Cache keyed on
quantized bbox + UTC day (20 min TTL). 429 -> 429 (Retry-After), no imagery
-> 404, other upstream errors -> 502.

Self-host TiTiler on the Pi (ghcr.io/developmentseed/titiler, arm64, 1G cap,
host loopback 8001 -> container 8000) instead of titiler.xyz. nginx
/titiler/ proxy snippet routes browser tiles to it; TITILER_PUBLIC_BASE and
TITILER_INTERNAL_URL are env-driven (no hardcoded hostnames).
This commit is contained in:
Sirius DevOps 2026-08-29 01:01:09 -04:00
commit 5212aaa5cd
6 changed files with 346 additions and 5 deletions

View file

@ -8,6 +8,8 @@ from live_layers import (
parse_bbox,
quantize_bbox,
rainviewer_tile_url,
sign_cog_url,
sentinel1_tile_url,
slim_alert_properties,
to_marker,
transform_adsb_lol,
@ -15,6 +17,8 @@ from live_layers import (
transform_amtraker,
transform_nhc_storms,
transform_wfigs_incidents,
SENTINEL1_ATTRIBUTION,
TITILER_COG_TILES,
_cache,
_ttl_get,
_wfigs_params,
@ -652,3 +656,129 @@ def test_planespotters_headers_add_contact_when_ua_is_generic(monkeypatch):
ua = live_layers._planespotters_headers()["User-Agent"]
assert "osint-dashboard" in ua
assert "@" in ua
# ── Sentinel-1 SAR (Planetary Computer STAC → signed COG template) ────────
def test_sign_cog_url_appends_token():
assert sign_cog_url("https://blob.example/x.tif", "tok=abc") == \
"https://blob.example/x.tif?token=tok%3Dabc"
# Existing query string → append with &
assert sign_cog_url("https://blob.example/x.tif?st=1", "tok") == \
"https://blob.example/x.tif?st=1&token=tok"
def test_sentinel1_tile_url_contains_titiler_rescale_and_cfastie():
signed = "https://blob.example/x.tif?token=secret"
url = sentinel1_tile_url(signed)
assert url.startswith(TITILER_COG_TILES + "?")
assert "WebMercatorQuad/{z}/{x}/{y}@1x" in url
assert "url=https%3A%2F%2Fblob.example%2Fx.tif%3Ftoken%3Dsecret" in url
assert "rescale=0%2C500" in url
assert "colormap_name=cfastie" in url
def test_sentinel1_tile_url_is_same_origin_relative():
# Self-hosted TiTiler: the browser must hit the Pi's nginx vhost, not
# titiler.xyz or a raw host:port. The template is a root-relative path.
url = sentinel1_tile_url("https://blob.example/x.tif")
assert url.startswith("/titiler/cog/tiles/WebMercatorQuad/")
assert "://" not in url
assert "titiler.xyz" not in url
def _stac_feature(assets: dict) -> dict:
return {
"type": "Feature",
"id": "S1A_IW_GRDH_1SDV_20240820T000000",
"properties": {"datetime": "2024-08-20T00:00:00Z"},
"assets": assets,
}
def test_fetch_sentinel1_vv_signed_tile_url(monkeypatch):
import asyncio
from live_layers import fetch_sentinel1, _cache
calls = []
async def fake_post(url, json=None, headers=None):
calls.append(("post", url, json))
return {"features": [_stac_feature({
"vv": {"href": "https://blob.example/grd-vv.tif"},
})]}
async def fake_get(url, params=None, headers=None):
calls.append(("get", url))
return {"token": "sig=abc123"}
monkeypatch.setattr("live_layers._post_json", fake_post)
monkeypatch.setattr("live_layers._get_json", fake_get)
_cache.clear()
out = asyncio.run(fetch_sentinel1("-80,35,-79,36"))
assert out["id"] == "sentinel-1-sar"
assert out["kind"] == "raster"
assert out["polarization"] == "vv"
assert out["opacity"] == 0.8
assert out["itemId"].startswith("S1A")
assert out["attribution"] == SENTINEL1_ATTRIBUTION
assert "WebMercatorQuad/{z}/{x}/{y}@1x" in out["tileUrl"]
assert "rescale=0%2C500" in out["tileUrl"]
assert "colormap_name=cfastie" in out["tileUrl"]
# SAS token "sig=abc123" is signed as ?token=sig%3Dabc123, then the whole
# COG URL is percent-encoded again as a query param (=> sig%253Dabc123).
assert "sig%253Dabc123" in out["tileUrl"]
# STAC search payload shape
post_url, post_json = calls[0][1], calls[0][2]
assert post_url.endswith("/api/stac/v1/search")
assert post_json["collections"] == ["sentinel-1-grd"]
assert post_json["limit"] == 1
assert post_json["sortby"][0]["direction"] == "desc"
def test_fetch_sentinel1_uses_hh_when_vv_missing(monkeypatch):
import asyncio
from live_layers import fetch_sentinel1, _cache
async def fake_post(url, json=None, headers=None):
return {"features": [_stac_feature({
"hh": {"href": "https://blob.example/grd-hh.tif"},
})]}
async def fake_get(url, params=None, headers=None):
return {"token": "tok"}
monkeypatch.setattr("live_layers._post_json", fake_post)
monkeypatch.setattr("live_layers._get_json", fake_get)
_cache.clear()
out = asyncio.run(fetch_sentinel1("-80,35,-79,36"))
assert out["polarization"] == "hh"
assert "url=https%3A%2F%2Fblob.example%2Fgrd-hh.tif" in out["tileUrl"]
def test_fetch_sentinel1_none_on_empty_features(monkeypatch):
import asyncio
from live_layers import fetch_sentinel1, _cache
async def fake_post(url, json=None, headers=None):
return {"features": []}
monkeypatch.setattr("live_layers._post_json", fake_post)
_cache.clear()
assert asyncio.run(fetch_sentinel1("-80,35,-79,36")) is None
def test_fetch_sentinel1_none_when_no_vv_or_hh(monkeypatch):
import asyncio
from live_layers import fetch_sentinel1, _cache
async def fake_post(url, json=None, headers=None):
return {"features": [_stac_feature({"thumbnail": {"href": "https://x"}})]}
monkeypatch.setattr("live_layers._post_json", fake_post)
_cache.clear()
assert asyncio.run(fetch_sentinel1("-80,35,-79,36")) is None