Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / maps / src / main / rust / tests / real_archive.rs
real_archive.rs299 lines · 12.2 KiBRaw
| 1 | //! The offline router (graph + highway overlay + transit) against a REAL planet archive |
| 2 | //! (`planet4.mamaps`, or the byte-identical local build `scripts/maps/inputs/world.mamaps`). |
| 3 | //! |
| 4 | //! By default these tests run against small SYNTHETIC archives built |
| 5 | //! in-test (see `graph_fixture.rs` and `pack.rs`): the same loader, |
| 6 | //! routing and planning paths, without the 80 GB input. Set the env |
| 7 | //! var to run them against the planet instead: |
| 8 | //! |
| 9 | //! ```text |
| 10 | //! MAMAPS_REAL_ARCHIVE=/path/to/world.mamaps \ |
| 11 | //! cargo test -p offlinerouter --test real_archive -- --nocapture --test-threads=1 |
| 12 | //! ``` |
| 13 | //! |
| 14 | //! Same calls the JNI layer makes (`Graph::load_archive`, `prepare_routing` + |
| 15 | //! `perform_search_loop` + `reconstruct_path`, `TransitIndex::load_archive`, `plan`, |
| 16 | //! `stop_departures`), minus the JNI marshalling and live traffic. |
| 17 | |
| 18 | use offlinerouter::geometry::TrafficSpeeds; |
| 19 | use offlinerouter::graph::{Graph, BICYCLE, DRIVING, WALK}; |
| 20 | use offlinerouter::routing::{perform_search_loop, prepare_routing, reconstruct_path, StepData}; |
| 21 | use offlinerouter::state::{RadixHeap, RoutingScratchpad}; |
| 22 | use offlinerouter::transit::{ |
| 23 | plan, rail_lines, stop_departures, LegKind, QueryDay, Schedule, TransitIndex, |
| 24 | }; |
| 25 | |
| 26 | #[path = "real_archive/graph_fixture.rs"] |
| 27 | mod graph_fixture; |
| 28 | #[path = "real_archive/pack.rs"] |
| 29 | mod pack; |
| 30 | |
| 31 | const ENV: &str = "MAMAPS_REAL_ARCHIVE"; |
| 32 | |
| 33 | /// (label, from (lat, lon), to (lat, lon), travel mode). |
| 34 | type RouteCase = (&'static str, (f64, f64), (f64, f64), i32); |
| 35 | |
| 36 | fn archive() -> Option<String> { |
| 37 | match std::env::var(ENV) { |
| 38 | Ok(p) if !p.trim().is_empty() && std::path::Path::new(&p).is_file() => Some(p), |
| 39 | _ => None, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn crow_km(a: (f64, f64), b: (f64, f64)) -> f64 { |
| 44 | let (la1, lo1, la2, lo2) = |
| 45 | (a.0.to_radians(), a.1.to_radians(), b.0.to_radians(), b.1.to_radians()); |
| 46 | let h = ((la2 - la1) / 2.0).sin().powi(2) |
| 47 | + la1.cos() * la2.cos() * ((lo2 - lo1) / 2.0).sin().powi(2); |
| 48 | 2.0 * 6371.0 * h.sqrt().asin() |
| 49 | } |
| 50 | |
| 51 | /// One route through the app's routing core, or `None` when it does not snap/route. |
| 52 | fn route(g: &Graph, from: (f64, f64), to: (f64, f64), mode: i32) -> Option<Vec<StepData>> { |
| 53 | let speeds = TrafficSpeeds::default(); |
| 54 | let mut scratch = RoutingScratchpad::new(); |
| 55 | let mut heap = RadixHeap::new(); |
| 56 | let mut ensure = |_lat_e7: i32, _lon_e7: i32| {}; |
| 57 | let mut ctx = prepare_routing( |
| 58 | g, |
| 59 | &speeds, |
| 60 | &mut ensure, |
| 61 | from.0, |
| 62 | from.1, |
| 63 | to.0, |
| 64 | to.1, |
| 65 | mode, |
| 66 | &mut scratch, |
| 67 | &mut heap, |
| 68 | )?; |
| 69 | perform_search_loop(g, &speeds, &mut ensure, mode, &mut ctx, &mut scratch, &mut heap); |
| 70 | if ctx.target_node == 0xFFFF_FFFF && ctx.direct.is_none() { |
| 71 | return None; |
| 72 | } |
| 73 | Some(reconstruct_path(g, &speeds, mode, &ctx, &mut scratch)) |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn graph_sections_load_and_routes_resolve() { |
| 78 | let (path, planet) = match archive() { |
| 79 | Some(p) => (p, true), |
| 80 | None => ( |
| 81 | graph_fixture::write_temp("graph", &graph_fixture::graph_archive()) |
| 82 | .to_string_lossy() |
| 83 | .into_owned(), |
| 84 | false, |
| 85 | ), |
| 86 | }; |
| 87 | let t0 = std::time::Instant::now(); |
| 88 | let g = Graph::load_archive(&path).expect("Graph::load_archive refused the archive"); |
| 89 | println!( |
| 90 | "graph: {} nodes, {} edges, overlay {}, elevation {}, loaded in {:?}", |
| 91 | g.node_count, |
| 92 | g.edge_count, |
| 93 | g.overlay().is_some(), |
| 94 | g.has_elevation(), |
| 95 | t0.elapsed() |
| 96 | ); |
| 97 | if planet { |
| 98 | assert!(g.node_count > 100_000_000, "not a planet graph"); |
| 99 | } else { |
| 100 | assert_eq!(g.node_count, 9, "the synthetic grid's node count"); |
| 101 | assert_eq!(g.edge_count, 24, "the synthetic grid's edge count"); |
| 102 | assert!(g.edge_lane_masks(0).is_some(), "lanes section carries edge 0"); |
| 103 | } |
| 104 | assert!(g.overlay().is_some(), "highway overlay (kind 14) not attached"); |
| 105 | |
| 106 | let planet_cases: &[RouteCase] = &[ |
| 107 | ( |
| 108 | "SF drive Ferry Bldg -> Golden Gate Park", |
| 109 | (37.7955, -122.3937), |
| 110 | (37.7694, -122.4862), |
| 111 | DRIVING, |
| 112 | ), |
| 113 | ("SF walk Union Sq -> Ferry Bldg", (37.7880, -122.4075), (37.7955, -122.3937), WALK), |
| 114 | ("SF bike Mission -> Marina", (37.7599, -122.4148), (37.8037, -122.4368), BICYCLE), |
| 115 | ("NYC drive Midtown -> JFK", (40.7580, -73.9855), (40.6413, -73.7781), DRIVING), |
| 116 | ("London drive Westminster -> Camden", (51.4995, -0.1248), (51.5390, -0.1426), DRIVING), |
| 117 | ("Tokyo walk Shibuya -> Harajuku", (35.6595, 139.7005), (35.6702, 139.7027), WALK), |
| 118 | ("Long drive SF -> LA (overlay)", (37.7749, -122.4194), (34.0522, -118.2437), DRIVING), |
| 119 | ]; |
| 120 | let synthetic_cases: &[RouteCase] = &[ |
| 121 | ( |
| 122 | "grid drive NE -> SW", |
| 123 | graph_fixture::node_lat_lon(8), |
| 124 | graph_fixture::node_lat_lon(0), |
| 125 | DRIVING, |
| 126 | ), |
| 127 | ("grid walk N edge", graph_fixture::node_lat_lon(6), graph_fixture::node_lat_lon(8), WALK), |
| 128 | ( |
| 129 | "grid bike SW -> NE", |
| 130 | graph_fixture::node_lat_lon(0), |
| 131 | graph_fixture::node_lat_lon(8), |
| 132 | BICYCLE, |
| 133 | ), |
| 134 | ]; |
| 135 | let cases = if planet { planet_cases } else { synthetic_cases }; |
| 136 | for &(name, from, to, mode) in cases { |
| 137 | let t = std::time::Instant::now(); |
| 138 | let steps = route(&g, from, to, mode).unwrap_or_else(|| panic!("{name}: no route")); |
| 139 | let km = steps.iter().map(|s| s.dist_mm).sum::<u64>() as f64 / 1e6; |
| 140 | let mins = steps.iter().map(|s| s.time_10ms).sum::<u64>() as f64 / 6000.0; |
| 141 | let named = steps.iter().filter(|s| g.road_name(s.name_off).is_some()).count(); |
| 142 | let lanes = steps.iter().filter(|s| !s.lanes.is_empty()).count(); |
| 143 | let crow = crow_km(from, to); |
| 144 | println!( |
| 145 | "{name}: {km:.1} km (crow {crow:.1}), {mins:.0} min, {} steps ({named} named, {lanes} with lanes) in {:?}", |
| 146 | steps.len(), |
| 147 | t.elapsed() |
| 148 | ); |
| 149 | assert!(!steps.is_empty(), "{name}: empty route"); |
| 150 | assert!( |
| 151 | km >= crow * 0.95 && km <= crow * 3.0 + 2.0, |
| 152 | "{name}: implausible length {km} km for {crow} km crow" |
| 153 | ); |
| 154 | assert!(named > 0, "{name}: no step resolved a road name (road_names section)"); |
| 155 | for s in &steps { |
| 156 | assert_eq!(s.coords.len() % 2, 0); |
| 157 | assert!(s.elevations.is_empty() || s.elevations.len() == s.coords.len() / 2); |
| 158 | } |
| 159 | } |
| 160 | if planet { |
| 161 | // Lane guidance (lanes section) shows up on a long drive through big junctions. |
| 162 | let la = route(&g, (37.7749, -122.4194), (34.0522, -118.2437), DRIVING).unwrap(); |
| 163 | assert!(la.iter().any(|s| !s.lanes.is_empty()), "no lane guidance on SF -> LA"); |
| 164 | } |
| 165 | if !planet { |
| 166 | std::fs::remove_file(&path).ok(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn transit_section_loads_plans_and_lists_departures() { |
| 172 | let (path, planet) = match archive() { |
| 173 | Some(p) => (p, true), |
| 174 | None => ( |
| 175 | graph_fixture::write_temp( |
| 176 | "transit", |
| 177 | &graph_fixture::transit_archive(&pack::synthetic_pack()), |
| 178 | ) |
| 179 | .to_string_lossy() |
| 180 | .into_owned(), |
| 181 | false, |
| 182 | ), |
| 183 | }; |
| 184 | assert!(TransitIndex::has_archive_transit(&path), "no transit section (kind 13)"); |
| 185 | let t0 = std::time::Instant::now(); |
| 186 | let idx = |
| 187 | TransitIndex::load_archive(&path).expect("TransitIndex::load_archive refused the archive"); |
| 188 | println!("transit: feed {:?}, loaded in {:?}", idx.feed_name(), t0.elapsed()); |
| 189 | |
| 190 | if planet { |
| 191 | assert!(idx.covers(37.7749, -122.4194), "transit pack does not cover SF"); |
| 192 | |
| 193 | // A weekday mid-morning (Wednesday 2026-11-11), the day this test was written. |
| 194 | let sched = Schedule { day: QueryDay::for_date(2, 20261111), overlay: None }; |
| 195 | let nine = 9 * 3600; |
| 196 | |
| 197 | let board = stop_departures(&idx, 37.7764, -122.3943, nine, sched, 10); // SF Caltrain 4th & King |
| 198 | println!( |
| 199 | "departures at 4th & King: {:?}", |
| 200 | board |
| 201 | .iter() |
| 202 | .map(|d| format!("{} -> {} @{}", d.route_name, d.headsign, d.dep_secs)) |
| 203 | .collect::<Vec<_>>() |
| 204 | ); |
| 205 | assert!(!board.is_empty(), "no offline departures at SF 4th & King"); |
| 206 | assert!(board.windows(2).all(|w| w[0].dep_secs <= w[1].dep_secs)); |
| 207 | |
| 208 | let tz = idx.timezone_at(37.7749, -122.4194); |
| 209 | let motis = idx.nearest_stop_motis_id(37.7764, -122.3943); |
| 210 | println!("timezone {tz}, MOTIS stop id {motis:?}"); |
| 211 | assert_eq!(tz, "America/Los_Angeles"); |
| 212 | |
| 213 | let lines = rail_lines(&idx, 37.70, -122.50, 37.81, -122.38); |
| 214 | println!("rail lines in SF bbox: {}", lines.len()); |
| 215 | assert!(!lines.is_empty()); |
| 216 | |
| 217 | for (name, from, to) in [ |
| 218 | ("SF Mission -> Downtown", (37.7599, -122.4148), (37.7880, -122.4075)), |
| 219 | ("NYC Brooklyn -> Midtown", (40.6782, -73.9442), (40.7580, -73.9855)), |
| 220 | ("London Camden -> Westminster", (51.5390, -0.1426), (51.4995, -0.1248)), |
| 221 | ("Tokyo Shinjuku -> Tokyo Stn", (35.6896, 139.7006), (35.6812, 139.7671)), |
| 222 | ] { |
| 223 | let legs = plan(&idx, from.0, from.1, to.0, to.1, nine, sched); |
| 224 | match &legs { |
| 225 | Some(legs) => println!( |
| 226 | "plan {name}: {}", |
| 227 | legs.iter() |
| 228 | .map(|l| { |
| 229 | let kind = match l.kind { |
| 230 | LegKind::Walk => "walk", |
| 231 | LegKind::Wait => "wait", |
| 232 | LegKind::Ride => "ride", |
| 233 | }; |
| 234 | format!("{kind}:{}({}->{})", l.name, l.from_stop, l.to_stop) |
| 235 | }) |
| 236 | .collect::<Vec<_>>() |
| 237 | .join(" | ") |
| 238 | ), |
| 239 | None => println!("plan {name}: no journey"), |
| 240 | } |
| 241 | let legs = legs.unwrap_or_else(|| panic!("{name}: no transit journey")); |
| 242 | assert!(legs.iter().any(|l| l.kind == LegKind::Ride), "{name}: journey has no ride"); |
| 243 | } |
| 244 | } else { |
| 245 | // The synthetic pack: Alpha (37.700) -> Beta -> Gamma on route N, |
| 246 | // 08:00 and 09:00 trips, Wednesday 2024-01-03. |
| 247 | assert_eq!(idx.feed_name(), "testpack"); |
| 248 | assert!(idx.covers(37.710, -122.400), "transit pack does not cover Beta"); |
| 249 | let sched = Schedule { day: QueryDay::for_date(2, 20240103), overlay: None }; |
| 250 | |
| 251 | let board = stop_departures(&idx, 37.700, -122.400, 28_000, sched, 10); // Alpha |
| 252 | println!( |
| 253 | "departures at Alpha: {:?}", |
| 254 | board |
| 255 | .iter() |
| 256 | .map(|d| format!("{} -> {} @{}", d.route_name, d.headsign, d.dep_secs)) |
| 257 | .collect::<Vec<_>>() |
| 258 | ); |
| 259 | assert!(!board.is_empty(), "no offline departures at Alpha"); |
| 260 | assert!(board.windows(2).all(|w| w[0].dep_secs <= w[1].dep_secs)); |
| 261 | assert_eq!(board[0].dep_secs, 28_800, "first departure is the 08:00 trip"); |
| 262 | |
| 263 | let tz = idx.timezone_at(37.700, -122.400); |
| 264 | let motis = idx.nearest_stop_motis_id(37.700, -122.400); |
| 265 | println!("timezone {tz}, MOTIS stop id {motis:?}"); |
| 266 | assert_eq!(tz, "America/Los_Angeles"); |
| 267 | assert!( |
| 268 | motis.as_deref().is_some_and(|m| m.contains("901201")), |
| 269 | "Alpha's MOTIS id: {motis:?}" |
| 270 | ); |
| 271 | |
| 272 | let lines = rail_lines(&idx, 37.69, -122.41, 37.73, -122.39); |
| 273 | println!("rail lines in synthetic bbox: {}", lines.len()); |
| 274 | assert!(!lines.is_empty()); |
| 275 | |
| 276 | let legs = plan(&idx, 37.700, -122.400, 37.720, -122.400, 25_000, sched) |
| 277 | .expect("a journey exists"); |
| 278 | println!( |
| 279 | "plan Alpha -> Gamma: {}", |
| 280 | legs.iter() |
| 281 | .map(|l| { |
| 282 | let kind = match l.kind { |
| 283 | LegKind::Walk => "walk", |
| 284 | LegKind::Wait => "wait", |
| 285 | LegKind::Ride => "ride", |
| 286 | }; |
| 287 | format!("{kind}:{}({}->{})", l.name, l.from_stop, l.to_stop) |
| 288 | }) |
| 289 | .collect::<Vec<_>>() |
| 290 | .join(" | ") |
| 291 | ); |
| 292 | let ride = legs.iter().find(|l| l.kind == LegKind::Ride).expect("journey has no ride"); |
| 293 | assert_eq!(ride.name, "N"); |
| 294 | assert_eq!(ride.from_stop, "Alpha"); |
| 295 | assert_eq!(ride.to_stop, "Gamma"); |
| 296 | assert_eq!((ride.dep_secs, ride.arr_secs), (28_800, 29_400)); |
| 297 | std::fs::remove_file(&path).ok(); |
| 298 | } |
| 299 | } |