Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / tiler_part11.rs
tiler_part11.rs92 lines · 5.4 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests_part11 { |
| 3 | use super::*; |
| 4 | use super::tests::*; |
| 5 | use crate::schema::Class; |
| 6 | use tilecodec::mamaps::dict; |
| 7 | /// PLANET z14 overflow reproduction: one tile-layer with >65535 bodies through the REAL |
| 8 | /// encode+append+read path. This is the ONLY path north-america never exercised |
| 9 | /// (NA max 38,239). Dense cities at z14 (Jakarta etc.) cross 65535 and hit the |
| 10 | /// extended-count branch. Tests boundaries 65534/65535/65536 and a large 70k case, |
| 11 | /// plus that the common path (<65535) stays byte-identical (flag 0, reserved 0). |
| 12 | #[test] |
| 13 | fn a_tile_layer_with_more_than_65535_buildings_round_trips_through_the_full_tiler() { |
| 14 | let _guard = budget(); |
| 15 | par::set_threads(1); |
| 16 | // All features in one tiny patch so they fall into the same z14 tile(s). |
| 17 | // Use slightly distinct geometries so dedup does not collapse them. |
| 18 | // At z14 tile width is ~0.022 deg. Buildings have size 0.0003 deg; with |
| 19 | // jitter 0.00002 deg the whole cluster occupies <0.004 deg — well inside |
| 20 | // one tile interior (plus buffer), so at z14 it collapses to ONE tile. |
| 21 | // Previous jitter 0.00008 deg made 0.008 deg spread -> clipped across 2 tiles. |
| 22 | for n in [65534usize, 65535, 65536, 70000] { |
| 23 | let mut features = Vec::with_capacity(n); |
| 24 | for i in 0..n { |
| 25 | let jitter_x = (i % 200) as f64 * 0.00001; |
| 26 | let jitter_y = (i / 200) as f64 * 0.00001; |
| 27 | // Center at -122.005, 37.005 — well inside interior of tile 14/2628/6338 area |
| 28 | let lon = -122.005 + jitter_x; |
| 29 | let lat = 37.005 + jitter_y; |
| 30 | // Small square that stays inside tile even with simplification at z14 |
| 31 | features.push(Feature { |
| 32 | class: Class::area(dict::LAYER_BUILDINGS, crate::schema::kind("building"), 14), |
| 33 | geometry: square(lon, lat, 0.0003), |
| 34 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 35 | turn_fwd: Vec::new(), |
| 36 | turn_bwd: Vec::new(), |
| 37 | building: None, |
| 38 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 39 | }); |
| 40 | } |
| 41 | let store = spilled(&features); |
| 42 | let (bytes, _stats) = build(&store, &settings(14, 14)) |
| 43 | .unwrap_or_else(|e| panic!("build failed for n={n}: {e:?}")); |
| 44 | let entries = tilecodec::mamaps::read::read_all(&bytes) |
| 45 | .unwrap_or_else(|e| panic!("read_all failed for n={n}: {e:?}")); |
| 46 | let mut max_layer_len = 0usize; |
| 47 | let mut total = 0usize; |
| 48 | let mut any_extended = false; |
| 49 | for (_, _, body_bytes) in &entries { |
| 50 | let body = Body::parse(body_bytes) |
| 51 | .unwrap_or_else(|e| panic!("Body::parse failed for n={n}: {e:?}")); |
| 52 | // Check body flag for extended |
| 53 | if body_bytes.len() >= 12 && body_bytes[11] == tilecodec::mamaps::body::BODY_FLAG_EXTENDED_COUNTS { |
| 54 | any_extended = true; |
| 55 | } |
| 56 | if let Some(layer) = body.layer(dict::LAYER_BUILDINGS) { |
| 57 | max_layer_len = max_layer_len.max(layer.features.len()); |
| 58 | total += layer.features.len(); |
| 59 | // Also verify Body::raw_len prefix matches |
| 60 | assert_eq!(Body::raw_len(body_bytes).expect("raw_len") as usize, body_bytes.len()); |
| 61 | } |
| 62 | } |
| 63 | // n buildings may split across a few tiles (grid), so max may be <n but for |
| 64 | // this tight patch it should be close. For 70k we expect >65535 in one tile. |
| 65 | eprintln!("n={n} -> tiles {} max_buildings {max_layer_len} total {total} extended={any_extended}", entries.len()); |
| 66 | if n >= 65536 { |
| 67 | assert!(max_layer_len > 65535, "n={n} expected a layer >65535 but widest was {max_layer_len}"); |
| 68 | assert!(any_extended, "n={n} should have used extended encoding"); |
| 69 | } else { |
| 70 | // For 65534/65535 we expect NOT extended (common path byte-identical) |
| 71 | // However due to tiling across tiles, individual tile may be <n; just verify no panic and parse ok. |
| 72 | // If the body's n is <=65535 it should NOT have the flag unless another tile triggered it; |
| 73 | // the body-level flag is per-body, so bodies with <=65535 features stay flag 0. |
| 74 | // We don't assert global flag here to avoid false positive when n=65535 splits across 2 tiles. |
| 75 | } |
| 76 | // For small case 100, verify common path produces flag 0 on all bodies |
| 77 | if n <= 65535 { |
| 78 | for (_, _, body_bytes) in &entries { |
| 79 | // Only check bodies that actually overflow; small bodies should remain flag 0 |
| 80 | let body = Body::parse(body_bytes).expect("parse"); |
| 81 | if let Some(layer) = body.layer(dict::LAYER_BUILDINGS) { |
| 82 | if layer.features.len() <= 65535 { |
| 83 | assert_eq!(body_bytes[11], 0, "common path must be flag 0 for n={n} layer {}", layer.features.len()); |
| 84 | assert_eq!(&body_bytes[12..16], &[0,0,0,0], "reserved must be 0 for n={n}"); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | release_threads(); |
| 91 | } |
| 92 | } |