Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / graph_build_part10.rs
graph_build_part10.rs162 lines · 7.2 KiBRaw
| 1 | |
| 2 | // ---- stop reconnection by component size ------------------------------ |
| 3 | |
| 4 | /// Three road networks and a stop on each, plus a stop attached to nothing. |
| 5 | /// |
| 6 | /// This is the planet case stated at the smallest scale that can state it. |
| 7 | /// Continents are not road-connected, so a planet's largest component is |
| 8 | /// Eurasia-plus-Africa at 57.7% and the old "reconnect everything outside the |
| 9 | /// largest component" rule tried to drag every American, Australian, Japanese, |
| 10 | /// British and Indonesian stop across an ocean — failing 1,173,347 times. The |
| 11 | /// rule is component *size*, so a stop already on a real road network is left |
| 12 | /// alone whichever network that is. |
| 13 | #[test] |
| 14 | fn a_stop_on_any_routable_component_is_left_alone() { |
| 15 | const A: u32 = 1200; // the largest component |
| 16 | const B: u32 = 1000; // exactly MIN_ROUTABLE_COMPONENT, so still routable |
| 17 | const C: u32 = 999; // one node short of it, so not |
| 18 | let total = A + B + C + 1; |
| 19 | |
| 20 | // A is far west; B and C are adjacent in both index and space, so the |
| 21 | // widening window around a stop in C reaches B before anything else. |
| 22 | let mut coords: Vec<geom::Pt> = Vec::with_capacity(total as usize); |
| 23 | for i in 0..A { |
| 24 | coords.push((370_000_000 + i as i32 * 1000, -1_220_000_000)); |
| 25 | } |
| 26 | for i in 0..B { |
| 27 | coords.push((400_000_000 + i as i32 * 1000, -740_000_000)); |
| 28 | } |
| 29 | for i in 0..C { |
| 30 | coords.push((410_000_000 + i as i32 * 1000, -740_000_000)); |
| 31 | } |
| 32 | // The stop attached to nothing sits beside B's far end. |
| 33 | coords.push((400_999_500, -740_000_000)); |
| 34 | |
| 35 | let mut edges: Vec<(u32, u32)> = Vec::new(); |
| 36 | let chain = |base: u32, len: u32, edges: &mut Vec<(u32, u32)>| { |
| 37 | for i in 1..len { |
| 38 | edges.push((base + i - 1, base + i)); |
| 39 | edges.push((base + i, base + i - 1)); |
| 40 | } |
| 41 | }; |
| 42 | chain(0, A, &mut edges); |
| 43 | chain(A, B, &mut edges); |
| 44 | chain(A + B, C, &mut edges); |
| 45 | let csr = csr_of(total, &edges); |
| 46 | |
| 47 | let mut stops = Bitset::new(u64::from(total)); |
| 48 | for s in [A / 2, A + B / 2, A + B + C / 2, A + B + C] { |
| 49 | stops.set(u64::from(s)); |
| 50 | } |
| 51 | |
| 52 | let rec = reconnect_isolated_stops(&coords, &stops, &csr); |
| 53 | assert_eq!(rec.lcc_size, u64::from(A)); |
| 54 | assert_eq!( |
| 55 | rec.already_connected, 2, |
| 56 | "the stops in A and in B are both already on a routable component" |
| 57 | ); |
| 58 | assert_eq!(rec.unreachable, 0); |
| 59 | // The stop in C and the stop attached to nothing, both connectors both ways. |
| 60 | assert_eq!(rec.synth.len(), 4); |
| 61 | for source in [A + B + C / 2, A + B + C] { |
| 62 | let target = rec |
| 63 | .synth |
| 64 | .iter() |
| 65 | .find(|s| s.source == source) |
| 66 | .expect("every isolated stop gets a connector") |
| 67 | .target; |
| 68 | assert!( |
| 69 | target < A + B, |
| 70 | "stop {source} was connected to node {target}, which is in C" |
| 71 | ); |
| 72 | assert_eq!(target, A + B - 1, "and to the nearest such node"); |
| 73 | } |
| 74 | // Both directions of each connector are present, which is what makes the |
| 75 | // stop reachable rather than merely reaching. |
| 76 | for s in &rec.synth { |
| 77 | assert!( |
| 78 | rec.synth |
| 79 | .iter() |
| 80 | .any(|t| t.source == s.target && t.target == s.source), |
| 81 | "connector {} -> {} has no return", |
| 82 | s.source, |
| 83 | s.target |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn the_routable_threshold_falls_back_to_the_largest_component() { |
| 90 | // A graph whose largest component is under MIN_ROUTABLE_COMPONENT still has |
| 91 | // a road network — it is just a small one. Requiring 1000 nodes there would |
| 92 | // qualify nothing and reconnect nothing, so the threshold is capped at the |
| 93 | // largest component that exists. Two nodes of road and a stop off to one |
| 94 | // side: the stop must still find the road. |
| 95 | let coords: Vec<geom::Pt> = vec![ |
| 96 | (370_000_000, -1_220_000_000), |
| 97 | (370_010_000, -1_220_000_000), |
| 98 | (380_000_000, -1_220_000_000), |
| 99 | ]; |
| 100 | let csr = csr_of(3, &[(0, 1), (1, 0)]); |
| 101 | let mut stops = Bitset::new(3); |
| 102 | stops.set(2); |
| 103 | |
| 104 | let rec = reconnect_isolated_stops(&coords, &stops, &csr); |
| 105 | assert_eq!(rec.lcc_size, 2); |
| 106 | assert_eq!(rec.already_connected, 0); |
| 107 | assert_eq!(rec.unreachable, 0); |
| 108 | assert_eq!(rec.synth.len(), 2); |
| 109 | assert_eq!(rec.synth[0].source, 1, "the nearest node of the only road"); |
| 110 | assert_eq!(rec.synth[0].target, 2); |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn args_default_the_output_directory() { |
| 115 | let (input, out, opts) = parse_args(&["cal.osm.pbf".into()]).unwrap(); |
| 116 | assert_eq!(input, PathBuf::from("cal.osm.pbf")); |
| 117 | assert_eq!(out, PathBuf::from("map_data")); |
| 118 | assert!( |
| 119 | opts.within_way_chains, |
| 120 | "the streaming within-way path is the CLI default" |
| 121 | ); |
| 122 | let (_, out, _) = parse_args(&["cal.osm.pbf".into(), "--out".into(), "d".into()]).unwrap(); |
| 123 | assert_eq!(out, PathBuf::from("d")); |
| 124 | let (_, _, opts) = |
| 125 | parse_args(&["cal.osm.pbf".into(), "--reference-collapse".into()]).unwrap(); |
| 126 | assert!( |
| 127 | !opts.within_way_chains, |
| 128 | "--reference-collapse opts back into the reference path" |
| 129 | ); |
| 130 | assert_eq!(opts.round_count(), 1, "one round is the default"); |
| 131 | assert!(opts.spill_dir.is_none(), "the spill defaults to the output dir"); |
| 132 | let (_, _, opts) = parse_args(&["cal.osm.pbf".into(), "--rounds".into(), "8".into()]).unwrap(); |
| 133 | assert_eq!(opts.round_count(), 8); |
| 134 | let (_, _, opts) = |
| 135 | parse_args(&["c.pbf".into(), "--spill-dir".into(), "/fast".into()]).unwrap(); |
| 136 | assert_eq!(opts.spill_dir, Some(PathBuf::from("/fast"))); |
| 137 | assert!(parse_args(&["a".into(), "--spill-dir".into()]).is_err()); |
| 138 | let (_, _, opts) = parse_args(&[ |
| 139 | "c.pbf".into(), |
| 140 | "--spill-dir".into(), |
| 141 | "/roomy".into(), |
| 142 | "--spill-pts-dir".into(), |
| 143 | "/fast".into(), |
| 144 | ]) |
| 145 | .unwrap(); |
| 146 | assert_eq!(opts.spill_dir, Some(PathBuf::from("/roomy"))); |
| 147 | assert_eq!(opts.spill_pts_dir, Some(PathBuf::from("/fast"))); |
| 148 | assert!(parse_args(&["a".into(), "--spill-pts-dir".into()]).is_err()); |
| 149 | assert!(parse_args(&["a".into(), "--rounds".into(), "0".into()]).is_err()); |
| 150 | assert!(parse_args(&["a".into(), "--rounds".into(), "many".into()]).is_err()); |
| 151 | assert!(parse_args(&["a".into(), "--rounds".into()]).is_err()); |
| 152 | let (_, _, opts) = parse_args(&["c.pbf".into()]).unwrap(); |
| 153 | assert!(opts.threads.is_none(), "the pool defaults to the box"); |
| 154 | let (_, _, opts) = |
| 155 | parse_args(&["c.pbf".into(), "--threads".into(), "4".into()]).unwrap(); |
| 156 | assert_eq!(opts.threads, Some(4)); |
| 157 | assert!(parse_args(&["a".into(), "--threads".into(), "0".into()]).is_err()); |
| 158 | assert!(parse_args(&["a".into(), "--threads".into()]).is_err()); |
| 159 | assert!(parse_args(&[]).is_err()); |
| 160 | assert!(parse_args(&["a".into(), "b".into()]).is_err()); |
| 161 | assert!(parse_args(&["--wat".into()]).is_err()); |
| 162 | } |