Modern Apps git

← Modern-Apps.git

maps: route long walks and bike rides over the highway overlay

Commit
299decadc366b9fc02cb1712a2e2a748dc653923
Author
vayun-mathur <[email protected]>
When
2026-10-25 23:51 UTC (1 month ago)
Parents
5fccc62

Diff

maps/src/main/rust/src/graph.rs
diff --git a/maps/src/main/rust/src/graph.rs b/maps/src/main/rust/src/graph.rsindex 1425748a6f..def76661f7 100644--- a/maps/src/main/rust/src/graph.rs+++ b/maps/src/main/rust/src/graph.rs@@ -52,6 +52,10 @@ pub const INVALID_EDGE: u64 = 0xFFFF_FFFF_FFFF_FFFF;  pub const WALK_SPEED_M_S: f64 = 4.5 / 3.6; pub const BICYCLE_SPEED_M_S: f64 = 16.0 / 3.6;+/// How many times slower a motorway counts when walking or cycling. A penalty+/// rather than a ban keeps the overlay connected where a freeway shoulder is the+/// only way through, and slowing an edge never breaks the A* heuristic.+pub const ACTIVE_MOTORWAY_PENALTY: f64 = 5.0; /// Upper bound on any driving edge's effective speed (km/h). The A* heuristic is /// scaled to this speed AND every edge's effective speed is clamped to it in /// [`crate::geometry::get_edge_time_10ms`], which together keep the heuristic
maps/src/main/rust/src/graph_overlay.rs
diff --git a/maps/src/main/rust/src/graph_overlay.rs b/maps/src/main/rust/src/graph_overlay.rsindex d9c919438c..3e873adf03 100644--- a/maps/src/main/rust/src/graph_overlay.rs+++ b/maps/src/main/rust/src/graph_overlay.rs@@ -370,10 +370,6 @@ mod overlay_tests {     /// A ~220 km motorway along the equator with a residential road 1.1 km north     /// of it, joined by a residential connector at every node. Motorway ids     /// `0..SPAN`, residential ids `SPAN..2*SPAN`.-    pub(super) fn corridor_graph() -> Graph {-        corridor()-    }-     fn corridor() -> Graph {         let mut nodes = Vec::new();         for lat in [0, 100_000] {@@ -395,29 +391,26 @@ mod overlay_tests {         build_graph(&nodes, &edges)     } -    /// `(total time, total distance, iterations, used overlay)` for the corridor's-    /// end-to-end drive on the residential side.-    fn drive(g: &Graph) -> (u64, u64, i32, bool) {+    /// `(total time, total distance, used overlay)` for a route along the corridor+    /// at `lat`, or `None` when planning refuses it.+    fn route(g: &Graph, mode: i32, lat: f64, e_lon: f64) -> Option<(u64, u64, bool)> {         let speeds = TrafficSpeeds::default();         let (mut scratch, mut heap) = (RoutingScratchpad::new(), RadixHeap::new());         let mut ensure = |_: i32, _: i32| {};-        let end_lon = f64::from((SPAN as i32 - 1) * STEP_E7) * 1e-7;-        let mut ctx = prepare_routing(-            g, &speeds, &mut ensure, 0.0101, 0.0001, 0.0101, end_lon - 0.0001, DRIVING, &mut scratch,-            &mut heap,-        )-        .expect("both ends snap");+        let mut ctx =+            prepare_routing(g, &speeds, &mut ensure, lat, 0.0001, lat, e_lon, mode, &mut scratch, &mut heap)?;         let hierarchical = ctx.hierarchy;-        perform_search_loop(g, &speeds, &mut ensure, DRIVING, &mut ctx, &mut scratch, &mut heap);+        perform_search_loop(g, &speeds, &mut ensure, mode, &mut ctx, &mut scratch, &mut heap);         assert_ne!(ctx.target_node, 0xFFFF_FFFF, "a route is found");         assert!(ctx.hierarchy == hierarchical, "the overlay search did not fall back");-        let steps = reconstruct_path(g, &speeds, DRIVING, &ctx, &mut scratch);-        (-            steps.iter().map(|s| s.time_10ms).sum(),-            steps.iter().map(|s| s.dist_mm).sum(),-            ctx.iterations,-            hierarchical,-        )+        let steps = reconstruct_path(g, &speeds, mode, &ctx, &mut scratch);+        Some((steps.iter().map(|s| s.time_10ms).sum(), steps.iter().map(|s| s.dist_mm).sum(), hierarchical))+    }++    /// The corridor's end-to-end drive on the residential side.+    fn drive(g: &Graph) -> (u64, u64, bool) {+        let end_lon = f64::from((SPAN as i32 - 1) * STEP_E7) * 1e-7;+        route(g, DRIVING, 0.0101, end_lon - 0.0001).expect("both ends snap")     }      #[test]@@ -448,12 +441,12 @@ mod overlay_tests {     #[test]     fn an_overlay_route_matches_the_full_search() {         let full = drive(&corridor());-        assert!(!full.3, "no overlay, no hierarchy");+        assert!(!full.2, "no overlay, no hierarchy");         let mut g = corridor();         let bytes = build_overlay(&g, u64::from(g.node_count));         assert!(g.attach_overlay_bytes(bytes));         let hier = drive(&g);-        assert!(hier.3, "a 220 km drive uses the overlay");+        assert!(hier.2, "a 220 km drive uses the overlay");         assert_eq!((hier.0, hier.1), (full.0, full.1), "same time and distance as plain A*");     } @@ -464,31 +457,29 @@ mod overlay_tests {         assert!(!g.attach_overlay_bytes(bytes));         assert!(g.overlay().is_none());     }-} -#[cfg(test)]-mod trip_limit_tests {-    use crate::geometry::TrafficSpeeds;-    use crate::graph::{BICYCLE, DRIVING, WALK};-    use crate::routing::prepare_routing;-    use crate::state::{RadixHeap, RoutingScratchpad};+    #[test]+    fn walking_and_cycling_cross_country_ride_the_overlay() {+        let mut g = corridor();+        let bytes = build_overlay(&g, u64::from(g.node_count));+        assert!(g.attach_overlay_bytes(bytes));+        for mode in [WALK, BICYCLE] {+            let (_, _, hierarchical) = route(&g, mode, 0.0101, 1.9999).expect("a 220 km trip is planned");+            assert!(hierarchical, "mode {mode} uses the overlay past the hierarchy threshold");+            let (_, _, hierarchical) = route(&g, mode, 0.0101, 0.5).expect("a 55 km trip is planned");+            assert!(!hierarchical, "mode {mode} searches every road on a short trip");+        }+        assert!(route(&g, PUBLIC_TRANSIT, 0.0101, 1.9999).is_none(), "a 220 km transit walk leg is refused");+    } -    /// A cross-country walk or ride is refused before any search, so it costs-    /// nothing; driving the same trip is still planned.     #[test]-    fn walking_and_cycling_refuse_cross_country_trips() {-        let g = super::overlay_tests::corridor_graph();-        let speeds = TrafficSpeeds::default();-        let (mut scratch, mut heap) = (RoutingScratchpad::new(), RadixHeap::new());-        let mut ensure = |_: i32, _: i32| {};-        let mut plan = |mode, e_lon: f64| {-            prepare_routing(&g, &speeds, &mut ensure, 0.0101, 0.0001, 0.0101, e_lon, mode, &mut scratch, &mut heap)-                .is_some()-        };-        // ~220 km apart.-        assert!(plan(DRIVING, 1.9999));-        assert!(!plan(WALK, 1.9999), "past the walking limit");-        assert!(plan(BICYCLE, 1.9999), "inside the cycling limit");-        assert!(plan(WALK, 0.5), "a 55 km walk is still planned");+    fn walking_leaves_a_motorway_for_a_parallel_side_road() {+        let g = corridor();+        let (_, dist_mm, _) = route(&g, WALK, 0.0001, 0.5).expect("a 55 km walk is planned");+        let along_motorway = accurate_dist_mm(0, 1_000, 0, 5_000_000);+        assert!(+            u64::from(along_motorway) + 2_000_000 < dist_mm,+            "took the two 1.1 km connectors to the side road: {dist_mm} mm vs {along_motorway} mm"+        );     } }
maps/src/main/rust/src/graph_part2.rs
diff --git a/maps/src/main/rust/src/graph_part2.rs b/maps/src/main/rust/src/graph_part2.rsindex c907f8ded3..480233ffe3 100644--- a/maps/src/main/rust/src/graph_part2.rs+++ b/maps/src/main/rust/src/graph_part2.rs@@ -355,6 +355,11 @@ impl Graph {                 } else {                     WALK_SPEED_M_S                 };+                let speed_m_s = if m != DRIVING as usize && r as u8 == MOTORWAY {+                    speed_m_s / ACTIVE_MOTORWAY_PENALTY+                } else {+                    speed_m_s+                };                 *multiplier = ((100.0 / (speed_m_s * 1000.0)) * 4_294_967_296.0) as u64;             }         }
maps/src/main/rust/src/graph_part4.rs
diff --git a/maps/src/main/rust/src/graph_part4.rs b/maps/src/main/rust/src/graph_part4.rsindex e67c26d767..f993541061 100644--- a/maps/src/main/rust/src/graph_part4.rs+++ b/maps/src/main/rust/src/graph_part4.rs@@ -403,6 +403,11 @@ impl Graph {                 } else {                     WALK_SPEED_M_S                 };+                let speed_m_s = if m != DRIVING as usize && r as u8 == MOTORWAY {+                    speed_m_s / ACTIVE_MOTORWAY_PENALTY+                } else {+                    speed_m_s+                };                 *multiplier = ((100.0 / (speed_m_s * 1000.0)) * 4_294_967_296.0) as u64;             }         }
maps/src/main/rust/src/routing/astar.rs
diff --git a/maps/src/main/rust/src/routing/astar.rs b/maps/src/main/rust/src/routing/astar.rsindex 44c30457b2..992f57d0f7 100644--- a/maps/src/main/rust/src/routing/astar.rs+++ b/maps/src/main/rust/src/routing/astar.rs@@ -1,4 +1,4 @@-//! The A* main loop, including the highway-overlay hops for long drives.+//! The A* main loop, including the highway-overlay hops for long routes. use crate::geometry::*; use crate::graph::*; use crate::state::{RadixHeap, RoutingScratchpad};
maps/src/main/rust/src/routing/search.rs
diff --git a/maps/src/main/rust/src/routing/search.rs b/maps/src/main/rust/src/routing/search.rsindex e11cd07b90..a8d09b32b5 100644--- a/maps/src/main/rust/src/routing/search.rs+++ b/maps/src/main/rust/src/routing/search.rs@@ -49,8 +49,9 @@ pub struct RoutingContext {     pub hierarchy: bool, } -/// Driving routes at least this long (straight line) use the highway overlay-/// when the graph has one.+/// Driving, walking and cycling routes at least this long (straight line) use the+/// highway overlay when the graph has one. Walking and cycling cost is distance+/// over a fixed speed, so the overlay keeps their A* heuristic consistent too. const HIERARCHY_MIN_MM: u32 = 150_000_000; /// A route from the start projection to the end projection that never leaves the /// edge they both snapped to.@@ -190,22 +191,15 @@ pub fn find_nearest_edge(g: &Graph, lat: f64, lon: f64, mode: i32) -> SnappedEdg     best } -/// Straight-line limit for a walking or cycling route.-///-/// Neither mode uses the highway overlay, so a cross-country request is a full-/// search of every path on the continent: on device LA to NYC on foot ran into-/// the 25M-expansion cap after ~30 s and gigabytes of scratch, and the route-/// sheet plans every mode for every selection. Nobody needs those routes.+/// Straight-line limit for a road-graph route. PUBLIC_TRANSIT reaches the road+/// graph only for a journey's walk legs and never uses the overlay, so a long one+/// would be a full-continent search. fn max_trip_mm(mode: i32) -> Option<u32> {-    match mode {-        WALK | PUBLIC_TRANSIT => Some(150_000_000),-        BICYCLE => Some(300_000_000),-        _ => None,-    }+    (mode == PUBLIC_TRANSIT).then_some(150_000_000) } -/// Snap endpoints, seed the open set. Returns `None` if snapping fails or the-/// trip is too far to walk or cycle.+/// Snap endpoints, seed the open set. Returns `None` if snapping fails or a+/// transit walk leg is implausibly long. #[allow(clippy::too_many_arguments)] pub fn prepare_routing(     g: &Graph,@@ -240,7 +234,7 @@ pub fn prepare_routing(      seed(g, traffic, &start, &end, mode, scratch, heap);     let direct = direct_path(g, traffic, &start, &end, mode);-    let hierarchy = mode == DRIVING+    let hierarchy = mode != PUBLIC_TRANSIT         && g.overlay().is_some()         && accurate_dist_mm(start.proj_lat, start.proj_lon, end.proj_lat, end.proj_lon)             >= HIERARCHY_MIN_MM;