maps P5b: maxspeed posted-limit source
- Commit
54e187e2f2d86c2d6f0e3e52e934fe0b9e428e41- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-16 23:27 UTC (3 months ago)
- Parents
db731e3
Diff
maps/src/main/java/com/vayunmathur/maps/data/OsmMaxspeed.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/data/OsmMaxspeed.kt b/maps/src/main/java/com/vayunmathur/maps/data/OsmMaxspeed.ktnew file mode 100644index 0000000000..0bf1e2eb2e--- /dev/null+++ b/maps/src/main/java/com/vayunmathur/maps/data/OsmMaxspeed.kt@@ -0,0 +1,78 @@+package com.vayunmathur.maps.data++import kotlin.math.roundToInt++/**+ * A posted speed limit resolved from an OSM `maxspeed` tag value.+ *+ * Both unit variants are precomputed so the driving UI can render the badge in+ * whichever unit matches the speedometer without re-parsing. [displayIsMph]+ * reflects the unit the tag was authored in (US/UK data is mph, most of the+ * world is km/h) and is the sensible default display unit.+ */+data class PostedLimit(val kmh: Int, val mph: Int, val displayIsMph: Boolean)++private const val MPH_TO_KMH = 1.609344++/**+ * Parse an OSM `maxspeed` tag value into a [PostedLimit], or `null` when the+ * value carries no concrete numeric limit (`none`, `signals`, `walk`,+ * unrecognised country schemes, blank, …).+ *+ * Handles the common forms: bare `"50"` (km/h), `"50 mph"`, `"30 km/h"`, and a+ * subset of implicit country schemes like `"DE:urban"`. Faithful to Vela's+ * `OsmMaxspeed` behaviour: unknown/relative values degrade to `null` rather+ * than guessing.+ */+fun parseMaxspeed(raw: String?): PostedLimit? {+ if (raw.isNullOrBlank()) return null+ val s = raw.trim().lowercase()++ countryImplicitKmh[s]?.let { return fromKmh(it) }++ when (s) {+ "none", "signals", "variable", "walk", "unposted" -> return null+ }++ Regex("""(\d+(?:\.\d+)?)\s*mph""").find(s)?.let { m ->+ val v = m.groupValues[1].toDouble().roundToInt()+ if (v <= 0) return null+ return PostedLimit(kmh = (v * MPH_TO_KMH).roundToInt(), mph = v, displayIsMph = true)+ }++ // Bare number or explicit km/h ("50", "50 km/h", "50kmh").+ val num = Regex("""(\d+(?:\.\d+)?)""").find(s) ?: return null+ val v = num.groupValues[1].toDouble().roundToInt()+ if (v <= 0) return null+ return fromKmh(v)+}++private fun fromKmh(kmh: Int): PostedLimit =+ PostedLimit(kmh = kmh, mph = (kmh / MPH_TO_KMH).roundToInt(), displayIsMph = false)++/**+ * A small subset of OSM implicit country/context maxspeed schemes. Values are+ * approximate legal defaults in km/h. Kept intentionally small — anything not+ * listed returns `null` (no badge) rather than an inaccurate guess.+ */+private val countryImplicitKmh: Map<String, Int> = mapOf(+ "de:living_street" to 7,+ "de:urban" to 50,+ "de:rural" to 100,+ "de:motorway" to 130,+ "gb:nsl_single" to 97, // 60 mph+ "gb:nsl_dual" to 113, // 70 mph+ "gb:motorway" to 113, // 70 mph+ "fr:urban" to 50,+ "fr:rural" to 80,+ "fr:motorway" to 130,+ "it:urban" to 50,+ "it:rural" to 90,+ "it:motorway" to 130,+ "at:urban" to 50,+ "at:rural" to 100,+ "at:motorway" to 130,+ "ch:urban" to 50,+ "ch:rural" to 80,+ "ch:motorway" to 120,+)
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.ktnew file mode 100644index 0000000000..62cbb6f56e--- /dev/null+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MaxspeedOverlay.kt@@ -0,0 +1,74 @@+package com.vayunmathur.maps.ui++import androidx.compose.runtime.Composable+import androidx.compose.ui.unit.dp+import com.vayunmathur.maps.data.PostedLimit+import com.vayunmathur.maps.data.parseMaxspeed+import kotlinx.serialization.json.JsonPrimitive+import org.maplibre.compose.camera.CameraProjection+import org.maplibre.compose.expressions.dsl.const+import org.maplibre.compose.layers.LineLayer+import org.maplibre.compose.sources.rememberVectorSource+import org.maplibre.compose.util.MaplibreComposable+import org.maplibre.spatialk.geojson.Position++/**+ * Posted-speed-limit data source (Decision D4), ported from Vela's maxspeed+ * PMTiles overlay (`VelaMapView.kt:967-1003` + `OsmMaxspeed.kt`).+ *+ * A vector overlay whose features carry the OSM `maxspeed` tag is mounted as an+ * invisible probe layer; the driving UI queries it with `queryRenderedFeatures`+ * under the puck (see [queryPostedLimit]) and shows the parsed value on the+ * speed widget.+ *+ * ⚠️ INERT UNTIL HOSTED: [PMTILES_URL] is intentionally blank. The maxspeed+ * tileset is not yet hosted on `data.vayunmathur.com`, and the Vela clone that+ * held the original hosted URL was removed after research (see+ * `maps/VELA_PORT_PLAN.md`). When blank, the probe layer is not mounted and+ * [queryPostedLimit] returns `null`, so no badge is shown. To activate: host a+ * `maxspeed.pmtiles` (source-layer [SOURCE_LAYER], feature property [PROP]) and+ * set [PMTILES_URL] to e.g. `pmtiles://https://data.vayunmathur.com/maxspeed.pmtiles`.+ */+object MaxspeedSource {+ const val PMTILES_URL: String = ""+ const val LAYER_ID: String = "maxspeed-probe"+ const val SOURCE_LAYER: String = "maxspeed"+ const val PROP: String = "maxspeed"++ val enabled: Boolean get() = PMTILES_URL.isNotBlank()+}++/**+ * Mount the maxspeed probe overlay. No-op (renders nothing) while the tileset+ * is unhosted, so it is always safe to include in the layer tree.+ */+@Composable+@MaplibreComposable+fun MaxspeedLayer() {+ if (!MaxspeedSource.enabled) return+ val source = rememberVectorSource(MaxspeedSource.PMTILES_URL)+ // Invisible, but rendered so queryRenderedFeatures can hit it under the puck.+ LineLayer(+ MaxspeedSource.LAYER_ID,+ source,+ sourceLayer = MaxspeedSource.SOURCE_LAYER,+ opacity = const(0f),+ width = const(2.dp),+ )+}++/**+ * Query the posted speed limit at [puck] (the snapped nav position) by+ * projecting it to screen and hit-testing the maxspeed probe layer. Returns+ * `null` when the overlay is unhosted, off-network, or the road has no+ * concrete `maxspeed` tag.+ */+fun queryPostedLimit(projection: CameraProjection, puck: Position): PostedLimit? {+ if (!MaxspeedSource.enabled) return null+ val offset = projection.screenLocationFromPosition(puck)+ val features = projection.queryRenderedFeatures(offset, setOf(MaxspeedSource.LAYER_ID))+ val raw = features.firstNotNullOfOrNull { f ->+ (f.properties?.get(MaxspeedSource.PROP) as? JsonPrimitive)?.content+ }+ return parseMaxspeed(raw)+}
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 90ee9bb4cf..c51e93b8c5 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt@@ -144,6 +144,10 @@ fun MyMapLayers( // Rendered above traffic but below the user puck. GooglePoiLayer(googlePins) + // Posted-speed-limit probe overlay (Decision D4). Invisible; queried+ // under the puck during navigation. No-op until the tileset is hosted.+ MaxspeedLayer()+ // Search-result pins (Vela MARKERS_LAYER analog) — drawn above the // ambient POI overlay so a query's hits stand out; tap re-selects. SearchResultLayer(searchResults)