Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / bundle_part5.rs
bundle_part5.rs348 lines · 15.5 KiBRaw
| 1 | #[cfg(test)] |
| 2 | /// A 6 km line, shared with one route over its first half and another over its second, |
| 3 | /// on opposite sides of it, so the lane it takes really does have to change at the seam. |
| 4 | fn back_to_back_corridors() -> Vec<Fixture> { |
| 5 | vec![ |
| 6 | (0u32, 0x00_00_10u32, "Main", north(37.70, -122.40, 100.0, 61)), |
| 7 | (1, 0x00_00_20, "First", shifted(&north(37.70, -122.40, 100.0, 31), 8.0)), |
| 8 | ( |
| 9 | 2, |
| 10 | 0x00_00_05, |
| 11 | "Last", |
| 12 | shifted(&north(37.70 + 3000.0 / 111_320.0, -122.40, 100.0, 31), -8.0), |
| 13 | ), |
| 14 | ] |
| 15 | } |
| 16 | |
| 17 | #[cfg(test)] |
| 18 | /// Two lines closing at a shallow angle: one runs 4 km north, the other comes in from a |
| 19 | /// hundred metres west over the first kilometre of it and then runs alongside. They are |
| 20 | /// one corridor from wherever they first come within [`CORRIDOR_M`], which is a long way |
| 21 | /// before either could draw the other's geometry without it showing. |
| 22 | fn a_shallow_merge() -> Vec<Fixture> { |
| 23 | let main = north(37.70, -122.40, 100.0, 41); |
| 24 | let cos_lat = 37.70_f64.to_radians().cos(); |
| 25 | let east = |m: f64| (m / (111_320.0 * cos_lat) * 1e7) as i32; |
| 26 | let joining: Vec<(i32, i32)> = main |
| 27 | .iter() |
| 28 | .enumerate() |
| 29 | .map(|(i, &(lat, lon))| { |
| 30 | let closed = (i as f64 * 100.0 / 1000.0).min(1.0); |
| 31 | (lat, lon + east(8.0 - 108.0 * (1.0 - closed))) |
| 32 | }) |
| 33 | .collect(); |
| 34 | vec![(0u32, 0x00_00_10u32, "Main", main), (1, 0x00_00_20, "Join", joining)] |
| 35 | } |
| 36 | |
| 37 | #[cfg(test)] |
| 38 | /// The corridor a polyline would be the reference of, for measuring the output against. |
| 39 | fn corridor_of(points: &[(i32, i32)]) -> Corridor { |
| 40 | let points = canonical(points); |
| 41 | let cum = cumulative(&points); |
| 42 | Corridor { points, cum, colours: Vec::new() } |
| 43 | } |
| 44 | |
| 45 | #[cfg(test)] |
| 46 | /// `n` services sharing `metres` of trunk running north and then leaving it in |
| 47 | /// different directions for 20 km. |
| 48 | fn trunk_then_branches(metres: f64, n: usize) -> Vec<Fixture> { |
| 49 | let steps = (metres / 100.0).round() as usize + 1; |
| 50 | let trunk = north(37.70, -122.40, 100.0, steps); |
| 51 | let top = *trunk.last().expect("a trunk"); |
| 52 | let cos_lat = (top.0 as f64 * 1e-7).to_radians().cos(); |
| 53 | let east = |m: f64| (m / (111_320.0 * cos_lat) * 1e7) as i32; |
| 54 | let up = |m: f64| (m / 111_320.0 * 1e7) as i32; |
| 55 | let branch = |dx: f64, dy: f64| -> Vec<(i32, i32)> { |
| 56 | let mut line = trunk.clone(); |
| 57 | for i in 1..=200 { |
| 58 | line.push((top.0 + up(dy * i as f64), top.1 + east(dx * i as f64))); |
| 59 | } |
| 60 | line |
| 61 | }; |
| 62 | let away = [(100.0, 0.0), (0.0, 100.0), (-100.0, 0.0), (70.0, 70.0)]; |
| 63 | let names = ["A", "B", "C", "D"]; |
| 64 | (0..n) |
| 65 | .map(|i| { |
| 66 | let (dx, dy) = away[i]; |
| 67 | (i as u32, 0x11u32 * (i as u32 + 1), names[i], branch(dx, dy)) |
| 68 | }) |
| 69 | .collect() |
| 70 | } |
| 71 | |
| 72 | #[cfg(test)] |
| 73 | mod tests_continued { |
| 74 | use super::*; |
| 75 | |
| 76 | /// The pieces of one route meet: the span before a corridor is carried across to the |
| 77 | /// point on the reference the corridor span begins at, and so is the ease across to the |
| 78 | /// body it leads into. |
| 79 | #[test] |
| 80 | fn the_spans_of_one_route_share_their_boundary_vertex() { |
| 81 | for lines in |
| 82 | [trunk_then_branches(2000.0, 4), back_to_back_corridors(), a_shallow_merge()] |
| 83 | { |
| 84 | let spans = assign(&candidates(&lines)); |
| 85 | for candidate in &spans { |
| 86 | for pair in candidate.windows(2) { |
| 87 | assert_eq!( |
| 88 | pair[0].points.last(), |
| 89 | pair[1].points.first(), |
| 90 | "a gap between two spans of one route", |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// A route can step straight out of one corridor and into the next, with nothing of its |
| 98 | /// own in between and two different references either side of the seam. |
| 99 | #[test] |
| 100 | fn a_route_passing_from_one_corridor_into_the_next_changes_lane_where_they_meet() { |
| 101 | let spans = assign(&candidates(&back_to_back_corridors())); |
| 102 | let places: Vec<(u8, u8)> = spans[0].iter().map(|s| (s.ordinal, s.lanes)).collect(); |
| 103 | assert!(places.contains(&(0, 2)), "the first corridor's lower ordinal: {places:?}"); |
| 104 | assert!(places.contains(&(1, 2)), "the second corridor's higher ordinal: {places:?}"); |
| 105 | assert!( |
| 106 | !places.iter().any(|p| p.1 == 1), |
| 107 | "and no stretch of its own between them: {places:?}", |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | /// A 6 km line, shared with one route over its first half and another over its second, |
| 112 | /// on opposite sides of it, so the lane it takes really does have to change at the seam. |
| 113 | fn back_to_back_corridors() -> Vec<Fixture> { |
| 114 | vec![ |
| 115 | (0u32, 0x00_00_10u32, "Main", north(37.70, -122.40, 100.0, 61)), |
| 116 | (1, 0x00_00_20, "First", shifted(&north(37.70, -122.40, 100.0, 31), 8.0)), |
| 117 | ( |
| 118 | 2, |
| 119 | 0x00_00_05, |
| 120 | "Last", |
| 121 | shifted(&north(37.70 + 3000.0 / 111_320.0, -122.40, 100.0, 31), -8.0), |
| 122 | ), |
| 123 | ] |
| 124 | } |
| 125 | |
| 126 | /// Two lines closing at a shallow angle: one runs 4 km north, the other comes in from a |
| 127 | /// hundred metres west over the first kilometre of it and then runs alongside. They are |
| 128 | /// one corridor from wherever they first come within [`CORRIDOR_M`], which is a long way |
| 129 | /// before either could draw the other's geometry without it showing. |
| 130 | fn a_shallow_merge() -> Vec<Fixture> { |
| 131 | let main = north(37.70, -122.40, 100.0, 41); |
| 132 | let cos_lat = 37.70_f64.to_radians().cos(); |
| 133 | let east = |m: f64| (m / (111_320.0 * cos_lat) * 1e7) as i32; |
| 134 | let joining: Vec<(i32, i32)> = main |
| 135 | .iter() |
| 136 | .enumerate() |
| 137 | .map(|(i, &(lat, lon))| { |
| 138 | let closed = (i as f64 * 100.0 / 1000.0).min(1.0); |
| 139 | (lat, lon + east(8.0 - 108.0 * (1.0 - closed))) |
| 140 | }) |
| 141 | .collect(); |
| 142 | vec![(0u32, 0x00_00_10u32, "Main", main), (1, 0x00_00_20, "Join", joining)] |
| 143 | } |
| 144 | |
| 145 | /// The corridor a polyline would be the reference of, for measuring the output against. |
| 146 | fn corridor_of(points: &[(i32, i32)]) -> Corridor { |
| 147 | let points = canonical(points); |
| 148 | let cum = cumulative(&points); |
| 149 | Corridor { points, cum, colours: Vec::new() } |
| 150 | } |
| 151 | |
| 152 | /// `n` services sharing `metres` of trunk running north and then leaving it in |
| 153 | /// different directions for 20 km. |
| 154 | fn trunk_then_branches(metres: f64, n: usize) -> Vec<Fixture> { |
| 155 | let steps = (metres / 100.0).round() as usize + 1; |
| 156 | let trunk = north(37.70, -122.40, 100.0, steps); |
| 157 | let top = *trunk.last().expect("a trunk"); |
| 158 | let cos_lat = (top.0 as f64 * 1e-7).to_radians().cos(); |
| 159 | let east = |m: f64| (m / (111_320.0 * cos_lat) * 1e7) as i32; |
| 160 | let up = |m: f64| (m / 111_320.0 * 1e7) as i32; |
| 161 | let branch = |dx: f64, dy: f64| -> Vec<(i32, i32)> { |
| 162 | let mut line = trunk.clone(); |
| 163 | for i in 1..=200 { |
| 164 | line.push((top.0 + up(dy * i as f64), top.1 + east(dx * i as f64))); |
| 165 | } |
| 166 | line |
| 167 | }; |
| 168 | let away = [(100.0, 0.0), (0.0, 100.0), (-100.0, 0.0), (70.0, 70.0)]; |
| 169 | let names = ["A", "B", "C", "D"]; |
| 170 | (0..n) |
| 171 | .map(|i| { |
| 172 | let (dx, dy) = away[i]; |
| 173 | (i as u32, 0x11u32 * (i as u32 + 1), names[i], branch(dx, dy)) |
| 174 | }) |
| 175 | .collect() |
| 176 | } |
| 177 | |
| 178 | /// A second copy of a line adds nothing to the map, whichever way round it is drawn. |
| 179 | /// THE case: a feed publishes each direction of a service as its own shape, and a |
| 180 | /// regional feed republishes the lot re-surveyed a few metres off. |
| 181 | #[test] |
| 182 | fn track_already_drawn_is_not_carried_again() { |
| 183 | let line = north(37.7, -122.4, 100.0, 60); |
| 184 | let mut covered = Covered::default(); |
| 185 | assert!(!covered.contains(&line), "the first copy is all new"); |
| 186 | covered.add(&line); |
| 187 | assert!(covered.contains(&line), "the same line again"); |
| 188 | let reversed: Vec<(i32, i32)> = line.iter().rev().copied().collect(); |
| 189 | assert!(covered.contains(&reversed), "the other direction"); |
| 190 | assert!(covered.contains(&shifted(&line, 9.0)), "the adjacent track"); |
| 191 | // A short-turn lies on its parent for its whole length, so it adds nothing either. |
| 192 | assert!(covered.contains(&north(37.7, -122.4, 100.0, 30)), "a short-turn"); |
| 193 | } |
| 194 | |
| 195 | /// How many distinct services already draw over a stretch of track. |
| 196 | /// |
| 197 | /// The colour-scoped gates let two services share a track on purpose, and that is right for a |
| 198 | /// city. On a planet one alignment is republished by a city feed, the regional feed containing |
| 199 | /// it and a national feed on top, each under its own colour, and every one of them claims a |
| 200 | /// lane — which is what draws one railway as fifteen jagged parallel lines. |
| 201 | #[test] |
| 202 | fn crowd_counts_the_distinct_services_over_a_track() { |
| 203 | let line = north(37.7, -122.4, 100.0, 60); |
| 204 | let mut covered = Covered::default(); |
| 205 | assert_eq!(covered.crowd(&line), 0, "empty track carries nobody"); |
| 206 | |
| 207 | covered.add_tagged(&line, 0xE31E24); |
| 208 | assert_eq!(covered.crowd(&line), 1); |
| 209 | // A second service on the same track is real and must be counted, not merged. |
| 210 | covered.add_tagged(&line, 0x0054A5); |
| 211 | assert_eq!(covered.crowd(&line), 2); |
| 212 | // The same service again is not a third. |
| 213 | covered.add_tagged(&line, 0xE31E24); |
| 214 | assert_eq!(covered.crowd(&line), 2, "one service counted twice"); |
| 215 | // Slightly off, still the same corridor. |
| 216 | covered.add_tagged(&shifted(&line, 9.0), 0x00A650); |
| 217 | assert_eq!(covered.crowd(&line), 3, "a re-survey a few metres off is the same track"); |
| 218 | // Track nobody has drawn is uncrowded however busy its neighbour is. |
| 219 | assert_eq!(covered.crowd(&north(37.9, -122.9, 100.0, 60)), 0, "elsewhere"); |
| 220 | } |
| 221 | |
| 222 | /// The worst point along the line, not the average: a branch joining a busy trunk for part of |
| 223 | /// its length is exactly the line worth suppressing. |
| 224 | #[test] |
| 225 | fn crowd_reports_the_busiest_point_not_the_whole_line() { |
| 226 | let trunk = north(37.7, -122.4, 100.0, 30); |
| 227 | let longer = north(37.7, -122.4, 100.0, 60); |
| 228 | let mut covered = Covered::default(); |
| 229 | for colour in [1u32, 2, 3] { |
| 230 | covered.add_tagged(&trunk, colour); |
| 231 | } |
| 232 | assert_eq!( |
| 233 | covered.crowd(&longer), |
| 234 | 3, |
| 235 | "a line overlapping a crowded trunk for half its length is crowded" |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | /// A re-publication that runs a little further is still a duplicate of the part it shares. |
| 240 | /// |
| 241 | /// The case that drew the LA A line as two strands a metre apart: two feeds publish the same |
| 242 | /// alignment at slightly different lengths, the longer one "adds new track" at one end, and an |
| 243 | /// all-or-nothing rule therefore keeps it whole — duplicating everything they share. Each copy |
| 244 | /// then gets its own corridor lane and taper, so one line reads as two that diverge and |
| 245 | /// reconverge. |
| 246 | #[test] |
| 247 | fn a_longer_republication_of_the_same_track_is_mostly_drawn() { |
| 248 | let short = north(37.7, -122.4, 100.0, 60); |
| 249 | let long = north(37.7, -122.4, 100.0, 63); |
| 250 | let mut covered = Covered::default(); |
| 251 | covered.add(&short); |
| 252 | assert!(!covered.contains(&long), "it does add a little new track at the end"); |
| 253 | let fraction = covered.covered_fraction(&long); |
| 254 | assert!(fraction > 0.9, "but nearly all of it is already drawn: {fraction}"); |
| 255 | assert!(fraction < 1.0, "and not quite all"); |
| 256 | } |
| 257 | |
| 258 | /// A branch is not a duplicate. Half shared, half genuinely new track, so it must survive |
| 259 | /// whatever threshold the duplicate test uses. |
| 260 | #[test] |
| 261 | fn a_route_that_branches_away_is_not_mostly_drawn() { |
| 262 | let trunk = north(37.7, -122.4, 100.0, 60); |
| 263 | let mut covered = Covered::default(); |
| 264 | covered.add(&trunk); |
| 265 | // Same start, then off to its own alignment well outside the corridor. |
| 266 | let mut branch: Vec<(i32, i32)> = trunk[..30].to_vec(); |
| 267 | branch.extend(shifted(&north(37.72, -122.4, 100.0, 30), 500.0)); |
| 268 | let fraction = covered.covered_fraction(&branch); |
| 269 | assert!(fraction < 0.9, "half of it is new track: {fraction}"); |
| 270 | } |
| 271 | |
| 272 | /// Survey noise is not new track. Two agencies' surveys of one track disagree by a few |
| 273 | /// metres, and a wobble inside the tolerance must not resurrect a duplicate line. |
| 274 | #[test] |
| 275 | fn a_wobble_inside_the_tolerance_is_the_same_track() { |
| 276 | let straight = north(37.7, -122.4, 100.0, 60); |
| 277 | let mut covered = Covered::default(); |
| 278 | covered.add(&straight); |
| 279 | let cos_lat = (straight[0].0 as f64 * 1e-7).to_radians().cos(); |
| 280 | let east = |m: f64| (m / (111_320.0 * cos_lat) * 1e7) as i32; |
| 281 | let wobbled: Vec<(i32, i32)> = straight |
| 282 | .iter() |
| 283 | .enumerate() |
| 284 | .map(|(i, &(lat, lon))| (lat, lon + east(if i % 2 == 0 { 9.0 } else { -9.0 }))) |
| 285 | .collect(); |
| 286 | assert!(covered.contains(&wobbled), "9 m of survey drift is the same track"); |
| 287 | } |
| 288 | |
| 289 | /// A branch adds track, so it is carried **whole** rather than trimmed to the part that |
| 290 | /// diverges. Trimming was tried and is wrong: it leaves a route in fragments, and a |
| 291 | /// stretch too short to be worth emitting leaves a hole that nothing else draws. The |
| 292 | /// trunk it shares is over-drawn in its own colour at its own offset, which is invisible. |
| 293 | #[test] |
| 294 | fn a_branch_adds_track_and_is_carried_whole() { |
| 295 | let trunk = north(37.7, -122.4, 100.0, 60); |
| 296 | let mut covered = Covered::default(); |
| 297 | covered.add(&trunk); |
| 298 | let mut branch = north(37.7, -122.4, 100.0, 30); |
| 299 | let split_at = *branch.last().unwrap(); |
| 300 | let cos_lat = (split_at.0 as f64 * 1e-7).to_radians().cos(); |
| 301 | for i in 1..=20 { |
| 302 | branch.push(( |
| 303 | split_at.0, |
| 304 | split_at.1 + (i as f64 * 100.0 / (111_320.0 * cos_lat) * 1e7) as i32, |
| 305 | )); |
| 306 | } |
| 307 | assert!(!covered.contains(&branch), "it leaves the trunk, so it is not redundant"); |
| 308 | } |
| 309 | |
| 310 | #[test] |
| 311 | fn a_different_alignment_is_all_new_track() { |
| 312 | let line = north(37.7, -122.4, 100.0, 60); |
| 313 | let mut covered = Covered::default(); |
| 314 | covered.add(&line); |
| 315 | assert!(!covered.contains(&shifted(&line, 1000.0))); |
| 316 | } |
| 317 | |
| 318 | #[test] |
| 319 | fn a_line_with_fewer_than_two_points_covers_nothing() { |
| 320 | let line = north(37.7, -122.4, 100.0, 10); |
| 321 | let covered = Covered::default(); |
| 322 | assert!(!covered.contains(&line[..1])); |
| 323 | assert!(!covered.contains(&[])); |
| 324 | } |
| 325 | |
| 326 | /// The two halves of the slicing primitive: measuring along a polyline and cutting it |
| 327 | /// between two of those measurements, with both ends landing exactly on the line. |
| 328 | #[test] |
| 329 | fn a_slice_of_a_polyline_lands_exactly_on_it_at_both_ends() { |
| 330 | let line = north(37.7, -122.4, 100.0, 11); |
| 331 | let cum = cumulative(&line); |
| 332 | assert!((cum[10] - 1000.0).abs() < 1.0, "{cum:?}"); |
| 333 | assert_eq!(slice_between(&line, &cum, 0.0, cum[10]), line, "the whole thing"); |
| 334 | let middle = slice_between(&line, &cum, 250.0, 750.0); |
| 335 | assert_eq!(middle.len(), 7, "both cut ends plus the five vertices between"); |
| 336 | assert!(distance_m(middle[0], line[2]) < 60.0, "a quarter of the way along"); |
| 337 | // Two adjacent slices meet exactly, which is what keeps a route's pieces joined. |
| 338 | let before = slice_between(&line, &cum, 0.0, 250.0); |
| 339 | assert_eq!(before.last(), middle.first()); |
| 340 | // Nothing between the two distances is nothing at all. |
| 341 | assert!(slice_between(&line, &cum, 400.0, 400.0).is_empty()); |
| 342 | } |
| 343 | |
| 344 | #[test] |
| 345 | fn nothing_in_nothing_out() { |
| 346 | assert!(assign(&[]).is_empty()); |
| 347 | } |
| 348 | } |