Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / junction_part5.rs
junction_part5.rs92 lines · 4.8 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod reconstruct_from_planet5 { |
| 3 | //! The planet5 (~50 GB revision) archive drops the prebuilt junction layer (no release style |
| 4 | //! draws it). This pins that the connectors stay reconstructable from what planet5 keeps: the |
| 5 | //! junction builder is run over the build's own graph (the unthinned v7 `road_graph` output) |
| 6 | //! and over planet5's graph (thinned to 1 m with the +-25 m junction zone, varint 1e-6 degree |
| 7 | //! shape points, zstd blocks: v9), and every connector is matched. |
| 8 | //! |
| 9 | //! Env-gated (real graphs): |
| 10 | //! `MAMAPS_JUNCTION_ORIG=<v7 graph dir> MAMAPS_JUNCTION_P5=<v9 graph dir> cargo test -p |
| 11 | //! mamaps_build --release reconstruct_from_planet5 -- --ignored --nocapture` |
| 12 | use super::*; |
| 13 | |
| 14 | type NodeConnectors = Vec<(u64, Vec<Vec<(f64, f64)>>)>; |
| 15 | |
| 16 | fn connectors(dir: &Path, stride: u64) -> NodeConnectors { |
| 17 | let graph = Graph::load(dir).expect("graph"); |
| 18 | let lanes = LaneTable::load(dir).expect("lanes"); |
| 19 | let incoming = InEdges::build(&graph).expect("incoming"); |
| 20 | let conventions = Conventions::default(); |
| 21 | (0..graph.node_count) |
| 22 | .step_by(stride as usize) |
| 23 | .map(|n| (n, junctions_for_node(n, &graph, lanes.as_ref(), &incoming, &conventions))) |
| 24 | .collect() |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | #[ignore = "needs MAMAPS_JUNCTION_ORIG and MAMAPS_JUNCTION_P5 (real graphs)"] |
| 29 | fn connectors_reconstruct_from_the_planet5_graph() { |
| 30 | let (Ok(a), Ok(b)) = (std::env::var("MAMAPS_JUNCTION_ORIG"), std::env::var("MAMAPS_JUNCTION_P5")) else { |
| 31 | return; |
| 32 | }; |
| 33 | let stride: u64 = std::env::var("MAMAPS_JUNCTION_STRIDE").ok().and_then(|v| v.parse().ok()).unwrap_or(1); |
| 34 | let ca = connectors(Path::new(&a), stride); |
| 35 | let cb = connectors(Path::new(&b), stride); |
| 36 | assert_eq!(ca.len(), cb.len(), "the two graphs must share node numbering"); |
| 37 | let (mut nodes, mut junction_nodes, mut same_count, mut conn_a, mut conn_b) = (0u64, 0u64, 0u64, 0u64, 0u64); |
| 38 | let mut devs: Vec<f64> = Vec::new(); |
| 39 | for ((n, x), (m, y)) in ca.iter().zip(cb.iter()) { |
| 40 | assert_eq!(n, m); |
| 41 | nodes += 1; |
| 42 | conn_a += x.len() as u64; |
| 43 | conn_b += y.len() as u64; |
| 44 | if x.is_empty() && y.is_empty() { |
| 45 | continue; |
| 46 | } |
| 47 | junction_nodes += 1; |
| 48 | if x.len() == y.len() { |
| 49 | same_count += 1; |
| 50 | for (p, q) in x.iter().zip(y.iter()) { |
| 51 | let mut d = 0.0f64; |
| 52 | if p.len() != q.len() { |
| 53 | d = f64::INFINITY; |
| 54 | } else { |
| 55 | for (u, v) in p.iter().zip(q.iter()) { |
| 56 | let dy = (u.1 - v.1) * 111_320.0; |
| 57 | let dx = (u.0 - v.0) * 111_320.0 * u.1.to_radians().cos(); |
| 58 | d = d.max(dx.hypot(dy)); |
| 59 | } |
| 60 | } |
| 61 | devs.push(d); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | let inf = devs.iter().filter(|d| d.is_infinite()).count(); |
| 66 | devs.retain(|d| d.is_finite()); |
| 67 | devs.sort_by(|a, b| a.total_cmp(b)); |
| 68 | println!("connectors with a different sample count (shape changed, e.g. turn class or sweep bucket): {inf}"); |
| 69 | let pct = |q: f64| devs.get(((devs.len() as f64 - 1.0) * q) as usize).copied().unwrap_or(0.0); |
| 70 | let exact = devs.iter().filter(|d| **d == 0.0).count(); |
| 71 | let within_10cm = devs.iter().filter(|d| **d <= 0.10).count(); |
| 72 | println!( |
| 73 | "nodes {nodes} junction nodes {junction_nodes} (same connector count at {same_count}) | connectors orig {conn_a} p5 {conn_b} | \ |
| 74 | matched {} : exact {exact}, <=10 cm {within_10cm}, p50 {:.3} m p99 {:.3} m max {:.3} m", |
| 75 | devs.len(), |
| 76 | pct(0.5), |
| 77 | pct(0.99), |
| 78 | pct(1.0) |
| 79 | ); |
| 80 | assert!(conn_a > 0, "no connectors built from the original graph"); |
| 81 | assert!(same_count as f64 >= junction_nodes as f64 * 0.999, "junction nodes whose connector count changed"); |
| 82 | // Measured on US West (v7 build graph vs planet5 v9): 99.93% of junction nodes keep their |
| 83 | // connector count; of the connectors, 93% move <= 10 cm, p99 0.30 m; 1.5% change their |
| 84 | // sampling, from approach headings that move <= ~2-3 degrees when 1 m thinning drops a |
| 85 | // first interior point beyond the 25 m junction zone (or 1e-6 degree rounding meets a |
| 86 | // very short first step). Every input is present; see the planet50 report for the |
| 87 | // device-builder spec (headings over the first >= 5 m and arm length from `dist_mm`). |
| 88 | assert!(inf as f64 <= conn_a as f64 * 0.03, "more than 3% of connectors changed sampling"); |
| 89 | assert!(within_10cm as f64 >= devs.len() as f64 * 0.9, "fewer than 90% of connectors within 10 cm"); |
| 90 | assert!(pct(0.5) <= 0.01, "median connector deviation above 1 cm"); |
| 91 | } |
| 92 | } |