Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / extract_part3.rs
extract_part3.rs443 lines · 18.2 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use super::*; |
| 4 | use crate::testpbf; |
| 5 | |
| 6 | fn features(path: &Path) -> Vec<String> { |
| 7 | std::fs::read_to_string(path) |
| 8 | .unwrap() |
| 9 | .lines() |
| 10 | .map(str::to_string) |
| 11 | .collect() |
| 12 | } |
| 13 | |
| 14 | #[test] |
| 15 | fn extracts_the_safety_layer_from_a_pbf() { |
| 16 | let (pbf_path, dir) = testpbf::write_layers_sample("extract_safety"); |
| 17 | let out = dir.join("safety.geojsonseq"); |
| 18 | let stats = build( |
| 19 | &pbf_path, |
| 20 | &out, |
| 21 | &Options { |
| 22 | layer: Layer::Safety, |
| 23 | bbox: None, |
| 24 | }, |
| 25 | ) |
| 26 | .unwrap(); |
| 27 | |
| 28 | // The fixture's four safety nodes: a speed camera, an ALPR, a stop sign |
| 29 | // and traffic signals. Everything else in it belongs to other layers. |
| 30 | assert_eq!(stats.features, 4); |
| 31 | assert_eq!(stats.from_nodes, 4); |
| 32 | assert_eq!((stats.from_ways, stats.from_relations), (0, 0)); |
| 33 | |
| 34 | let lines = features(&out); |
| 35 | assert_eq!(lines.len(), 4); |
| 36 | let kinds: Vec<&str> = ["speed_camera", "alpr", "stop_sign", "traffic_signals"] |
| 37 | .into_iter() |
| 38 | .filter(|k| lines.iter().any(|l| l.contains(&format!("\"kind\":\"{k}\"")))) |
| 39 | .collect(); |
| 40 | assert_eq!(kinds, ["speed_camera", "alpr", "stop_sign", "traffic_signals"]); |
| 41 | |
| 42 | // Every line is a Point Feature with an osm_id in the node/N form. |
| 43 | for l in &lines { |
| 44 | assert!(l.starts_with("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\""), "{l}"); |
| 45 | assert!(l.contains("\"osm_id\":\"node/"), "{l}"); |
| 46 | } |
| 47 | |
| 48 | // The ALPR node keeps its operator and surveillance_type. |
| 49 | let alpr = lines.iter().find(|l| l.contains("\"alpr\"")).unwrap(); |
| 50 | assert!(alpr.contains("\"operator\":\"Flock Safety\""), "{alpr}"); |
| 51 | assert!(alpr.contains("\"surveillance_type\":\"ALPR\""), "{alpr}"); |
| 52 | |
| 53 | // The cafe node has a name and tags, but is not road furniture. |
| 54 | assert!(!lines.iter().any(|l| l.contains("Corner Cafe"))); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn two_runs_are_byte_identical() { |
| 59 | let (pbf_path, dir) = testpbf::write_layers_sample("extract_det"); |
| 60 | let run = |suffix: &str| { |
| 61 | let out = dir.join(format!("safety{suffix}.geojsonseq")); |
| 62 | build( |
| 63 | &pbf_path, |
| 64 | &out, |
| 65 | &Options { |
| 66 | layer: Layer::Safety, |
| 67 | bbox: None, |
| 68 | }, |
| 69 | ) |
| 70 | .unwrap(); |
| 71 | std::fs::read(out).unwrap() |
| 72 | }; |
| 73 | assert_eq!(run("a"), run("b")); |
| 74 | } |
| 75 | |
| 76 | /// The point of `--threads`: it must change only how fast the work happens. |
| 77 | /// Counts that do not divide the chunk count are the interesting ones. |
| 78 | #[test] |
| 79 | fn the_thread_cap_changes_no_bytes() { |
| 80 | let (pbf_path, dir) = testpbf::write_layers_sample("extract_threads"); |
| 81 | let run = |n: usize| { |
| 82 | crate::par::set_threads(n); |
| 83 | let out = dir.join(format!("safety.t{n}.geojsonseq")); |
| 84 | build( |
| 85 | &pbf_path, |
| 86 | &out, |
| 87 | &Options { |
| 88 | layer: Layer::Safety, |
| 89 | bbox: None, |
| 90 | }, |
| 91 | ) |
| 92 | .unwrap(); |
| 93 | std::fs::read(out).unwrap() |
| 94 | }; |
| 95 | let serial = run(1); |
| 96 | for n in [2, 3, 7, 32] { |
| 97 | assert_eq!(serial, run(n), "{n} threads perturbed the output"); |
| 98 | } |
| 99 | crate::par::clear_threads(); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn a_bbox_that_excludes_everything_yields_an_empty_layer() { |
| 104 | let (pbf_path, dir) = testpbf::write_layers_sample("extract_bbox"); |
| 105 | let out = dir.join("safety.geojsonseq"); |
| 106 | // The fixture sits near 37N 122W; this box is over the Atlantic. |
| 107 | let stats = build( |
| 108 | &pbf_path, |
| 109 | &out, |
| 110 | &Options { |
| 111 | layer: Layer::Safety, |
| 112 | bbox: Some(BBox::parse("-30,20,-20,30").unwrap()), |
| 113 | }, |
| 114 | ) |
| 115 | .unwrap(); |
| 116 | assert_eq!(stats.features, 0); |
| 117 | // The count says "4 safety features were outside the box", which is what |
| 118 | // distinguishes a bad bbox from a PBF with no cameras in it. |
| 119 | assert_eq!(stats.outside_bbox, 4); |
| 120 | assert_eq!(features(&out).len(), 0); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn a_bbox_that_includes_everything_changes_nothing() { |
| 125 | let (pbf_path, dir) = testpbf::write_layers_sample("extract_bbox_all"); |
| 126 | let out = dir.join("safety.geojsonseq"); |
| 127 | let stats = build( |
| 128 | &pbf_path, |
| 129 | &out, |
| 130 | &Options { |
| 131 | layer: Layer::Safety, |
| 132 | bbox: Some(BBox::parse("-123,36,-121,38").unwrap()), |
| 133 | }, |
| 134 | ) |
| 135 | .unwrap(); |
| 136 | assert_eq!(stats.features, 4); |
| 137 | assert_eq!(stats.outside_bbox, 0); |
| 138 | } |
| 139 | |
| 140 | // --- maxspeed --------------------------------------------------------- |
| 141 | |
| 142 | fn extract_layer(tag: &str, layer: Layer) -> (Vec<String>, Stats) { |
| 143 | let (pbf_path, dir) = testpbf::write_layers_sample(tag); |
| 144 | let out = dir.join(format!("{}.geojsonseq", layer.name())); |
| 145 | let stats = build(&pbf_path, &out, &Options { layer, bbox: None }).unwrap(); |
| 146 | (features(&out), stats) |
| 147 | } |
| 148 | |
| 149 | #[test] |
| 150 | fn extracts_the_maxspeed_layer_with_its_raw_values() { |
| 151 | let (lines, stats) = extract_layer("extract_maxspeed", Layer::Maxspeed); |
| 152 | // Two of the fixture's three highway ways carry a limit; the third does not. |
| 153 | assert_eq!((stats.features, stats.from_ways), (2, 2)); |
| 154 | assert_eq!(stats.from_nodes, 0); |
| 155 | assert_eq!(lines.len(), 2); |
| 156 | |
| 157 | // The whole point of the layer: the string survives verbatim. |
| 158 | let mph = lines.iter().find(|l| l.contains("way/3001")).unwrap(); |
| 159 | assert!(mph.contains("\"maxspeed\":\"25 mph\""), "{mph}"); |
| 160 | assert!(mph.contains("\"highway\":\"residential\""), "{mph}"); |
| 161 | assert!(mph.contains("\"name\":\"Main St\""), "{mph}"); |
| 162 | assert!(mph.contains("\"type\":\"LineString\""), "{mph}"); |
| 163 | |
| 164 | // maxspeed=none is kept as "none", and beats the directional tag. |
| 165 | let none = lines.iter().find(|l| l.contains("way/3002")).unwrap(); |
| 166 | assert!(none.contains("\"maxspeed\":\"none\""), "{none}"); |
| 167 | assert!(!none.contains("30 mph"), "the direction-agnostic tag wins: {none}"); |
| 168 | |
| 169 | // The way with no limit is absent. |
| 170 | assert!(!lines.iter().any(|l| l.contains("way/3003"))); |
| 171 | |
| 172 | // Geometry came from resolved node coordinates, not from nowhere. |
| 173 | assert!(mph.contains("-122.43"), "{mph}"); |
| 174 | } |
| 175 | |
| 176 | // --- roads ------------------------------------------------------------ |
| 177 | |
| 178 | #[test] |
| 179 | fn extracts_the_roads_layer_with_lanes_speed_and_width() { |
| 180 | let (lines, stats) = extract_layer("extract_roads", Layer::Roads); |
| 181 | // All three of the fixture's highway ways, including the one with no |
| 182 | // speed limit: `roads` is every road, not only the ones with a limit. |
| 183 | assert_eq!((stats.features, stats.from_ways), (3, 3)); |
| 184 | assert_eq!(lines.len(), 3); |
| 185 | |
| 186 | // The fully-attributed motorway. |
| 187 | let m = lines.iter().find(|l| l.contains("way/3002")).unwrap(); |
| 188 | assert!(m.contains("\"class\":1"), "motorway is class 1: {m}"); |
| 189 | assert!(m.contains("\"lanes\":3"), "{m}"); |
| 190 | // through|through|right as the graph's own LANE_* masks, left to right. |
| 191 | assert!( |
| 192 | m.contains(&format!( |
| 193 | "\"turn_lanes_forward\":\"{}|{}|{}\"", |
| 194 | crate::tags::LANE_THROUGH, |
| 195 | crate::tags::LANE_THROUGH, |
| 196 | crate::tags::LANE_RIGHT |
| 197 | )), |
| 198 | "{m}" |
| 199 | ); |
| 200 | assert!(m.contains("\"oneway\":1"), "{m}"); |
| 201 | assert!(m.contains("\"width\":12.00"), "{m}"); |
| 202 | assert!(m.contains("\"bridge\":1"), "{m}"); |
| 203 | assert!(m.contains("\"layer\":1"), "{m}"); |
| 204 | // `maxspeed=none` keeps its string and gets no number: it is not 0 km/h. |
| 205 | assert!(m.contains("\"maxspeed\":\"none\""), "{m}"); |
| 206 | assert!(!m.contains("maxspeed_kmh"), "{m}"); |
| 207 | |
| 208 | // A posted mph limit arrives as both forms. |
| 209 | let r = lines.iter().find(|l| l.contains("way/3001")).unwrap(); |
| 210 | assert!(r.contains("\"class\":7"), "residential is class 7: {r}"); |
| 211 | assert!(r.contains("\"maxspeed\":\"25 mph\""), "{r}"); |
| 212 | assert!(r.contains("\"maxspeed_kmh\":40"), "{r}"); |
| 213 | assert!(r.contains("\"type\":\"LineString\""), "{r}"); |
| 214 | // Geometry came from resolved node coordinates. |
| 215 | assert!(r.contains("-122.43"), "{r}"); |
| 216 | |
| 217 | // And the service road with nothing on it is class + osm_id only. |
| 218 | let s = lines.iter().find(|l| l.contains("way/3003")).unwrap(); |
| 219 | assert!(s.contains("\"properties\":{\"class\":8,\"osm_id\":\"way/3003\"}"), "{s}"); |
| 220 | |
| 221 | // The railway ways and the platform are not roads. |
| 222 | assert!(!lines.iter().any(|l| l.contains("way/5001"))); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn roads_is_deterministic_and_bbox_filtered() { |
| 227 | let (pbf_path, dir) = testpbf::write_layers_sample("det_roads"); |
| 228 | let run = |suffix: &str| { |
| 229 | let out = dir.join(format!("roads{suffix}.geojsonseq")); |
| 230 | build(&pbf_path, &out, &Options { layer: Layer::Roads, bbox: None }).unwrap(); |
| 231 | std::fs::read(out).unwrap() |
| 232 | }; |
| 233 | assert_eq!(run("a"), run("b")); |
| 234 | |
| 235 | let stats = build( |
| 236 | &pbf_path, |
| 237 | &dir.join("empty.geojsonseq"), |
| 238 | &Options { |
| 239 | layer: Layer::Roads, |
| 240 | bbox: Some(BBox::parse("-30,20,-20,30").unwrap()), |
| 241 | }, |
| 242 | ) |
| 243 | .unwrap(); |
| 244 | assert_eq!(stats.features, 0); |
| 245 | assert_eq!(stats.outside_bbox, 3); |
| 246 | } |
| 247 | |
| 248 | // --- transit_lines ---------------------------------------------------- |
| 249 | |
| 250 | #[test] |
| 251 | fn extracts_the_transit_lines_layer_from_ways_and_relations() { |
| 252 | let (lines, stats) = extract_layer("extract_transit", Layer::TransitLines); |
| 253 | // Two railway ways plus one route relation. The platform way and the bus |
| 254 | // route are both dropped. |
| 255 | assert_eq!(stats.features, 3); |
| 256 | assert_eq!((stats.from_ways, stats.from_relations), (2, 1)); |
| 257 | assert_eq!(lines.len(), 3); |
| 258 | |
| 259 | let subway = lines.iter().find(|l| l.contains("way/5001")).unwrap(); |
| 260 | assert!(subway.contains("\"kind\":\"subway\""), "{subway}"); |
| 261 | assert!(subway.contains("\"name\":\"Market St Subway\""), "{subway}"); |
| 262 | assert!(subway.contains("\"type\":\"LineString\""), "{subway}"); |
| 263 | |
| 264 | // narrow_gauge folds into rail. |
| 265 | let ng = lines.iter().find(|l| l.contains("way/5002")).unwrap(); |
| 266 | assert!(ng.contains("\"kind\":\"rail\""), "{ng}"); |
| 267 | |
| 268 | assert!(!lines.iter().any(|l| l.contains("way/5003")), "no platform"); |
| 269 | assert!(!lines.iter().any(|l| l.contains("relation/9002")), "no bus route"); |
| 270 | } |
| 271 | |
| 272 | #[test] |
| 273 | fn a_route_relation_becomes_an_unstitched_multilinestring() { |
| 274 | let (lines, _) = extract_layer("extract_transit_rel", Layer::TransitLines); |
| 275 | let route = lines.iter().find(|l| l.contains("relation/9001")).unwrap(); |
| 276 | assert!(route.contains("\"type\":\"MultiLineString\""), "{route}"); |
| 277 | assert!(route.contains("\"kind\":\"subway\""), "{route}"); |
| 278 | // The tags GDAL used to bury in an `other_tags` HSTORE are now just tags. |
| 279 | assert!(route.contains("\"name\":\"Red Line\""), "{route}"); |
| 280 | assert!(route.contains("\"ref\":\"Red\""), "{route}"); |
| 281 | assert!(route.contains("\"colour\":\"#DA291C\""), "{route}"); |
| 282 | |
| 283 | // Two member ways, so two parts: `[[[...]],[[...]]]` is three opening |
| 284 | // brackets before the first coordinate pair. |
| 285 | let at = route.find("\"coordinates\":").unwrap(); |
| 286 | assert!(route[at..].starts_with("\"coordinates\":[[["), "{route}"); |
| 287 | // Counting the part separators: two parts means one `]],[[`. |
| 288 | assert_eq!(route.matches("]],[[").count(), 1, "two parts: {route}"); |
| 289 | } |
| 290 | |
| 291 | #[test] |
| 292 | fn a_routes_platform_members_are_not_drawn_as_track() { |
| 293 | // The fixture's route relation carries a third member: a *closed* |
| 294 | // `railway=platform` way, `role=platform`. Assembling geometry from every |
| 295 | // member drew a box around the station. |
| 296 | let (lines, stats) = extract_layer("extract_transit_platform", Layer::TransitLines); |
| 297 | let route = lines.iter().find(|l| l.contains("relation/9001")).unwrap(); |
| 298 | assert_eq!(route.matches("]],[[").count(), 1, "the two path members only: {route}"); |
| 299 | // The platform's corners sit at 122.404-122.405W, nowhere near the path. |
| 300 | assert!(!route.contains("-122.4050000"), "platform outline drawn: {route}"); |
| 301 | assert!(!route.contains("-122.4040000"), "platform outline drawn: {route}"); |
| 302 | // And it is not a feature in its own right either, roled or not. |
| 303 | assert!(!lines.iter().any(|l| l.contains("way/5004"))); |
| 304 | assert_eq!((stats.from_ways, stats.from_relations), (2, 1)); |
| 305 | } |
| 306 | |
| 307 | #[test] |
| 308 | fn the_line_layers_are_deterministic_and_bbox_filtered() { |
| 309 | for layer in [Layer::Maxspeed, Layer::TransitLines] { |
| 310 | let (pbf_path, dir) = testpbf::write_layers_sample(&format!("det_{}", layer.name())); |
| 311 | let run = |suffix: &str| { |
| 312 | let out = dir.join(format!("{}{suffix}.geojsonseq", layer.name())); |
| 313 | build(&pbf_path, &out, &Options { layer, bbox: None }).unwrap(); |
| 314 | std::fs::read(out).unwrap() |
| 315 | }; |
| 316 | assert_eq!(run("a"), run("b"), "{} is not deterministic", layer.name()); |
| 317 | |
| 318 | // The fixture's ways sit near 37.79N 122.42W; this box is Atlantic. |
| 319 | let out = dir.join("empty.geojsonseq"); |
| 320 | let stats = build( |
| 321 | &pbf_path, |
| 322 | &out, |
| 323 | &Options { |
| 324 | layer, |
| 325 | bbox: Some(BBox::parse("-30,20,-20,30").unwrap()), |
| 326 | }, |
| 327 | ) |
| 328 | .unwrap(); |
| 329 | assert_eq!(stats.features, 0, "{}", layer.name()); |
| 330 | assert!(stats.outside_bbox > 0, "{}", layer.name()); |
| 331 | |
| 332 | // And a box that contains them changes nothing. |
| 333 | let out = dir.join("all.geojsonseq"); |
| 334 | let all = build( |
| 335 | &pbf_path, |
| 336 | &out, |
| 337 | &Options { |
| 338 | layer, |
| 339 | bbox: Some(BBox::parse("-123,36,-121,38").unwrap()), |
| 340 | }, |
| 341 | ) |
| 342 | .unwrap(); |
| 343 | assert_eq!(all.outside_bbox, 0, "{}", layer.name()); |
| 344 | assert!(all.features > 0, "{}", layer.name()); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | #[test] |
| 349 | fn a_transit_lines_layer_with_no_relations_is_reported() { |
| 350 | // Ways-only is the shape a GDAL-less legacy build produces: the right |
| 351 | // feature count, and no `colour` anywhere, so every line renders grey. |
| 352 | let ways_only = Stats { features: 900, from_ways: 900, ..Default::default() }; |
| 353 | assert!(ways_only.missing_half(Layer::TransitLines).is_some()); |
| 354 | // Both halves present, or nothing at all, are both fine. |
| 355 | let both = Stats { |
| 356 | features: 900, |
| 357 | from_ways: 800, |
| 358 | from_relations: 100, |
| 359 | ..Default::default() |
| 360 | }; |
| 361 | assert_eq!(both.missing_half(Layer::TransitLines), None); |
| 362 | assert_eq!(Stats::default().missing_half(Layer::TransitLines), None); |
| 363 | // And the check is specific to the two-source layer. |
| 364 | assert_eq!(ways_only.missing_half(Layer::Maxspeed), None); |
| 365 | } |
| 366 | |
| 367 | // --- admin_city ------------------------------------------------------- |
| 368 | |
| 369 | #[test] |
| 370 | fn assembles_an_admin_city_boundary_from_its_member_ways() { |
| 371 | let (lines, stats) = extract_layer("extract_admin", Layer::AdminCity); |
| 372 | // Only the admin_level=8 relation. The county at level 6 is dropped. |
| 373 | assert_eq!(stats.features, 1, "{lines:?}"); |
| 374 | assert_eq!((stats.from_relations, stats.from_ways), (1, 0)); |
| 375 | |
| 376 | let f = &lines[0]; |
| 377 | assert!(f.contains("\"admin_level\":8"), "a number, not a string: {f}"); |
| 378 | assert!(f.contains("\"name\":\"Oakland\""), "{f}"); |
| 379 | assert!(f.contains("\"osm_id\":\"relation/9101\""), "{f}"); |
| 380 | // No name:en on the fixture, and the city level has no fallback to `name`. |
| 381 | assert!(!f.contains("name_en"), "{f}"); |
| 382 | assert!(!lines.iter().any(|l| l.contains("Alameda County"))); |
| 383 | } |
| 384 | |
| 385 | #[test] |
| 386 | fn an_admin_outer_ring_is_stitched_and_its_hole_is_kept() { |
| 387 | let (lines, _) = extract_layer("extract_admin_rings", Layer::AdminCity); |
| 388 | let f = &lines[0]; |
| 389 | // One outer ring plus one hole: a Polygon, not a MultiPolygon. |
| 390 | assert!(f.contains("\"type\":\"Polygon\""), "{f}"); |
| 391 | // Two rings means one `]],[[` separator between them. |
| 392 | assert_eq!(f.matches("]],[[").count(), 1, "exterior plus one hole: {f}"); |
| 393 | |
| 394 | // The exterior came from two ways -- one roled `outer`, one unroled, and one |
| 395 | // of them traversed backwards -- so all four corners must be present and the |
| 396 | // ring must close on the corner it started from. |
| 397 | let at = f.find("\"coordinates\":[[").unwrap() + "\"coordinates\":[".len(); |
| 398 | let end = at + f[at..].find("]],").unwrap() + 1; |
| 399 | let outer = &f[at..end]; |
| 400 | for corner in [ |
| 401 | "[-122.4000000,37.8000000]", |
| 402 | "[-122.3600000,37.8000000]", |
| 403 | "[-122.3600000,37.8400000]", |
| 404 | "[-122.4000000,37.8400000]", |
| 405 | ] { |
| 406 | assert!(outer.contains(corner), "missing outer corner {corner}: {outer}"); |
| 407 | } |
| 408 | assert_eq!(outer.matches("[-122.4000000,37.8000000]").count(), 2, "closed: {outer}"); |
| 409 | |
| 410 | // The hole is the second ring, and is inside the first. |
| 411 | let hole = &f[end..]; |
| 412 | assert!(hole.contains("[-122.3900000,37.8100000]"), "{hole}"); |
| 413 | } |
| 414 | |
| 415 | #[test] |
| 416 | fn admin_city_is_deterministic_and_bbox_filtered() { |
| 417 | let (pbf_path, dir) = testpbf::write_layers_sample("det_admin"); |
| 418 | let run = |suffix: &str| { |
| 419 | let out = dir.join(format!("admin{suffix}.geojsonseq")); |
| 420 | build( |
| 421 | &pbf_path, |
| 422 | &out, |
| 423 | &Options { layer: Layer::AdminCity, bbox: None }, |
| 424 | ) |
| 425 | .unwrap(); |
| 426 | std::fs::read(out).unwrap() |
| 427 | }; |
| 428 | assert_eq!(run("a"), run("b")); |
| 429 | |
| 430 | let stats = build( |
| 431 | &pbf_path, |
| 432 | &dir.join("empty.geojsonseq"), |
| 433 | &Options { |
| 434 | layer: Layer::AdminCity, |
| 435 | bbox: Some(BBox::parse("-30,20,-20,30").unwrap()), |
| 436 | }, |
| 437 | ) |
| 438 | .unwrap(); |
| 439 | assert_eq!(stats.features, 0); |
| 440 | assert_eq!(stats.outside_bbox, 1); |
| 441 | } |
| 442 | |
| 443 | } |