Modern Apps git

← Modern-Apps.git

mamaps: thin junction layer with principled rules

Commit
c59c4a49cb4a4a69204ef3a37b65503c1388ee74
Author
vayun-mathur <[email protected]>
When
2026-10-09 18:41 UTC (1 month ago)
Parents
722f50f

Diff

scripts/maps/mamaps_build/src/schema/junction.rs
diff --git a/scripts/maps/mamaps_build/src/schema/junction.rs b/scripts/maps/mamaps_build/src/schema/junction.rsindex 2a6f0c73d7..2481f744c1 100644--- a/scripts/maps/mamaps_build/src/schema/junction.rs+++ b/scripts/maps/mamaps_build/src/schema/junction.rs@@ -36,6 +36,21 @@ //! Ordinary urban intersections are a heuristic and will need tuning against screenshots. Nothing //! here should be read as knowing where the paint is. //!+//! # Thinning (size)+//!+//! The junction layer is the archive's largest single line item, so three+//! principled rules thin it — predicates, never count caps, so no junction is+//! thinned arbitrarily:+//!+//! * **Arterial gate.** A junction emits nothing unless at least one arm is+//!   [`ARTERIAL_MAX_CLASS`] or better. Residential-only crossings keep their+//!   carriageways; they lose only the lane markings.+//! * **Slight turns at degree 3.** A 20–45° movement at a three-way node is a+//!   road bend, not a turn, and the carriageway already shows the path. Kept+//!   at degree ≥ 4, where a slight exit is a real choice.+//! * **Halved sampling.** Connectors sample at roughly half the old density;+//!   the curve shape is unchanged to the eye at display scale.+//! //! # Both ends of a connector are placed by the same rule, and a lane count is never invented //! //! A connector runs from a lane to a lane, so both ends need a lane index and a lane count. Those@@ -216,6 +231,15 @@ use crate::store::Sink; /// above it. That split is how `roads-lanes` already carries lane detail. pub const MIN_ZOOM: u8 = 14; +/// The largest (least significant) road class that still counts as an arterial.+///+/// Graph edge types share OSM's highway vocabulary as integers+/// (`osm_ingest::tags`: motorway=1 … tertiary=5, unclassified=6,+/// residential=7, …). A junction emits connectors only when at least one arm+/// is this class or better: quiet residential crossings lose lane markings+/// they barely show, while every junction an arterial touches keeps its own.+pub const ARTERIAL_MAX_CLASS: u8 = 5;+ /// Width of one lane, in **Web Mercator metres** — ground metres at the equator, which at any /// other latitude is a smaller ground distance and the same projected one. That is what makes it a /// fixed number of pixels everywhere; see the module docs, where expressing it as ground metres at
scripts/maps/mamaps_build/src/schema/junction_part1.rs
diff --git a/scripts/maps/mamaps_build/src/schema/junction_part1.rs b/scripts/maps/mamaps_build/src/schema/junction_part1.rsindex 157ef80d48..e80746a12e 100644--- a/scripts/maps/mamaps_build/src/schema/junction_part1.rs+++ b/scripts/maps/mamaps_build/src/schema/junction_part1.rs@@ -343,6 +343,10 @@ struct Arm {     /// deliberately the one way to ask, so a count can never be fabricated from something that     /// merely happened to be in scope.     masks: Option<Vec<u16>>,+    /// The edge's road class (`graph.edge_type & ROAD_TYPE_MASK`: motorway=1 …+    /// tertiary=5, unclassified=6, …). Read off the edge, never inferred, so the+    /// arterial gate in `junctions_for_node` is data rather than a second guess.+    road_class: u8, }  /// Resolve one incident edge, or `None` when it is not usable as an arm of this junction.@@ -379,7 +383,8 @@ fn arm(     }     let masks = lanes.and_then(|table| table.masks(idx)).filter(|m| !m.is_empty());     let two_way = graph.has_drivable_twin(to, from).unwrap_or(false);-    Some(Arm { neighbour: if outgoing { to } else { from }, heading, length_m, two_way, masks })+    let road_class = graph.edge_type(idx) & ROAD_TYPE_MASK;+    Some(Arm { neighbour: if outgoing { to } else { from }, heading, length_m, two_way, masks, road_class }) }  /// A lane's centre, in metres right of the road's centreline as seen by traffic travelling this@@ -480,11 +485,15 @@ fn connector(      // A straight-through movement needs two points; a U-turn needs every one of these. Anything     // flatter than the sampling would resolve is dropped by the tiler's simplifier anyway.+    //+    // Sampled at roughly half the historical density: a z14 connector spans+    // tens of metres, so ~5 points resolve its curve at display scale and the+    // rest were coordinate bytes. Minimum 3 keeps a curve a curve.     let sweep = normalise_degrees(exit.heading - approach.heading).abs();     let samples = if turn == Turn::Through && sweep < 3.0 && (in_off - out_off).abs() < 0.05 {         2     } else {-        (4.0 + sweep / 18.0).round().clamp(4.0, 14.0) as usize+        (3.0 + sweep / 36.0).round().clamp(3.0, 7.0) as usize     };      let mut out = Vec::with_capacity(samples);
scripts/maps/mamaps_build/src/schema/junction_part2.rs
diff --git a/scripts/maps/mamaps_build/src/schema/junction_part2.rs b/scripts/maps/mamaps_build/src/schema/junction_part2.rsindex 04af937fde..c712b7ae44 100644--- a/scripts/maps/mamaps_build/src/schema/junction_part2.rs+++ b/scripts/maps/mamaps_build/src/schema/junction_part2.rs@@ -70,6 +70,16 @@ fn junctions_for_node(     if degree.len() < 3 {         return out;     }+    // Thinning rule: a junction with no arterial arm emits nothing. Quiet+    // residential crossings keep their carriageways; they lose only the lane+    // markings, which is where the layer's bytes concentrate.+    if !in_arms+        .iter()+        .chain(&out_arms)+        .any(|a| a.road_class <= super::junction::ARTERIAL_MAX_CLASS)+    {+        return out;+    }      let (tx, ty) = tile_build::geom::project(         f64::from(node.1) * 1e-7,@@ -172,6 +182,14 @@ fn junctions_for_node(             if *turn == Turn::Through {                 continue;             };+            // Thinning rule: a slight (20–45°) movement at a three-way node is+            // a road bend, not a turn — the carriageway already shows the path.+            // Kept at degree 4+, where a slight exit is a real choice.+            if degree.len() == 3+                && matches!(*turn, Turn::SlightLeft | Turn::SlightRight)+            {+                continue;+            }             let out_lanes = effective_lanes(exit);             // This lane's place among the lanes making the same movement into the same exit.             let siblings: Vec<usize> =
scripts/maps/mamaps_build/src/schema/junction_part3.rs
diff --git a/scripts/maps/mamaps_build/src/schema/junction_part3.rs b/scripts/maps/mamaps_build/src/schema/junction_part3.rsindex 86157f6d12..815e8da4e0 100644--- a/scripts/maps/mamaps_build/src/schema/junction_part3.rs+++ b/scripts/maps/mamaps_build/src/schema/junction_part3.rs@@ -4,7 +4,8 @@ mod tests {     use crate::schema::traffic_extra::tests::{write_graph, EdgeSpec, GraphFixture};      /// A four-arm crossroads: node 0 in the middle, nodes 1..4 due north, east, south and west of-    /// it, every arm two-way and residential. Roughly 100 m out on each side, which is a real+    /// it, every arm two-way and primary (arterial, so the arterial gate keeps+    /// the node). Roughly 100 m out on each side, which is a real     /// intersection's scale and comfortably longer than [`SETBACK_M`].     ///     /// Built with the traffic layer's own byte-for-byte v6 writer, so the reader here is exercised@@ -27,7 +28,7 @@ mod tests {             (lat_e7 - 9_000, -1_200_000_000),     // 3: south             (lat_e7, -1_200_000_000 - lon_step_e7), // 4: west         ];-        let edge = |source, target| EdgeSpec { source, target, type_: 7, interior: Vec::new() };+        let edge = |source, target| EdgeSpec { source, target, type_: 3, interior: Vec::new() };         write_graph(             "junction",             &coords,@@ -198,6 +199,7 @@ mod tests {             length_m: 100.0,             two_way,             masks,+            road_class: 7,         };         assert_eq!(             effective_lanes(&arm_with(Some(vec![LANE_LEFT, LANE_THROUGH]), true)),
scripts/maps/mamaps_build/src/schema/junction_part4.rs
diff --git a/scripts/maps/mamaps_build/src/schema/junction_part4.rs b/scripts/maps/mamaps_build/src/schema/junction_part4.rsindex 8956071f1f..4ed422ccb5 100644--- a/scripts/maps/mamaps_build/src/schema/junction_part4.rs+++ b/scripts/maps/mamaps_build/src/schema/junction_part4.rs@@ -83,7 +83,7 @@ mod tests2 {             (350_009_000, -1_199_999_000), // 2: north, the opposing carriageway across the median             (350_000_000, -1_199_988_000), // 3: east, so the node has three neighbours         ];-        let edge = |source, target| EdgeSpec { source, target, type_: 7, interior: Vec::new() };+        let edge = |source, target| EdgeSpec { source, target, type_: 3, interior: Vec::new() };         write_graph(             "junction_divided",             &coords,@@ -140,7 +140,7 @@ mod tests2 {             (350_000_000, -1_199_988_000), // 2: east             (349_991_000, -1_200_000_000), // 3: south         ];-        let edge = |source, target| EdgeSpec { source, target, type_: 7, interior: Vec::new() };+        let edge = |source, target| EdgeSpec { source, target, type_: 3, interior: Vec::new() };         let fixture = write_graph(             "junction_oneway",             &coords,@@ -168,6 +168,75 @@ mod tests2 {         assert_eq!(emitted, 3, "the one-way approach contributes its two turns");     } +    /// A three-way node with one arm 30° off: north, south, and an arm+    /// bearing ~030°. `edge_type` is the caller's to choose, so the arterial+    /// gate and the slight-turn rule can be pinned on identical geometry.+    fn tee_with_slight_arm(edge_type: u8) -> GraphFixture {+        let coords: [(i32, i32); 4] = [+            (350_000_000, -1_200_000_000), // 0: the junction+            (350_009_000, -1_200_000_000), // 1: north, ~100 m+            (349_991_000, -1_200_000_000), // 2: south, ~100 m+            (350_007_779, -1_199_994_517), // 3: ~30° east of north, ~100 m+        ];+        let edge = |source, target| EdgeSpec { source, target, type_: edge_type, interior: Vec::new() };+        write_graph(+            "junction_tee",+            &coords,+            &[+                edge(0, 1),+                edge(0, 2),+                edge(0, 3),+                edge(1, 0),+                edge(2, 0),+                edge(3, 0),+            ],+            &[],+        )+    }++    /// **Slight turns at a degree-3 node are road bends, not turns.**+    ///+    /// From the south approach the 30° arm is a slight right, and from that+    /// arm south is a slight left — both dropped. The sharp pair+    /// (north ↔ 30° arm, ±150°) survives. Two connectors, not four.+    #[test]+    fn a_slight_turn_at_a_three_way_node_is_not_emitted() {+        let (emitted, lines) = stream(&tee_with_slight_arm(3));+        assert_eq!(emitted, 2, "only the sharp pair survives at degree 3");+        assert_eq!(lines.len(), 2);+    }++    /// **The same geometry at degree 4 keeps everything.** The crossroads+    /// fixture's arms leave at 90°, so no movement is slight — but the pin+    /// matters in the other direction: the rule must not fire where it+    /// should not.+    #[test]+    fn a_four_way_node_keeps_all_of_its_turns() {+        let (emitted, _) = stream(&crossroads(&[]));+        assert_eq!(emitted, 8, "four approaches x two turns, none slight");+    }++    /// **A junction no arterial touches emits nothing.** Identical geometry+    /// to the tee above, but every arm residential: the whole node goes+    /// quiet rather than a subset of its movements.+    #[test]+    fn a_junction_with_no_arterial_arm_emits_nothing() {+        let (emitted, lines) = stream(&tee_with_slight_arm(7));+        assert_eq!(emitted, 0, "no arm is tertiary or better");+        assert!(lines.is_empty());+    }++    /// **Connectors sample at roughly half the old density.** A 90° turn+    /// used to walk 9 points (`4 + 90/18`); now it walks 6 (`3 + 90/36`).+    #[test]+    fn a_right_angle_connector_is_sampled_sparsely_but_still_a_curve() {+        let (_, lines) = stream(&crossroads(&[]));+        assert!(!lines.is_empty());+        for line in &lines {+            assert_eq!(line.len(), 6, "every crossroads turn is 90°: 3 + 90/36 rounds to 6");+        }+    }+     /// Left-hand traffic mirrors the connectors rather than leaving them on the wrong side.     #[test]     fn a_left_hand_traffic_country_gets_its_connectors_on_the_other_side() {