Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / rings_part1.rs
rings_part1.rs535 lines · 24.1 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use super::*; |
| 4 | use tilecodec::mamaps::body::{Feature, GEOM_LINE, GEOM_POLYGON}; |
| 5 | use tilecodec::mamaps::dict; |
| 6 | |
| 7 | /// A layer holding one polygon feature with the given rings, all labelled by position. |
| 8 | fn layer_of(rings: &[Vec<(i16, i16)>]) -> Layer { |
| 9 | let mut layer = Layer::new(dict::LAYER_LANDTYPE); |
| 10 | layer.features.push(Feature { |
| 11 | kind: dict::NONE, |
| 12 | kind_detail: dict::NONE, |
| 13 | geom_type: GEOM_POLYGON, |
| 14 | flags: 0, |
| 15 | name_idx: tilecodec::mamaps::body::NAME_NONE, |
| 16 | parts_offset: 0, |
| 17 | part_count: rings.len() as u32, |
| 18 | transit_color: 0, |
| 19 | transit_ordinal: 0, |
| 20 | transit_lanes: 0, |
| 21 | transit_taper: 0, |
| 22 | lane_count: 0, |
| 23 | }); |
| 24 | for (index, ring) in rings.iter().enumerate() { |
| 25 | layer.parts.push(Part { |
| 26 | coord_start: layer.coords.len() as u32, |
| 27 | point_count: ring.len() as u32, |
| 28 | winding: if index == 0 { WINDING_OUTER } else { WINDING_HOLE }, |
| 29 | }); |
| 30 | layer.coords.extend_from_slice(ring); |
| 31 | } |
| 32 | layer |
| 33 | } |
| 34 | |
| 35 | /// Positive signed area, which is what this module calls an exterior's winding. |
| 36 | /// |
| 37 | /// Tile coordinates put y downward, so a positive shoelace area looks clockwise on screen. The |
| 38 | /// name follows the formula rather than the screen, because the formula is what the code and |
| 39 | /// `tess::fill` both use. |
| 40 | fn square(x: i16, y: i16, size: i16) -> Vec<(i16, i16)> { |
| 41 | vec![(x, y), (x + size, y), (x + size, y + size), (x, y + size), (x, y)] |
| 42 | } |
| 43 | |
| 44 | fn reversed(ring: &[(i16, i16)]) -> Vec<(i16, i16)> { |
| 45 | let mut out = ring.to_vec(); |
| 46 | out.reverse(); |
| 47 | out |
| 48 | } |
| 49 | |
| 50 | /// **The bug that reached a real build.** A degenerate exterior makes `normalise` drop the |
| 51 | /// feature, and the id table is indexed by feature position — so dropping one without dropping |
| 52 | /// its id shifts every id after it onto the wrong feature. The encoder catches the length |
| 53 | /// mismatch, but only when the casualty happens to be the last feature does it stay a length |
| 54 | /// mismatch rather than a silent misattribution. |
| 55 | #[test] |
| 56 | fn dropping_a_degenerate_feature_drops_its_id_too() { |
| 57 | // Three features: a good square, a collapsed exterior, and another good square. |
| 58 | let mut layer = layer_of(&[square(0, 0, 100)]); |
| 59 | let mut push = |ring: Vec<(i16, i16)>| { |
| 60 | layer.features.push(Feature { |
| 61 | kind: dict::NONE, |
| 62 | kind_detail: dict::NONE, |
| 63 | geom_type: GEOM_POLYGON, |
| 64 | flags: 0, |
| 65 | name_idx: tilecodec::mamaps::body::NAME_NONE, |
| 66 | parts_offset: layer.parts.len() as u32, |
| 67 | part_count: 1, |
| 68 | transit_color: 0, |
| 69 | transit_ordinal: 0, |
| 70 | transit_lanes: 0, |
| 71 | transit_taper: 0, |
| 72 | lane_count: 0, |
| 73 | }); |
| 74 | layer.parts.push(Part { |
| 75 | coord_start: layer.coords.len() as u32, |
| 76 | point_count: ring.len() as u32, |
| 77 | winding: WINDING_OUTER, |
| 78 | }); |
| 79 | layer.coords.extend_from_slice(&ring); |
| 80 | }; |
| 81 | // Zero area: every point the same, so there is no exterior left after normalising. |
| 82 | push(vec![(5, 5), (5, 5), (5, 5), (5, 5)]); |
| 83 | push(square(200, 200, 100)); |
| 84 | |
| 85 | let mut ids = vec![11u64, 22, 33]; |
| 86 | normalise_with_ids(&mut layer, Some(&mut ids), None, None); |
| 87 | |
| 88 | assert_eq!(layer.features.len(), 2, "the collapsed exterior is dropped"); |
| 89 | assert_eq!(ids, vec![11, 33], "and its id goes with it, not the one after it"); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn an_exterior_wound_the_wrong_way_is_reversed_not_relabelled() { |
| 94 | let mut layer = layer_of(&[reversed(&square(0, 0, 100))]); |
| 95 | let stats = normalise(&mut layer); |
| 96 | assert_eq!(stats.rewound, 1); |
| 97 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 98 | // The coordinates changed, not just the label: the winding field states a fact about them. |
| 99 | assert!(signed_area(layer.points(&layer.parts[0])) > 0.0); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn a_hole_wound_the_wrong_way_is_reversed() { |
| 104 | let mut layer = layer_of(&[square(0, 0, 100), square(20, 20, 20)]); |
| 105 | let stats = normalise(&mut layer); |
| 106 | // The hole was given counter-clockwise, which is an exterior's winding. |
| 107 | assert_eq!(stats.rewound, 1); |
| 108 | assert_eq!(stats.holes_dropped, 0); |
| 109 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 110 | assert!(signed_area(layer.points(&layer.parts[1])) < 0.0, "the hole is clockwise"); |
| 111 | } |
| 112 | |
| 113 | /// **The four pathologies the published z0 tile exhibits.** Each is dropped and counted rather |
| 114 | /// than clipped, because a botched boolean operation is worse than a missing lake. |
| 115 | #[test] |
| 116 | fn a_hole_outside_straddling_or_overlapping_is_dropped_and_counted() { |
| 117 | // Wholly outside. |
| 118 | let mut layer = layer_of(&[square(0, 0, 100), square(500, 500, 20)]); |
| 119 | let stats = normalise(&mut layer); |
| 120 | assert_eq!(stats.holes_dropped, 1); |
| 121 | assert_eq!(layer.features[0].part_count, 1, "only the exterior is left"); |
| 122 | |
| 123 | // Straddling the exterior. |
| 124 | let mut layer = layer_of(&[square(0, 0, 100), square(90, 90, 40)]); |
| 125 | assert_eq!(normalise(&mut layer).holes_dropped, 1); |
| 126 | |
| 127 | // Overlapping another hole. |
| 128 | let mut layer = |
| 129 | layer_of(&[square(0, 0, 200), square(20, 20, 60), square(50, 50, 60)]); |
| 130 | let stats = normalise(&mut layer); |
| 131 | assert_eq!(stats.holes_dropped, 1, "the first hole is kept, the overlapping one is not"); |
| 132 | assert_eq!(layer.features[0].part_count, 2); |
| 133 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 134 | } |
| 135 | |
| 136 | /// **The zoom-in island flooding.** A hole clipped onto the tile edge shares exact |
| 137 | /// vertices with its exterior's clipped edge there. The boundary counts as inside, so |
| 138 | /// the hole is kept — reading it as outside dropped real islands on fine tiles while |
| 139 | /// the coarse tile (hole fully inside) stayed pixel-perfect. |
| 140 | #[test] |
| 141 | fn a_hole_touching_its_exterior_is_kept() { |
| 142 | // Left and right edges of the hole sit exactly on the exterior's edges: the shape |
| 143 | // the tile clipper emits when both rings reach the same tile edge. |
| 144 | let exterior = vec![(0i16, 0i16), (100, 0), (100, 100), (0, 100), (0, 0)]; |
| 145 | let touching = vec![(0i16, 40i16), (100, 40), (100, 60), (0, 60), (0, 40)]; |
| 146 | let mut layer = layer_of(&[exterior, touching]); |
| 147 | let stats = normalise(&mut layer); |
| 148 | assert_eq!(stats.holes_dropped, 0, "an edge-touching hole is not a straddle"); |
| 149 | assert_eq!(layer.features[0].part_count, 2); |
| 150 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 151 | } |
| 152 | |
| 153 | /// The complement: a hole with a vertex genuinely outside the exterior still goes. |
| 154 | /// One vertex at 130 against an exterior ending at 100 is outside, not on the edge, |
| 155 | /// so no boundary rule rescues it. |
| 156 | #[test] |
| 157 | fn a_hole_crossing_its_exterior_by_one_vertex_is_still_dropped() { |
| 158 | let exterior = vec![(0i16, 0i16), (100, 0), (100, 100), (0, 100), (0, 0)]; |
| 159 | let crossing = vec![(40i16, 30i16), (40, 70), (70, 70), (130, 50), (70, 30), (40, 30)]; |
| 160 | let mut layer = layer_of(&[exterior, crossing]); |
| 161 | let stats = normalise(&mut layer); |
| 162 | assert_eq!(stats.holes_dropped, 1); |
| 163 | assert_eq!(layer.features[0].part_count, 1); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn a_zero_area_ring_goes_and_takes_nothing_with_it() { |
| 168 | // A degenerate hole. |
| 169 | let mut layer = |
| 170 | layer_of(&[square(0, 0, 100), vec![(20, 20), (40, 20), (20, 20), (20, 20)]]); |
| 171 | let stats = normalise(&mut layer); |
| 172 | assert_eq!(stats.zero_area, 1); |
| 173 | assert_eq!(layer.features[0].part_count, 1); |
| 174 | |
| 175 | // A degenerate exterior takes its holes with it: there is nothing for them to be in. |
| 176 | let mut layer = layer_of(&[vec![(0, 0), (10, 0), (0, 0), (0, 0)], square(2, 2, 2)]); |
| 177 | let stats = normalise(&mut layer); |
| 178 | assert_eq!(stats.zero_area, 1); |
| 179 | assert!(layer.features.is_empty(), "the feature draws nothing, so it is not carried"); |
| 180 | } |
| 181 | |
| 182 | /// Nested lakes: a hole inside a hole. The inner one is land again, and dropping it is the |
| 183 | /// conservative answer — it paints as water rather than as a wedge of nothing. |
| 184 | #[test] |
| 185 | fn a_lake_nested_inside_another_hole_is_dropped() { |
| 186 | let mut layer = |
| 187 | layer_of(&[square(0, 0, 400), square(50, 50, 200), square(100, 100, 50)]); |
| 188 | let stats = normalise(&mut layer); |
| 189 | assert_eq!(stats.holes_dropped, 1); |
| 190 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 191 | } |
| 192 | |
| 193 | /// An already-valid polygon must come out untouched, or every rebuild would churn. |
| 194 | #[test] |
| 195 | fn a_valid_polygon_is_left_exactly_alone() { |
| 196 | let rings = [square(0, 0, 200), reversed(&square(20, 20, 40))]; |
| 197 | let mut layer = layer_of(&rings); |
| 198 | let before = (layer.parts.clone(), layer.coords.clone()); |
| 199 | let stats = normalise(&mut layer); |
| 200 | assert!(stats.clean(), "{stats:?}"); |
| 201 | assert_eq!((layer.parts, layer.coords), before); |
| 202 | } |
| 203 | |
| 204 | /// Winding means nothing on an open path, so a line's parts pass through untouched. |
| 205 | #[test] |
| 206 | fn a_line_layer_is_not_rewound() { |
| 207 | let mut layer = Layer::new(dict::LAYER_ROADS); |
| 208 | layer.features.push(Feature { |
| 209 | kind: dict::NONE, |
| 210 | kind_detail: dict::NONE, |
| 211 | geom_type: GEOM_LINE, |
| 212 | flags: 0, |
| 213 | name_idx: tilecodec::mamaps::body::NAME_NONE, |
| 214 | parts_offset: 0, |
| 215 | part_count: 1, |
| 216 | transit_color: 0, |
| 217 | transit_ordinal: 0, |
| 218 | transit_lanes: 0, |
| 219 | transit_taper: 0, |
| 220 | lane_count: 0, |
| 221 | }); |
| 222 | layer.parts.push(Part { coord_start: 0, point_count: 3, winding: WINDING_OUTER }); |
| 223 | layer.coords = vec![(0, 0), (50, 0), (50, 50)]; |
| 224 | let before = (layer.parts.clone(), layer.coords.clone()); |
| 225 | let stats = normalise(&mut layer); |
| 226 | assert!(stats.clean()); |
| 227 | assert_eq!((layer.parts, layer.coords), before); |
| 228 | } |
| 229 | |
| 230 | /// The check has to be able to fail, or it proves nothing. |
| 231 | #[test] |
| 232 | fn the_check_reports_each_invariant_it_can_see_broken() { |
| 233 | let mut layer = layer_of(&[reversed(&square(0, 0, 100))]); |
| 234 | assert!(!check(&layer).is_empty(), "a clockwise exterior"); |
| 235 | |
| 236 | layer = layer_of(&[square(0, 0, 100), square(20, 20, 20)]); |
| 237 | let problems = check(&layer); |
| 238 | assert!( |
| 239 | problems.iter().any(|p| p.contains("not clockwise")), |
| 240 | "a counter-clockwise hole: {problems:?}", |
| 241 | ); |
| 242 | |
| 243 | layer = layer_of(&[square(0, 0, 100), reversed(&square(500, 500, 20))]); |
| 244 | let problems = check(&layer); |
| 245 | assert!( |
| 246 | problems.iter().any(|p| p.contains("not inside")), |
| 247 | "a hole outside its exterior: {problems:?}", |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | /// The arena has to stay exactly tiled by the parts after a ring is dropped, or the encoder |
| 252 | /// refuses the body — which is the check that makes this safe to run over a whole build. |
| 253 | #[test] |
| 254 | fn the_arena_stays_tiled_by_its_parts_after_a_drop() { |
| 255 | let mut layer = |
| 256 | layer_of(&[square(0, 0, 100), square(500, 500, 20), square(20, 20, 20)]); |
| 257 | normalise(&mut layer); |
| 258 | let mut at = 0u32; |
| 259 | for part in &layer.parts { |
| 260 | assert_eq!(part.coord_start, at); |
| 261 | at += part.point_count; |
| 262 | } |
| 263 | assert_eq!(at as usize, layer.coords.len(), "no orphaned coordinates"); |
| 264 | let body = tilecodec::mamaps::body::Body { |
| 265 | extent: 4096, |
| 266 | layers: vec![layer], |
| 267 | names: Vec::new(), |
| 268 | ids: Vec::new(), |
| 269 | turn_lanes: Vec::new(), |
| 270 | buildings: Vec::new(), |
| 271 | heightmap: None, carriageways: Vec::new(), convention: None, region_links: Vec::new(), |
| 272 | }; |
| 273 | // The encoder's own contiguity check, which is the real proof. |
| 274 | assert!(tilecodec::mamaps::body::serialize(&body).is_ok()); |
| 275 | } |
| 276 | |
| 277 | /// **Verification item 6, over a whole build rather than a synthetic case.** Every polygon of |
| 278 | /// every tile has to satisfy all five invariants after the tiler runs, because that is exactly |
| 279 | /// what the archive's `FLAG_RINGS_VALIDATED` claims and what the renderer will skip a repair |
| 280 | /// pass on the strength of. |
| 281 | #[test] |
| 282 | fn every_polygon_of_a_real_build_is_valid() { |
| 283 | use crate::schema::Class; |
| 284 | use tilecodec::mamaps::body::Body; |
| 285 | use tilecodec::mamaps::dict; |
| 286 | |
| 287 | // Shapes chosen to exercise the pathologies: a lake with a hole, a hole outside its |
| 288 | // exterior, and a shape large enough to be clipped across several tiles at deep zoom. |
| 289 | let ring = |x: f64, y: f64, size: f64| { |
| 290 | vec![(x, y), (x + size, y), (x + size, y + size), (x, y + size), (x, y)] |
| 291 | }; |
| 292 | let features = vec![ |
| 293 | crate::extract::Feature { |
| 294 | class: Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("lake"), 0), |
| 295 | geometry: tile_build::geom::Geometry::Polygons(vec![vec![ |
| 296 | ring(-120.5, 35.0, 1.0), |
| 297 | // A hole, wound the same way as its exterior, which stage C has to reverse. |
| 298 | ring(-120.2, 35.2, 0.3), |
| 299 | ]]), |
| 300 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 301 | turn_fwd: Vec::new(), |
| 302 | turn_bwd: Vec::new(), |
| 303 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 304 | building: None, |
| 305 | }, |
| 306 | crate::extract::Feature { |
| 307 | class: Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("water"), 0), |
| 308 | geometry: tile_build::geom::Geometry::Polygons(vec![vec![ |
| 309 | ring(-119.0, 36.0, 0.5), |
| 310 | // A hole nowhere near its exterior, which stage C has to drop. |
| 311 | ring(-100.0, 20.0, 0.1), |
| 312 | ]]), |
| 313 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 314 | turn_fwd: Vec::new(), |
| 315 | turn_bwd: Vec::new(), |
| 316 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 317 | building: None, |
| 318 | }, |
| 319 | ]; |
| 320 | let settings = crate::tiler::Settings { |
| 321 | build_id: 1, |
| 322 | scratch: std::env::temp_dir() |
| 323 | .join(format!("mamaps_rings_{}.tilechunks", std::process::id())), |
| 324 | force_chunk_spill_file: false, |
| 325 | dem: crate::dem::Dem::from_grids(14, 17, Vec::new()), |
| 326 | region_links: std::collections::HashMap::new(), |
| 327 | country_zooms: std::collections::HashMap::new(), |
| 328 | }; |
| 329 | let store = crate::store::Store::of(&features).expect("spill"); |
| 330 | let (bytes, stats) = crate::tiler::build(&store, &settings).expect("build"); |
| 331 | |
| 332 | // The archive claims validity, so every tile in it had better be valid. |
| 333 | let header = tilecodec::mamaps::Header::parse(&bytes).expect("header"); |
| 334 | assert!(header.rings_validated(), "the build claims validated rings"); |
| 335 | let mut checked = 0usize; |
| 336 | for (id, _, body) in tilecodec::mamaps::read::read_all(&bytes).expect("read") { |
| 337 | let body = Body::parse(&body).expect("parse"); |
| 338 | for layer in &body.layers { |
| 339 | let problems = check(layer); |
| 340 | assert!(problems.is_empty(), "tile {id}: {problems:?}"); |
| 341 | checked += layer.features.len(); |
| 342 | } |
| 343 | } |
| 344 | assert!(checked > 0, "the build produced no polygons to check"); |
| 345 | // And stage C really had something to do, or this proves nothing. |
| 346 | let corrected: crate::rings::Stats = |
| 347 | stats.iter().fold(crate::rings::Stats::default(), |mut acc, z| { |
| 348 | acc.add(z.rings); |
| 349 | acc |
| 350 | }); |
| 351 | assert!(!corrected.clean(), "stage C corrected nothing: {corrected:?}"); |
| 352 | } |
| 353 | |
| 354 | /// **The zoom-in island flooding, end to end.** Lake Superior at survey scale: |
| 355 | /// a ~7°-wide lake through the real pipeline — classify (named → min_zoom 6), |
| 356 | /// simplify, clip per tile, stage C — asserting the water survives at z0, z5 |
| 357 | /// and z14. Before the area floor, an unnamed lake vanished below z12 and a |
| 358 | /// named one below z6; the renderer then drew earth over the whole basin. |
| 359 | /// |
| 360 | /// Superior's real shoreline is thousands of vertices; ten per edge keeps the |
| 361 | /// shape (and its tile-edge crossings) while the test stays instant. |
| 362 | #[test] |
| 363 | fn lake_superior_survives_coarse_and_fine() { |
| 364 | use crate::schema::Class; |
| 365 | use tilecodec::mamaps::body::Body; |
| 366 | use tilecodec::mamaps::dict; |
| 367 | |
| 368 | // Lake Superior's real bbox (~-92.1..-84.4 lon, 46.4..48.9 lat), ten |
| 369 | // vertices per edge: the tile-edge crossings are what the clipper and |
| 370 | // stage C must survive, not the shoreline detail. |
| 371 | let edge = |x0: f64, y0: f64, x1: f64, y1: f64| -> Vec<(f64, f64)> { |
| 372 | (0..10) |
| 373 | .map(|i| { |
| 374 | let t = i as f64 / 9.0; |
| 375 | (x0 + (x1 - x0) * t, y0 + (y1 - y0) * t) |
| 376 | }) |
| 377 | .collect() |
| 378 | }; |
| 379 | let (x0, y0, x1, y1) = (-92.1, 46.4, -84.4, 48.9); |
| 380 | let mut ring = edge(x0, y0, x1, y0); |
| 381 | ring.extend(edge(x1, y0, x1, y1).into_iter().skip(1)); |
| 382 | ring.extend(edge(x1, y1, x0, y1).into_iter().skip(1)); |
| 383 | ring.extend(edge(x0, y1, x0, y0).into_iter().skip(1)); |
| 384 | // Classify exactly as a named lake classifies: min_zoom 0 start with an area |
| 385 | // floor, which is what carries great water to the world tile. |
| 386 | let tags: &[(&str, &str)] = |
| 387 | &[("natural", "water"), ("water", "lake"), ("name", "Lake Superior")]; |
| 388 | let class = crate::schema::landtype::classify_early(tags, true) |
| 389 | .expect("a named lake classifies"); |
| 390 | assert_eq!(class.min_zoom, 0, "the named-lake start zoom"); |
| 391 | let features = vec![crate::extract::Feature { |
| 392 | class, |
| 393 | geometry: tile_build::geom::Geometry::Polygons(vec![vec![ring]]), |
| 394 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 395 | turn_fwd: Vec::new(), |
| 396 | turn_bwd: Vec::new(), |
| 397 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 398 | building: None, |
| 399 | }]; |
| 400 | let settings = crate::tiler::Settings { |
| 401 | build_id: 1, |
| 402 | scratch: std::env::temp_dir() |
| 403 | .join(format!("mamaps_superior_{}.tilechunks", std::process::id())), |
| 404 | force_chunk_spill_file: false, |
| 405 | dem: crate::dem::Dem::from_grids(14, 17, Vec::new()), |
| 406 | region_links: std::collections::HashMap::new(), |
| 407 | country_zooms: std::collections::HashMap::new(), |
| 408 | }; |
| 409 | let store = crate::store::Store::of(&features).expect("spill"); |
| 410 | let (bytes, _) = crate::tiler::build(&store, &settings).expect("build"); |
| 411 | |
| 412 | // A lake-kind polygon must be present at z0 (whole basin in a handful of |
| 413 | // tiles), z5 and z14. Count tiles carrying any lake-kind feature. |
| 414 | let mut saw = [false, false, false]; |
| 415 | for (id, _, body) in tilecodec::mamaps::read::read_all(&bytes).expect("read") { |
| 416 | let (z, _, _) = tilecodec::pmtiles::tile_zxy(id); |
| 417 | let slot = match z { |
| 418 | 0 => 0, |
| 419 | 5 => 1, |
| 420 | 14 => 2, |
| 421 | _ => continue, |
| 422 | }; |
| 423 | let body = Body::parse(&body).expect("parse"); |
| 424 | let Some(layer) = body.layer(dict::LAYER_LANDTYPE) else { |
| 425 | continue; |
| 426 | }; |
| 427 | let lake_kind = crate::schema::kind("lake"); |
| 428 | if layer.features.iter().any(|f| { |
| 429 | f.geom_type == tilecodec::mamaps::body::GEOM_POLYGON && f.kind == lake_kind |
| 430 | }) { |
| 431 | saw[slot] = true; |
| 432 | } |
| 433 | } |
| 434 | assert!(saw[0], " Superior water reaches z0"); |
| 435 | assert!(saw[1], "Superior water reaches z5"); |
| 436 | if crate::tiler::wash_cap_enabled() { |
| 437 | assert!(!saw[2], "wash cap drops lake water at z14 (overzoomed from z13)"); |
| 438 | } else { |
| 439 | assert!(saw[2], "Superior water reaches z14"); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /// **Country label gating, end to end.** Two `country` places through the real |
| 444 | /// pipeline with scored start zooms: a Russia-scale label (z0) and a Vatican-scale |
| 445 | /// label (z4). The z0 tile carries only Russia; the z4 tile carries both. A third |
| 446 | /// unscored country keeps the schema floor and rides every zoom — a missing score |
| 447 | /// must never hide a country. |
| 448 | #[test] |
| 449 | fn country_labels_start_at_their_scored_zoom() { |
| 450 | use crate::schema::Class; |
| 451 | use tilecodec::mamaps::body::Body; |
| 452 | use tilecodec::mamaps::dict; |
| 453 | |
| 454 | fn country(id: u64) -> crate::extract::Feature { |
| 455 | let class = crate::schema::places::classify( |
| 456 | &[("place", "country"), ("name", "Test")] as &[(&str, &str)], |
| 457 | ) |
| 458 | .expect("classified"); |
| 459 | crate::extract::Feature { |
| 460 | class, |
| 461 | geometry: tile_build::geom::Geometry::Points(vec![(10.0, 10.0)]), |
| 462 | name: Some("Test".to_string()), |
| 463 | id, |
| 464 | transit_color: 0, |
| 465 | transit_ordinal: 0, |
| 466 | transit_lanes: 0, |
| 467 | transit_taper: 0, |
| 468 | lane_count: 0, |
| 469 | turn_fwd: Vec::new(), |
| 470 | turn_bwd: Vec::new(), |
| 471 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 472 | building: None, |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | let russia = 1001u64; |
| 477 | let vatican = 1002u64; |
| 478 | let unscored = 1003u64; |
| 479 | let features = vec![country(russia), country(vatican), country(unscored)]; |
| 480 | let mut zooms: std::collections::HashMap<u64, u8> = std::collections::HashMap::new(); |
| 481 | zooms.insert(russia, 0); |
| 482 | zooms.insert(vatican, 4); |
| 483 | let settings = crate::tiler::Settings { |
| 484 | build_id: 1, |
| 485 | scratch: std::env::temp_dir() |
| 486 | .join(format!("mamaps_countries_{}.tilechunks", std::process::id())), |
| 487 | force_chunk_spill_file: false, |
| 488 | dem: crate::dem::Dem::from_grids(14, 17, Vec::new()), |
| 489 | region_links: std::collections::HashMap::new(), |
| 490 | country_zooms: zooms, |
| 491 | }; |
| 492 | let store = crate::store::Store::of(&features).expect("spill"); |
| 493 | let (bytes, _) = crate::tiler::build(&store, &settings).expect("build"); |
| 494 | |
| 495 | // Which ids does each zoom's `places` layer carry? |
| 496 | let mut at_zoom: std::collections::HashMap<u8, Vec<u64>> = |
| 497 | std::collections::HashMap::new(); |
| 498 | for (id, _, body) in tilecodec::mamaps::read::read_all(&bytes).expect("read") { |
| 499 | let (z, _, _) = tilecodec::pmtiles::tile_zxy(id); |
| 500 | if z != 0 && z != 4 { |
| 501 | continue; |
| 502 | } |
| 503 | let body = Body::parse(&body).expect("parse"); |
| 504 | let Some(layer) = body.layer(dict::LAYER_PLACES) else { |
| 505 | continue; |
| 506 | }; |
| 507 | let country_kind = crate::schema::kind("country"); |
| 508 | let has = |want: u64| { |
| 509 | layer |
| 510 | .features |
| 511 | .iter() |
| 512 | .enumerate() |
| 513 | .filter(|(_, f)| f.kind == country_kind) |
| 514 | .any(|(i, _)| body.feature_id(dict::LAYER_PLACES, i) == Some(want)) |
| 515 | }; |
| 516 | for want in [russia, vatican, unscored] { |
| 517 | if has(want) { |
| 518 | at_zoom.entry(z).or_default().push(want); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | let mut z0 = at_zoom.get(&0).cloned().unwrap_or_default(); |
| 523 | let mut z4 = at_zoom.get(&4).cloned().unwrap_or_default(); |
| 524 | z0.sort_unstable(); |
| 525 | z4.sort_unstable(); |
| 526 | z0.dedup(); |
| 527 | z4.dedup(); |
| 528 | assert_eq!(z0, vec![russia, unscored], "z0 carries Russia and the unscored country"); |
| 529 | assert_eq!( |
| 530 | z4, |
| 531 | vec![russia, vatican, unscored], |
| 532 | "z4 carries all three — Vatican joins, nothing leaves" |
| 533 | ); |
| 534 | } |
| 535 | } |