Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / tiler_part10.rs
tiler_part10.rs552 lines · 27.8 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests_part10 { |
| 3 | use super::*; |
| 4 | use super::tests::*; |
| 5 | use crate::schema::Class; |
| 6 | use tilecodec::mamaps::dict; |
| 7 | #[test] |
| 8 | fn a_lake_tiles_and_reads_back_out_of_the_archive() { |
| 9 | let features = vec![lake(-120.0, 35.0, 0.5, 0)]; |
| 10 | let (bytes, stats) = build(&spilled(&features), &settings(0, 6)).expect("build"); |
| 11 | check_not_empty(&stats).expect("not empty"); |
| 12 | |
| 13 | let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read back"); |
| 14 | assert!(!entries.is_empty()); |
| 15 | // Every stored body holds the water layer and nothing else. |
| 16 | for (_, _, body) in &entries { |
| 17 | let body = Body::parse(body).expect("parse"); |
| 18 | let layer = body.layer(dict::LAYER_LANDTYPE).expect("water"); |
| 19 | assert!(!layer.features.is_empty()); |
| 20 | assert_eq!(layer.features[0].geom_type, GEOM_POLYGON); |
| 21 | assert!(body.layer(dict::LAYER_BUILDINGS).is_none()); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// A feature's `min_zoom` is what keeps a world tile from carrying every pond. Enforced here, |
| 26 | /// so the archive does not hold what the style would not draw. |
| 27 | #[test] |
| 28 | fn a_feature_is_not_written_above_its_own_min_zoom() { |
| 29 | let features = vec![lake(-120.0, 35.0, 0.01, 10)]; |
| 30 | let (_, stats) = build(&spilled(&features), &settings(0, 11)).expect("build"); |
| 31 | for s in &stats { |
| 32 | if s.zoom < 10 { |
| 33 | assert_eq!(s.tiles, 0, "z{} should be empty", s.zoom); |
| 34 | } |
| 35 | } |
| 36 | assert!(stats.iter().any(|s| s.zoom >= 10 && s.tiles > 0), "and present once it is due"); |
| 37 | } |
| 38 | |
| 39 | /// **Invariant 4 and 5 together.** Ids ascend across the whole archive because `tile_id` is |
| 40 | /// zoom-major and every emit path is a `BTreeMap`, and the output is byte-identical run to run |
| 41 | /// because nothing iterates a hash map. |
| 42 | #[test] |
| 43 | fn the_output_is_byte_identical_and_its_ids_ascend() { |
| 44 | let features = vec![ |
| 45 | lake(-120.0, 35.0, 0.4, 0), |
| 46 | lake(-119.0, 36.0, 0.3, 0), |
| 47 | Feature { |
| 48 | class: Class::area(dict::LAYER_BUILDINGS, crate::schema::kind("building"), 0), |
| 49 | geometry: square(-120.1, 35.1, 0.02), |
| 50 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 51 | turn_fwd: Vec::new(), |
| 52 | turn_bwd: Vec::new(), |
| 53 | building: None, |
| 54 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 55 | }, |
| 56 | ]; |
| 57 | let first = build(&spilled(&features), &settings(0, 8)).expect("first").0; |
| 58 | let second = build(&spilled(&features), &settings(0, 8)).expect("second").0; |
| 59 | assert_eq!(first, second, "two runs of the same input"); |
| 60 | |
| 61 | let entries = tilecodec::mamaps::read::read_all(&first).expect("read"); |
| 62 | assert!(entries.windows(2).all(|p| p[1].0 > p[0].0), "ids ascend"); |
| 63 | for (id, _, _) in &entries { |
| 64 | let (z, _, _) = tilecodec::pmtiles::tile_zxy(*id); |
| 65 | let range = tilecodec::pmtiles::zoom_base(z)..tilecodec::pmtiles::zoom_base(z + 1); |
| 66 | assert!(range.contains(id), "id {id} is outside z{z}"); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// Two layers in one tile, which is the shape the format exists for: a cold tile is one range |
| 71 | /// request whatever the style is drawing. |
| 72 | #[test] |
| 73 | fn a_tile_carrying_two_layers_holds_them_both() { |
| 74 | let features = vec![ |
| 75 | lake(-120.0, 35.0, 0.02, 0), |
| 76 | Feature { |
| 77 | class: Class::area(dict::LAYER_BUILDINGS, crate::schema::kind("building"), 0), |
| 78 | geometry: square(-120.005, 35.005, 0.002), |
| 79 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 80 | turn_fwd: Vec::new(), |
| 81 | turn_bwd: Vec::new(), |
| 82 | building: None, |
| 83 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 84 | }, |
| 85 | ]; |
| 86 | let (bytes, _) = build(&spilled(&features), &settings(14, 14)).expect("build"); |
| 87 | let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read"); |
| 88 | let both = entries.iter().any(|(_, _, body)| { |
| 89 | let body = Body::parse(body).expect("parse"); |
| 90 | body.layer(dict::LAYER_LANDTYPE).is_some() && body.layer(dict::LAYER_BUILDINGS).is_some() |
| 91 | }); |
| 92 | assert!(both, "some tile should carry both"); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn a_build_that_matched_nothing_fails_rather_than_publishing_an_empty_archive() { |
| 97 | let stats = vec![ZoomStats { zoom: 0, ..ZoomStats::default() }]; |
| 98 | assert!(check_not_empty(&stats).is_err()); |
| 99 | assert!(build(&spilled(&[]), &settings(0, 2)).is_err(), "the writer refuses an empty archive"); |
| 100 | } |
| 101 | |
| 102 | /// Simplification is per zoom, so a shallow tile holds fewer points for the same shape. If this |
| 103 | /// ever inverts, the tolerance is being applied at the wrong end. |
| 104 | #[test] |
| 105 | fn a_shallow_zoom_carries_fewer_points_than_a_deep_one() { |
| 106 | // A wiggly line, so there is something to simplify away. |
| 107 | let points: Vec<(f64, f64)> = (0..200) |
| 108 | .map(|i| (-120.0 + i as f64 * 0.001, 35.0 + (i % 3) as f64 * 0.001 * 0.5)) |
| 109 | .collect(); |
| 110 | let features = vec![Feature { |
| 111 | class: Class::line(dict::LAYER_LANDTYPE, crate::schema::kind("river"), 0), |
| 112 | geometry: Geometry::Lines(vec![points]), |
| 113 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 114 | turn_fwd: Vec::new(), |
| 115 | turn_bwd: Vec::new(), |
| 116 | building: None, |
| 117 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 118 | }]; |
| 119 | // v8: the landtype wash tiles to z14, so the deep zoom holds the full shape. |
| 120 | let (_, stats) = build(&spilled(&features), &settings(6, 14)).expect("build"); |
| 121 | let at = |z: u8| stats.iter().find(|s| s.zoom == z).expect("zoom").points; |
| 122 | assert!(at(6) < at(14), "z6 has {} points, z14 has {}", at(6), at(14)); |
| 123 | } |
| 124 | |
| 125 | /// **The property the parallel tiler exists to keep.** The archive is a function of the input, |
| 126 | /// not of the thread count. |
| 127 | /// |
| 128 | /// One thread is not a formality: it is the configuration where the reader and the single worker |
| 129 | /// share one queue, and it is the configuration a producer running on the pool would deadlock |
| 130 | /// in. Three is the awkward one, where chunks outnumber threads unevenly and completion order |
| 131 | /// is guaranteed not to be read order. |
| 132 | #[test] |
| 133 | fn the_archive_is_identical_at_every_thread_count() { |
| 134 | let _budget = budget(); |
| 135 | let store = spilled(&a_crowd()); |
| 136 | // Small enough that the crowd above becomes many chunks, so the merge is doing real work at |
| 137 | // every thread count rather than folding one chunk into itself. |
| 138 | set_chunk_vertices(64); |
| 139 | par::set_threads(1); |
| 140 | let (one, first_stats) = build(&store, &settings(0, 12)).expect("one thread"); |
| 141 | for threads in [2usize, 3, 8, 17] { |
| 142 | par::set_threads(threads); |
| 143 | let (bytes, stats) = build(&store, &settings(0, 12)).expect("many threads"); |
| 144 | assert_eq!(bytes.len(), one.len(), "{threads} threads changed the archive length"); |
| 145 | assert_eq!(bytes, one, "{threads} threads changed the archive bytes"); |
| 146 | // The report is published beside the archive and diffed too, so it has to agree as |
| 147 | // well: a counter summed in completion order would still be right, and one accumulated |
| 148 | // per chunk into a shared total would not. |
| 149 | for (a, b) in first_stats.iter().zip(&stats) { |
| 150 | assert_eq!( |
| 151 | (a.zoom, a.tiles, a.features, a.points, a.dropped, a.bytes), |
| 152 | (b.zoom, b.tiles, b.features, b.points, b.dropped, b.bytes), |
| 153 | "{threads} threads changed the z{} counters", |
| 154 | a.zoom, |
| 155 | ); |
| 156 | assert_eq!(a.rings, b.rings, "{threads} threads changed z{} stage C", a.zoom); |
| 157 | } |
| 158 | } |
| 159 | set_chunk_vertices(0); |
| 160 | release_threads(); |
| 161 | } |
| 162 | |
| 163 | /// The stronger claim, and the one that makes the thread count irrelevant rather than merely |
| 164 | /// tested: concatenating a partition of the feature stream in partition order reproduces the |
| 165 | /// stream, so **where** the chunk boundaries fall cannot matter either. |
| 166 | /// |
| 167 | /// A chunk of one vertex means one chunk per feature — every tile in the merge is then assembled |
| 168 | /// from as many pieces as it has features, which is the worst case for [`concatenate`] and the |
| 169 | /// one where a wrong `parts_offset` could not hide. |
| 170 | #[test] |
| 171 | fn the_archive_is_identical_however_the_features_are_chunked() { |
| 172 | let _budget = budget(); |
| 173 | let store = spilled(&a_crowd()); |
| 174 | par::set_threads(4); |
| 175 | set_chunk_vertices(1); |
| 176 | let one_per_feature = build(&store, &settings(0, 12)).expect("tiny chunks").0; |
| 177 | for size in [7usize, 64, 4096, 1 << 20] { |
| 178 | set_chunk_vertices(size); |
| 179 | let bytes = build(&store, &settings(0, 12)).expect("build").0; |
| 180 | assert_eq!(bytes, one_per_feature, "a chunk of {size} vertices changed the archive"); |
| 181 | } |
| 182 | set_chunk_vertices(0); |
| 183 | release_threads(); |
| 184 | } |
| 185 | |
| 186 | /// Feature order **within a tile layer** is the store's order, asserted rather than assumed. |
| 187 | /// |
| 188 | /// This is the failure the whole module is arranged around, and it is invisible from outside: a |
| 189 | /// merge that folded chunks in completion order would still put every feature in the right tile, |
| 190 | /// still draw the right picture, and still write different bytes every run. So each feature |
| 191 | /// carries a `kind` counting up in store order, and the decoded archive has to count up |
| 192 | /// too. (`kind`, not `kind_detail`: v10 minimal records drop detail, so the |
| 193 | /// counter must ride in a surviving field.) |
| 194 | #[test] |
| 195 | fn feature_order_within_a_tile_layer_is_the_store_order() { |
| 196 | let _budget = budget(); |
| 197 | // All in one small patch so they pile into the same handful of z14 tiles, each labelled with |
| 198 | // its position in the store. |
| 199 | let features: Vec<Feature> = (0..40u16) |
| 200 | .map(|i| Feature { |
| 201 | class: Class { |
| 202 | kind: 1000 + i, |
| 203 | ..Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("lake"), 0) |
| 204 | }, |
| 205 | geometry: square(-120.0 + i as f64 * 0.00005, 35.0, 0.004), |
| 206 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 207 | turn_fwd: Vec::new(), |
| 208 | turn_bwd: Vec::new(), |
| 209 | building: None, |
| 210 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 211 | }) |
| 212 | .collect(); |
| 213 | let store = spilled(&features); |
| 214 | set_chunk_vertices(1); |
| 215 | par::set_threads(8); |
| 216 | let (bytes, _) = build(&store, &settings(14, 14)).expect("build"); |
| 217 | set_chunk_vertices(0); |
| 218 | release_threads(); |
| 219 | |
| 220 | let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read"); |
| 221 | let mut widest = 0usize; |
| 222 | for (id, _, body) in &entries { |
| 223 | let body = Body::parse(body).expect("parse"); |
| 224 | let Some(layer) = body.layer(dict::LAYER_LANDTYPE) else { continue }; |
| 225 | let order: Vec<u16> = layer.features.iter().map(|f| f.kind).collect(); |
| 226 | let mut ascending = order.clone(); |
| 227 | ascending.sort_unstable(); |
| 228 | assert_eq!(order, ascending, "tile {id} holds its features out of store order"); |
| 229 | widest = widest.max(order.len()); |
| 230 | } |
| 231 | // And that assertion has to have had something to bite on: a tile carrying one feature is |
| 232 | // in order however badly the merge behaves. |
| 233 | assert!(widest >= 20, "no tile gathered enough features to order (widest was {widest})"); |
| 234 | } |
| 235 | |
| 236 | /// [`concatenate`] against the thing it claims to equal: one layer built by pushing both |
| 237 | /// features in one go. Not "the offsets look plausible" but "the layer is the same layer". |
| 238 | #[test] |
| 239 | fn concatenating_two_chunks_of_a_layer_is_one_layer() { |
| 240 | let class = Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("lake"), 0); |
| 241 | let feature = Feature { class, geometry: square(0.0, 0.0, 1.0), name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, turn_fwd: Vec::new(), turn_bwd: Vec::new(), building: None, carriageway: tilecodec::mamaps::body::Carriageway::default() }; |
| 242 | // Tile-local already, so the fixture is about the arenas rather than about projection, and |
| 243 | // big enough that no minimum-area floor can drop it. |
| 244 | let box_at = |x: i32| { |
| 245 | IntGeometry::Polygons(vec![vec![vec![ |
| 246 | (x, 0), |
| 247 | (x + 500, 0), |
| 248 | (x + 500, 500), |
| 249 | (x, 500), |
| 250 | (x, 0), |
| 251 | ]]]) |
| 252 | }; |
| 253 | |
| 254 | let mut together = ChunkEntry::new(dict::LAYER_LANDTYPE); |
| 255 | assert_eq!(push(&mut together, &feature, &box_at(0)), (1, 5)); |
| 256 | push(&mut together, &feature, &box_at(1000)); |
| 257 | |
| 258 | let mut first = ChunkEntry::new(dict::LAYER_LANDTYPE); |
| 259 | push(&mut first, &feature, &box_at(0)); |
| 260 | let mut second = ChunkEntry::new(dict::LAYER_LANDTYPE); |
| 261 | push(&mut second, &feature, &box_at(1000)); |
| 262 | concatenate(&mut first, &mut second); |
| 263 | |
| 264 | assert_eq!(first, together, "two chunks concatenated are not the one-pass layer"); |
| 265 | // Spelled out as well, because `assert_eq` on the whole layer would also pass if both were |
| 266 | // empty, and an arena its parts do not tile exactly is what the encoder rejects. |
| 267 | assert_eq!(first.layer.features.len(), 2); |
| 268 | assert_eq!(first.layer.features[1].parts_offset, 1); |
| 269 | assert_eq!(first.layer.parts[1].coord_start, first.layer.parts[0].point_count); |
| 270 | assert_eq!(first.layer.coords.len(), 10); |
| 271 | } |
| 272 | |
| 273 | /// The merge's contract on its own, without a build around it: ascending tiles, and layers in id |
| 274 | /// order within a tile, with same-layer contributions from several chunks collapsed into one. |
| 275 | /// |
| 276 | /// Written through a [`ChunkSpill`] first, because that is the only way the merge is reachable |
| 277 | /// now — and so this doubles as the round-trip check on a chunk whose entries all have empty |
| 278 | /// arenas. |
| 279 | #[test] |
| 280 | fn the_merge_yields_ascending_tiles_with_their_layers_in_id_order() { |
| 281 | let mut early: Chunk = BTreeMap::new(); |
| 282 | early.insert((10, 3), ChunkEntry::new(3)); |
| 283 | early.insert((30, 1), ChunkEntry::new(1)); |
| 284 | let mut middle: Chunk = BTreeMap::new(); |
| 285 | middle.insert((10, 1), ChunkEntry::new(1)); |
| 286 | middle.insert((20, 2), ChunkEntry::new(2)); |
| 287 | let mut late: Chunk = BTreeMap::new(); |
| 288 | late.insert((10, 3), ChunkEntry::new(3)); |
| 289 | |
| 290 | let spill = ChunkSpill::create(scratch()).expect("scratch"); |
| 291 | let refs: Vec<ChunkRef> = [early, middle, late] |
| 292 | .into_iter() |
| 293 | .map(|chunk| spill.write_chunk(chunk).expect("spill a chunk")) |
| 294 | .collect(); |
| 295 | |
| 296 | let merged: Vec<(u64, Vec<u8>)> = merge(&refs, &spill) |
| 297 | .map(|tile| tile.expect("read a tile back")) |
| 298 | .map(|(id, layers)| (id, layers.iter().map(|l| l.layer.layer_id).collect())) |
| 299 | .collect(); |
| 300 | // Tile 10 carries layer 1 before layer 3 even though layer 3 was read first, and its two |
| 301 | // separate layer-3 pieces arrive as one layer rather than two. |
| 302 | assert_eq!(merged, vec![(10, vec![1, 3]), (20, vec![2]), (30, vec![1])]); |
| 303 | spill.check_books().expect("the books balance"); |
| 304 | } |
| 305 | |
| 306 | /// The read window is a memory/syscall trade and must not be observable in the archive. Forced |
| 307 | /// here at both clamps and either side of one entry's header, because a real build only ever |
| 308 | /// reaches one clamp and which one depends on the extract. |
| 309 | #[test] |
| 310 | fn the_archive_is_identical_however_the_read_window_is_sized() { |
| 311 | let _guard = budget(); |
| 312 | par::set_threads(4); |
| 313 | // Small chunks, so a zoom has many streams and a tile's layers really do come from several. |
| 314 | set_chunk_vertices(64); |
| 315 | let store = spilled(&a_crowd()); |
| 316 | |
| 317 | let want = build(&store, &settings(0, 14)).expect("build").0; |
| 318 | for window in [1usize, 23, 24, 25, 4096, tilespill::MIN_WINDOW, tilespill::READ_BUDGET / 8, tilespill::MAX_WINDOW] { |
| 319 | tilespill::set_read_window(window); |
| 320 | let got = build(&store, &settings(0, 14)).expect("build").0; |
| 321 | assert_eq!(got, want, "a {window}-byte read window moved the archive"); |
| 322 | } |
| 323 | |
| 324 | tilespill::set_read_window(0); |
| 325 | set_chunk_vertices(0); |
| 326 | release_threads(); |
| 327 | } |
| 328 | |
| 329 | /// **Phase B: a full-bleed single-water-polygon tile is water background, not a tile.** |
| 330 | /// A tile whose only content is one water polygon covering the whole tile draws exactly |
| 331 | /// what the style's water background already paints, so it is `None` (not stored) like |
| 332 | /// an emptied tile — and the reader overzooms the parent instead of fetching a body. |
| 333 | #[test] |
| 334 | fn a_full_bleed_water_tile_is_not_stored() { |
| 335 | use tilecodec::pmtiles::{tile_id, tile_zxy}; |
| 336 | // A water square far larger than one tile, so interior tiles clip full-bleed while the |
| 337 | // build still has edge tiles carrying real geometry (the writer refuses an empty archive). |
| 338 | let sea = Feature { |
| 339 | class: Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("sea"), 0), |
| 340 | geometry: square(-10.0, -10.0, 20.0), |
| 341 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 342 | turn_fwd: Vec::new(), |
| 343 | turn_bwd: Vec::new(), |
| 344 | building: None, |
| 345 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 346 | }; |
| 347 | let (bytes, _) = build(&spilled(&[sea]), &settings(0, 6)).expect("build"); |
| 348 | let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read"); |
| 349 | // Every stored tile must carry something drawable beyond a full-bleed water fill: |
| 350 | // either a second feature/layer, a hole (island), or a partial-bleed ring. |
| 351 | let extent = crate::tiler::EXTENT as f64; |
| 352 | for (id, _, body) in &entries { |
| 353 | let body = Body::parse(body).expect("parse"); |
| 354 | let Some(layer) = body.layer(dict::LAYER_LANDTYPE) else { continue }; |
| 355 | if layer.features.len() != 1 { |
| 356 | continue; |
| 357 | } |
| 358 | let feature = &layer.features[0]; |
| 359 | let water = matches!( |
| 360 | tilecodec::mamaps::dict::KINDS.get(feature.kind as usize - 1).copied(), |
| 361 | Some( |
| 362 | "ocean" | "sea" | "lake" | "water" | "river" | "stream" | "bay" |
| 363 | | "strait" | "fjord" | "reef" | "canal" | "dock", |
| 364 | ), |
| 365 | ); |
| 366 | if !water || feature.geom_type != GEOM_POLYGON || feature.part_count != 1 { |
| 367 | continue; |
| 368 | } |
| 369 | let part = &layer.parts[feature.parts_offset as usize]; |
| 370 | let start = part.coord_start as usize; |
| 371 | let points = &layer.coords[start..start + part.point_count as usize]; |
| 372 | let (mut min_x, mut min_y) = (i16::MAX, i16::MAX); |
| 373 | let (mut max_x, mut max_y) = (i16::MIN, i16::MIN); |
| 374 | for &(x, y) in points { |
| 375 | min_x = min_x.min(x); |
| 376 | min_y = min_y.min(y); |
| 377 | max_x = max_x.max(x); |
| 378 | max_y = max_y.max(y); |
| 379 | } |
| 380 | let full = min_x as f64 <= 0.5 |
| 381 | && min_y as f64 <= 0.5 |
| 382 | && max_x as f64 >= extent - 0.5 |
| 383 | && max_y as f64 >= extent - 0.5; |
| 384 | let (z, x, y) = tile_zxy(*id); |
| 385 | assert!( |
| 386 | !full, |
| 387 | "z{z}/{x}/{y} is a full-bleed single-water-polygon tile and must not be stored", |
| 388 | ); |
| 389 | } |
| 390 | // And the interior really is empty: a tile strictly inside the square resolves to no |
| 391 | // body, so the reader overzooms the parent whose background shows through. |
| 392 | let _ = tile_id(0, 0, 0); |
| 393 | } |
| 394 | |
| 395 | /// **Phase B: a water tile with an island hole is drawable land and stays stored.** |
| 396 | /// The narrow check must not fire when the polygon carries a hole — that hole is an |
| 397 | /// island, and dropping the tile would delete land. |
| 398 | #[test] |
| 399 | fn a_water_tile_with_a_hole_stays_stored() { |
| 400 | // A sea square with a smaller land square punched out of its middle is modelled here |
| 401 | // as two features: the sea polygon plus a land-base polygon inside it. The tile |
| 402 | // covering both carries two features, so `is_water_background` refuses it. |
| 403 | let sea = Feature { |
| 404 | class: Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("sea"), 0), |
| 405 | geometry: square(-10.0, -10.0, 20.0), |
| 406 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 407 | turn_fwd: Vec::new(), |
| 408 | turn_bwd: Vec::new(), |
| 409 | building: None, |
| 410 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 411 | }; |
| 412 | let island = Feature { |
| 413 | class: Class::area(dict::LAYER_LANDTYPE, tilecodec::mamaps::dict::NONE, 0), |
| 414 | geometry: square(-1.0, -1.0, 2.0), |
| 415 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 416 | turn_fwd: Vec::new(), |
| 417 | turn_bwd: Vec::new(), |
| 418 | building: None, |
| 419 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 420 | }; |
| 421 | let (bytes, _) = build(&spilled(&[sea, island]), &settings(0, 6)).expect("build"); |
| 422 | let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read"); |
| 423 | let mut saw_land = false; |
| 424 | for (_, _, body) in &entries { |
| 425 | let body = Body::parse(body).expect("parse"); |
| 426 | if let Some(layer) = body.layer(dict::LAYER_LANDTYPE) { |
| 427 | if layer.features.iter().any(|f| f.kind == tilecodec::mamaps::dict::NONE) { |
| 428 | saw_land = true; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | assert!(saw_land, "the island's land base must survive Phase B"); |
| 433 | } |
| 434 | |
| 435 | /// The wash-cap predicate, pinned without touching process-global state: |
| 436 | /// z14 landtype areas with `min_zoom != 14` drop; detail areas, all lines, |
| 437 | /// other layers and all other zooms survive. |
| 438 | #[test] |
| 439 | fn the_wash_cap_drops_only_z14_wash_areas() { |
| 440 | use tilecodec::mamaps::dict; |
| 441 | let lt = dict::LAYER_LANDTYPE; |
| 442 | // Wash areas drop at z14 whatever their min_zoom below the max. |
| 443 | for min_zoom in [0u8, 6, 8, 10, 13] { |
| 444 | assert!(super::wash_drops_kind(lt, true, min_zoom, 14), "min_zoom {min_zoom}"); |
| 445 | } |
| 446 | // Detail areas (min_zoom == max) survive: overzoom cannot recover them. |
| 447 | assert!(!super::wash_drops_kind(lt, true, 14, 14)); |
| 448 | // Lines survive at every zoom: cheap, high-salience, never wash. |
| 449 | assert!(!super::wash_drops_kind(lt, false, 0, 14)); |
| 450 | assert!(!super::wash_drops_kind(lt, false, 8, 14)); |
| 451 | // Other layers are untouched by the cap. |
| 452 | assert!(!super::wash_drops_kind(dict::LAYER_ROADS, true, 0, 14)); |
| 453 | assert!(!super::wash_drops_kind(dict::LAYER_BUILDINGS, true, 0, 14)); |
| 454 | // Every other zoom is untouched, including z13 (the overzoom source). |
| 455 | for z in [0u8, 6, 12, 13] { |
| 456 | assert!(!super::wash_drops_kind(lt, true, 0, z), "z{z}"); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /// The wash/detail partition is a schema fact, pinned so drift is |
| 461 | /// deliberate: exactly the six street-level area kinds sit at min_zoom 14. |
| 462 | #[test] |
| 463 | fn the_detail_kinds_are_exactly_the_minzoom_14_areas() { |
| 464 | use crate::schema::landtype; |
| 465 | let detail = ["swimming_pool", "dog_park", "playground", "pitch", "kindergarten", "platform"]; |
| 466 | for (key, value) in [ |
| 467 | ("leisure", "swimming_pool"), |
| 468 | ("leisure", "dog_park"), |
| 469 | ("leisure", "playground"), |
| 470 | ("leisure", "pitch"), |
| 471 | ("amenity", "kindergarten"), |
| 472 | ("public_transport", "platform"), |
| 473 | ] { |
| 474 | let tags: &[(&str, &str)] = &[(key, value)]; |
| 475 | let class = landtype::classify_late(tags).expect(value); |
| 476 | assert_eq!(class.min_zoom, 14, "{value} must be a detail kind"); |
| 477 | assert!(class.area); |
| 478 | } |
| 479 | // And the wash representatives sit below it (`natural=water` is an |
| 480 | // early rule — water is classified before roads — so it goes through |
| 481 | // `classify_early`; the rest are late). |
| 482 | for (key, value) in [ |
| 483 | ("natural", "wood"), |
| 484 | ("landuse", "farmland"), |
| 485 | ("leisure", "park"), |
| 486 | ("boundary", "national_park"), |
| 487 | ] { |
| 488 | let tags: &[(&str, &str)] = &[(key, value)]; |
| 489 | let class = landtype::classify_late(tags).expect(value); |
| 490 | assert!(class.min_zoom < 14, "{value} must be wash, not detail"); |
| 491 | } |
| 492 | { |
| 493 | let tags: &[(&str, &str)] = &[("natural", "water")]; |
| 494 | let class = landtype::classify_early(tags, true).expect("water"); |
| 495 | assert!(class.min_zoom < 14, "water must be wash, not detail"); |
| 496 | } |
| 497 | assert_eq!(detail.len(), 6); |
| 498 | } |
| 499 | |
| 500 | /// End to end: at z14 a wash lake is gone while a detail |
| 501 | /// pool and a river line survive; at z13 and below the lake is untouched. |
| 502 | #[test] |
| 503 | fn the_wash_cap_keeps_detail_and_lines_but_not_wash_at_z14() { |
| 504 | let pool = Feature { |
| 505 | class: Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("swimming_pool"), 14), |
| 506 | geometry: square(-122.0, 37.0, 0.005), |
| 507 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 508 | turn_fwd: Vec::new(), |
| 509 | turn_bwd: Vec::new(), |
| 510 | building: None, |
| 511 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 512 | }; |
| 513 | let river = Feature { |
| 514 | class: Class::line(dict::LAYER_LANDTYPE, crate::schema::kind("river"), 8), |
| 515 | geometry: Geometry::Lines(vec![vec![ |
| 516 | (-122.0, 37.0), |
| 517 | (-121.9, 37.1), |
| 518 | ]]), |
| 519 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 520 | turn_fwd: Vec::new(), |
| 521 | turn_bwd: Vec::new(), |
| 522 | building: None, |
| 523 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 524 | }; |
| 525 | let features = vec![lake(-122.0, 37.0, 0.5, 0), pool, river]; |
| 526 | let (bytes, _) = build(&spilled(&features), &settings(0, 14)).expect("build"); |
| 527 | let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read"); |
| 528 | let kinds_at = |want_z: u8| { |
| 529 | let mut kinds = std::collections::BTreeSet::new(); |
| 530 | for (id, _, body) in &entries { |
| 531 | let (z, _, _) = tilecodec::pmtiles::tile_zxy(*id); |
| 532 | if z != want_z { |
| 533 | continue; |
| 534 | } |
| 535 | let body = Body::parse(body).expect("parse"); |
| 536 | if let Some(layer) = body.layer(dict::LAYER_LANDTYPE) { |
| 537 | kinds.extend(layer.features.iter().map(|f| f.kind)); |
| 538 | } |
| 539 | } |
| 540 | kinds |
| 541 | }; |
| 542 | let z14 = kinds_at(14); |
| 543 | let pool_id = crate::schema::kind("swimming_pool"); |
| 544 | let river_id = crate::schema::kind("river"); |
| 545 | let lake_id = crate::schema::kind("lake"); |
| 546 | assert!(z14.contains(&pool_id), "detail pool survives at z14"); |
| 547 | assert!(z14.contains(&river_id), "river line survives at z14"); |
| 548 | assert!(!z14.contains(&lake_id), "wash lake is gone at z14"); |
| 549 | let z13 = kinds_at(13); |
| 550 | assert!(z13.contains(&lake_id), "wash lake survives at z13"); |
| 551 | } |
| 552 | } |