Modern Apps git

← Modern-Apps.git

maps: read the baked overlays from their own pmtiles archive

Commit
f84f465c4ef8925b2286d59ca732c988aecf0d1e
Author
vayun-mathur <[email protected]>
When
2026-08-23 21:55 UTC (3 months ago)
Parents
52f1d1b

Diff

maps/src/main/java/com/vayunmathur/maps/ui/MaxspeedOverlay.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MaxspeedOverlay.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MaxspeedOverlay.ktindex db4251929c..fc6b6ccf00 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MaxspeedOverlay.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MaxspeedOverlay.kt@@ -21,15 +21,15 @@ import org.maplibre.spatialk.geojson.Position  * under the puck (see [queryPostedLimit]) and shows the parsed value on the  * speed widget.  *- * The maxspeed data is baked into the v5 PMTiles (P13), so the probe reads the- * same file as the basemap — [MaxspeedSource.PMTILES_URL] delegates to the- * single [com.vayunmathur.maps.util.MapTileCache.BASEMAP_PMTILES_URL] source of- * truth (source-layer [SOURCE_LAYER], feature property [PROP]). Switching to the- * global v5 file is therefore a one-line change in [MapTileCache].+ * The maxspeed data is baked into our overlay PMTiles archive (P13), so the probe+ * reads [MaxspeedSource.PMTILES_URL], which delegates to the single+ * [com.vayunmathur.maps.util.MapTileCache.OVERLAY_PMTILES_URL] source of truth+ * (source-layer [SOURCE_LAYER], feature property [PROP]). Switching to the global+ * overlay file is therefore a one-line change in [MapTileCache].  */ object MaxspeedSource {-    /** Baked into the v5 basemap PMTiles — read from the shared base URL. */-    val PMTILES_URL: String get() = com.vayunmathur.maps.util.MapTileCache.BASEMAP_PMTILES_URL+    /** Baked into the overlay PMTiles — read from the shared overlay URL. */+    val PMTILES_URL: String get() = com.vayunmathur.maps.util.MapTileCache.OVERLAY_PMTILES_URL     const val LAYER_ID: String = "maxspeed-probe"     const val SOURCE_LAYER: String = "maxspeed"     const val PROP: String = "maxspeed"
maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.ktindex 33ff56b0cc..9cb4f2b28d 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt@@ -98,11 +98,11 @@ fun MyMapLayers(         var routeSource by remember { mutableStateOf<GeoJsonSource?>(null) }         var userSource by remember { mutableStateOf<GeoJsonSource?>(null) } -        // Admin borders (country/region/city) and the OSM transit lines are baked-        // into the v5 basemap PMTiles (P13/P22), replacing the old admin0/admin1-        // FlatGeobuf assets. This one vector source feeds the search/selection-        // highlight below AND the transit-lines overlay.-        val adminSource = rememberVectorSource(MapTileCache.BASEMAP_PMTILES_URL)+        // Every one of our baked overlays lives in the overlay PMTiles archive+        // (P13/P22/P27) rather than the base: admin borders, transit lines, POIs and+        // GTFS stops. One vector source feeds all of them, which is enough because+        // they are all layers of the same file.+        val overlaySource = rememberVectorSource(MapTileCache.OVERLAY_PMTILES_URL)          LaunchedEffect(Unit) {             userSource = GeoJsonSource(@@ -153,23 +153,20 @@ fun MyMapLayers(         SafetyLayer(safetyEnabled)          // P22: OSM transit-lines overlay — rail/subway/light_rail/tram/monorail-        // baked into the v5 basemap PMTiles as the `transit_lines` source-layer.+        // baked into the overlay PMTiles as the `transit_lines` source-layer.         // Shown with the P6 "Transit" toggle (alongside the P10 Transitous-        // stops). Harmless no-op while the layer is absent (until v5 is-        // regenerated with it).+        // stops). Harmless no-op while the layer is absent.         if (transitEnabled) {-            TransitLinesLayer(adminSource, tokens)+            TransitLinesLayer(overlaySource, tokens)         }          // Ambient POI overlay (P29): rendered NATIVELY from the baked `ma_pois`-        // PMTiles source-layer (P27) via the EXISTING single vector source-        // (`adminSource`) — the same one the admin/transit overlays reuse. This-        // avoids the second-source "PMTiles directory" parse error that broke the-        // original overlay, and it removes the slow per-viewport offline-index-        // GeoJSON pins path. Google is still hit only on tap for rich details-        // (see MapPage.onMapClick -> toSelectedMaPoi). A non-null [poiFilterTypes]-        // (set by tapping a category chip) filters the layer to those OSM types.-        MaPoisLayer(adminSource, poiFilterTypes)+        // source-layer (P27) via the shared [overlaySource]. Replaces the slow+        // per-viewport offline-index GeoJSON pins path. Google is still hit only on+        // tap for rich details (see MapPage.onMapClick -> toSelectedMaPoi). A+        // non-null [poiFilterTypes] (set by tapping a category chip) filters the+        // layer to those OSM types.+        MaPoisLayer(overlaySource, poiFilterTypes)          // Saved-place pins (Home / Work / starred list). Tap re-selects the         // place → PlaceSheet (Vela's SavedPin).@@ -185,9 +182,7 @@ fun MyMapLayers(         if (transitEnabled) {             // Baked GTFS stop pins (P10). Shown only when the Transit layer is on;             // tap a stop → live departure board (handled in MapPage.onMapClick).-            // Same shared source as the admin/POI overlays — a second VectorSource-            // on the same PMTiles triggers a directory parse error.-            TransitStopsLayer(adminSource)+            TransitStopsLayer(overlaySource)         }          // Posted-speed-limit probe overlay (Decision D4). Invisible; queried@@ -238,15 +233,15 @@ fun MyMapLayers(                 is SpecificFeature.Admin0Label ->                     // Country highlight: filter the v5 admin_country layer by                     // ISO_A2 (the key CountryMap.getAdmin0 matched on the FGB).-                    AdminHighlight(adminSource, "admin_country", "ISO_A2", selectedFeature.iso)+                    AdminHighlight(overlaySource, "admin_country", "ISO_A2", selectedFeature.iso)                 is SpecificFeature.Admin1Label ->                     // Region/state highlight: filter admin_region by iso_3166_2                     // (the key CountryMap.getAdmin1 matched on the FGB).-                    AdminHighlight(adminSource, "admin_region", "iso_3166_2", selectedFeature.iso)+                    AdminHighlight(overlaySource, "admin_region", "iso_3166_2", selectedFeature.iso)                 is SpecificFeature.Admin2Label ->                     // City highlight: admin_city carries no ISO code, so match the                     // English name — the same value parse() read off the label.-                    AdminHighlight(adminSource, "admin_city", "name_en", selectedFeature.name)+                    AdminHighlight(overlaySource, "admin_city", "name_en", selectedFeature.name)                 is SpecificFeature.Route -> {                     if (route != null) {                         LaunchedEffect(@@ -280,7 +275,7 @@ fun MyMapLayers(  /**  * OSM transit-lines overlay (P22-APP): draws the `transit_lines` source-layer- * baked into the v5 basemap PMTiles by the generator. Each feature carries+ * baked into the overlay PMTiles archive by the generator. Each feature carries  * `kind` (rail/subway/light_rail/tram/monorail/train), plus optional `name`,  * `ref` and `colour`. Lines are colored by the feature's own `colour` when the  * generator provides one, otherwise by a per-[kind] palette. Rendered only when
maps/src/main/java/com/vayunmathur/maps/ui/SafetyLayer.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/SafetyLayer.kt b/maps/src/main/java/com/vayunmathur/maps/ui/SafetyLayer.ktindex 17f166aed2..fba91b5e43 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/SafetyLayer.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/SafetyLayer.kt@@ -19,18 +19,18 @@ import org.maplibre.compose.sources.rememberVectorSource import org.maplibre.compose.util.MaplibreComposable  /**- * Safety / road-furniture data source (P13). The features are baked into the v5- * basemap PMTiles (source-layer [SOURCE_LAYER]), so the overlay reads the same- * [MapTileCache.BASEMAP_PMTILES_URL] as everything else — switching to the- * global v5 file is a one-line change in [MapTileCache].+ * Safety / road-furniture data source (P13). The features are baked into the+ * overlay PMTiles archive (source-layer [SOURCE_LAYER]), so the overlay reads+ * [MapTileCache.OVERLAY_PMTILES_URL] — switching to the global overlay file is a+ * one-line change in [MapTileCache].  *  * Each `safety` feature carries a `kind` in [KIND_SPEED_CAMERA],  * [KIND_ALPR], [KIND_SURVEILLANCE], [KIND_STOP_SIGN], [KIND_TRAFFIC_SIGNALS],  * which drives the category icon in [SafetyLayer].  */ object SafetyLayersSource {-    /** Baked into the v5 basemap PMTiles — read from the shared base URL. */-    val PMTILES_URL: String get() = MapTileCache.BASEMAP_PMTILES_URL+    /** Baked into the overlay PMTiles — read from the shared overlay URL. */+    val PMTILES_URL: String get() = MapTileCache.OVERLAY_PMTILES_URL     const val SOURCE_LAYER: String = "safety"     const val LAYER_ID: String = "safety-icons" @@ -45,7 +45,7 @@ object SafetyLayersSource {  /**  * Mount the safety-layers overlay when [enabled] (the P6 LayersSheet toggle) and- * the v5 tileset exists. A single [SymbolLayer] over the `safety` vector source+ * the overlay tileset exists. A single [SymbolLayer] over the `safety` vector source  * draws a data-driven category icon per feature `kind` (speed camera / ALPR /  * surveillance / stop sign / traffic signals). No-op while browsing with the  * toggle off.
maps/src/main/java/com/vayunmathur/maps/ui/TransitStopsLayer.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/TransitStopsLayer.kt b/maps/src/main/java/com/vayunmathur/maps/ui/TransitStopsLayer.ktindex 95a0873dfa..216bd34b0a 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/TransitStopsLayer.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/TransitStopsLayer.kt@@ -31,7 +31,7 @@ private const val TRANSIT_STOP_DOT_LAYER_ID = "transit-stop-dots"  /**  * Baked GTFS stop layer. Placement, name, `motis_id` and `route_type` come from- * the `transit_stops` source-layer in the v5 basemap PMTiles, written by+ * the `transit_stops` source-layer in the overlay PMTiles archive, written by  * `scripts/maps/build_transit_stops_layer.sh`.  *  * This replaced a per-viewport `GET /api/v1/map/stops` fetch. Stops are static@@ -55,9 +55,8 @@ private val TRANSIT_FERRY_COLOR = Color(0xFF0277BD)  /**  * Draw the `transit_stops` source-layer: a [CircleLayer] dot with a [SymbolLayer]- * glyph on top, from z10 up. Takes the SAME shared [VectorSource] the admin and- * POI overlays use — a second source on the same PMTiles triggers a directory- * parse error (see the note at [MaPoisLayer]).+ * glyph on top, from z10 up. Takes the shared overlay [VectorSource] the admin and+ * POI overlays also read, since they are all layers of the same archive.  *  * The marker is a runtime Canvas bitmap rather than a sprite asset, so it renders  * even while the style's remote glyphs 404; [MaPoisLayer] does the same.
maps/src/main/java/com/vayunmathur/maps/ui/map/style/StylePatcher.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/map/style/StylePatcher.kt b/maps/src/main/java/com/vayunmathur/maps/ui/map/style/StylePatcher.ktindex c8cf915489..26646a0962 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/map/style/StylePatcher.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/map/style/StylePatcher.kt@@ -12,8 +12,11 @@ import kotlinx.serialization.json.put import kotlinx.serialization.json.putJsonObject  /**- * Native basemap layers suppressed at runtime — amenities are Google-only now- * (custom overlay layer). Keeping this in code (vs editing style.json) makes it+ * Native basemap layers suppressed at runtime.+ *+ * The base archive still carries Protomaps' own `pois`, but we draw POIs ourselves+ * from the baked `ma_pois` overlay (see MaPoisLayer), so leaving the base layer on+ * would double every pin. Keeping this in code (vs editing style.json) makes it  * OTA-swappable per Decision D1.  */ private val SUPPRESSED_LAYERS = setOf("pois")@@ -37,6 +40,11 @@ private const val PATCHED_MARKER = "ma:patched"  * colour keys are swapped for [BasemapPalette] values, so we do not carry a second copy of a  * 3544-line style file.  *+ * Neither source declares a `maxzoom`. It used to: the merged v5 archive unioned its inputs'+ * zoom ranges, so it advertised the overlays' z16 while its base tiles stopped at z15, and base+ * layers vanished at max zoom. The overlays now live in their own archive, so the base+ * advertises its own true maxzoom and MapLibre overzooms past it on its own.+ *  * Returns [jsonString] unchanged if it has already been patched. A theme flip re-patches from  * the *original* asset, not from the previous output.  */@@ -57,17 +65,10 @@ fun patchStyleForHybrid(         putJsonObject("protomaps_base") {             put("type", "vector")             put("url", baseLocalUrl)-            // The v5-ca base data stops at z15 but the merged pmtiles advertise-            // maxzoom 16, so base layers vanish at z16 while the separate overlay-            // sources (safety/maxspeed/transit_lines/admin) keep rendering. Cap the-            // source at 15 so MapLibre OVERZOOMS z15 tiles past z15, keeping the-            // base visible at max zoom.-            put("maxzoom", 15)         }         putJsonObject("protomaps_hybrid") {             put("type", "vector")             put("url", hybridUrl)-            put("maxzoom", 15)         }     } @@ -78,10 +79,11 @@ fun patchStyleForHybrid(             val id = layer["id"]?.jsonPrimitive?.content ?: ""             val type = layer["type"]?.jsonPrimitive?.content ?: "" -            // Suppress native basemap POIs at runtime (Decision D1) — amenities-            // are Google-only now, rendered on the custom overlay layer. Dropping-            // the source layer here (rather than editing style.json) keeps it-            // OTA-swappable. Also drops the would-be _base/_hybrid variants.+            // Suppress the base archive's own POIs at runtime (Decision D1): we draw+            // them from the baked `ma_pois` overlay instead, so keeping both would+            // double every pin. Dropping the source layer here (rather than editing+            // style.json) keeps it OTA-swappable. Also drops the would-be+            // _base/_hybrid variants.             if (id in SUPPRESSED_LAYERS) return@forEach              // Dark palette (P14): recolor the base Protomaps paint at runtime so
maps/src/main/java/com/vayunmathur/maps/util/MapTileCache.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/MapTileCache.kt b/maps/src/main/java/com/vayunmathur/maps/util/MapTileCache.ktindex 6ab0b3ed94..4a77470f18 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/MapTileCache.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/MapTileCache.kt@@ -36,8 +36,9 @@ import javax.net.ssl.SSLException /**  * Disk cache for the streamed protomaps basemap tiles.  *- * The basemap is streamed live from [BASEMAP_PMTILES_URL] via pmtiles-over-HTTP- * range requests. MapLibre routes every HTTP resource load through the+ * The basemap is streamed live from [BASEMAP_PMTILES_URL], and our overlays from+ * [OVERLAY_PMTILES_URL], via pmtiles-over-HTTP range requests. MapLibre routes+ * every HTTP resource load through the  * [HttpRequest] produced by its [ModuleProvider]; we install our own provider  * via [MapLibre.setModuleProvider] so the whole map stack runs on  * `library:network` (HttpURLConnection) instead of MapLibre's bundled OkHttp@@ -56,18 +57,28 @@ object MapTileCache {     /**      * The single source of truth for the streamed basemap PMTiles URL.      *-     * v5 (P13) bakes the safety/road-furniture and admin border layers into the-     * same file as the base schema, so the base style source, the maxspeed probe-     * ([com.vayunmathur.maps.ui.MaxspeedSource]), the safety overlay-     * ([com.vayunmathur.maps.ui.SafetyLayersSource]) and the admin-     * search-highlight all read from THIS one URL.-     *-     * Currently the California test build (`v5-ca.pmtiles`, hosted + verified).-     * To ship globally, swap this one line to `v5.pmtiles` once the planet build-     * is hosted (same pipeline, bigger bbox) — nothing else needs to change.+     * Base schema ONLY — the Protomaps layers `style.json` draws. Our own overlays+     * live in [OVERLAY_PMTILES_URL], a separate archive, because at planet scale the+     * base is ~127 GB and the overlays are a couple of GB: joining them would make+     * the merge impossible for the sake of a file 97% of which never changes.+     * See `scripts/maps/build_v5_pmtiles.sh --no-base`.      */     const val BASEMAP_PMTILES_URL =-        "pmtiles://https://data.vayunmathur.com/v5-ca.pmtiles"+        "pmtiles://https://data.vayunmathur.com/v4.pmtiles"++    /**+     * Our overlay archive: `safety`, `maxspeed`, `transit_lines`, `ma_pois`,+     * `transit_stops` and the three `admin_*` levels, with no base layers.+     *+     * Read by the maxspeed probe ([com.vayunmathur.maps.ui.MaxspeedSource]), the+     * safety overlay ([com.vayunmathur.maps.ui.SafetyLayersSource]), the ambient POI+     * layer, the transit line/stop overlays and the admin search-highlight.+     *+     * Currently the California test build (`v5-ca-overlay.pmtiles`). Shipping+     * globally is a one-line change here once the planet overlay build is hosted.+     */+    const val OVERLAY_PMTILES_URL =+        "pmtiles://https://data.vayunmathur.com/v5-ca-overlay.pmtiles"      internal const val TILE_HOST = "data.vayunmathur.com"     private val REFRESH_INTERVAL_MS = 24.hours.inWholeMilliseconds@@ -77,14 +88,20 @@ object MapTileCache {     internal const val TAG = "MapTileCache"      /**-     * Marker written into the cache dir. Entries are wiped when it changes, so-     * it carries the origin host, a format revision, AND the exact pmtiles URL:-     * if [BASEMAP_PMTILES_URL] is repointed (or its bytes are regenerated under-     * the same name), every cached range keyed off the old file is dropped so we-     * can never serve a stale/short chunk from a previous build. v3 also only-     * ever stores validated 206 partials (see [CachingHttpRequest.load]).+     * Marker written into the cache dir. Entries are wiped when it changes, so it+     * carries the origin host, a format revision, AND both pmtiles URLs: if either+     * is repointed (or its bytes are regenerated under the same name), every cached+     * range keyed off the old files is dropped so we can never serve a stale/short+     * chunk from a previous build. v3 also only ever stores validated 206 partials+     * (see [CachingHttpRequest.load]).+     *+     * BOTH urls have to be in here. The overlays are the half that gets rebuilt —+     * the base is republished almost never — so a marker naming only the basemap+     * would leave a republished overlay serving byte ranges from the previous+     * build's directory, which is the exact failure this marker exists to prevent.      */-    private const val CACHE_ORIGIN = "$TILE_HOST/v3/$BASEMAP_PMTILES_URL"+    private const val CACHE_ORIGIN =+        "$TILE_HOST/v3/$BASEMAP_PMTILES_URL|$OVERLAY_PMTILES_URL"      @Volatile private var installed = false 
maps/src/test/java/com/vayunmathur/maps/ui/map/style/StylePatcherTest.kt
diff --git a/maps/src/test/java/com/vayunmathur/maps/ui/map/style/StylePatcherTest.kt b/maps/src/test/java/com/vayunmathur/maps/ui/map/style/StylePatcherTest.ktindex f7c42f0fe1..cb98be1626 100644--- a/maps/src/test/java/com/vayunmathur/maps/ui/map/style/StylePatcherTest.kt+++ b/maps/src/test/java/com/vayunmathur/maps/ui/map/style/StylePatcherTest.kt@@ -112,10 +112,11 @@ class StylePatcherTest {     fun `the original sources are replaced, not merged`() {         val sources = patch()["sources"]!!.jsonObject         assertEquals(setOf("protomaps_base", "protomaps_hybrid"), sources.keys)-        // Both capped at 15: the base data stops there while the merged archive advertises 16,-        // so without the cap base layers vanish at max zoom instead of overzooming.+        // No maxzoom cap: the base archive advertises its own, so MapLibre overzooms+        // past it. The cap only existed to correct a merged archive that claimed the+        // overlays' z16 while its base tiles stopped at z15.         for (name in sources.keys) {-            assertEquals(15, sources[name]!!.jsonObject["maxzoom"]!!.jsonPrimitive.content.toInt())+            assertNull(sources[name]!!.jsonObject["maxzoom"], "$name must not cap zoom")         }     }