#[cfg(test)] mod tests_part9 { use super::*; use super::tests::*; use crate::schema::Class; use tilecodec::mamaps::dict; /// **Lane C: the widest tile-layer check covers all eight content layers.** /// A tile per layer is built through the real encode path and /// [`widest_layers`] reports each one — so a codec that dropped a layer's /// accounting would fail here rather than at the 65,535-feature cap on /// device. /// /// Fail-watch: delete a layer's `fetch_max` sample in `encode_batch` and /// this quotes `left: 0, right: 1` on that layer's widest count; restore /// after quoting. #[test] fn widest_tile_layer_reports_all_eight_layers() { use tilecodec::mamaps::dict::LAYERS; // Eight content layers: landtype..junction minus transit, whose geometry // comes from a GTFS export the fixture path does not carry. let layers: [(u8, Class, Geometry); 9] = [ ( dict::LAYER_LANDTYPE, Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("lake"), 0), square(-120.0, 35.0, 0.4), ), ( dict::LAYER_ROADS, Class::line(dict::LAYER_ROADS, crate::schema::kind("minor_road"), 0), Geometry::Lines(vec![vec![(-120.0, 35.0), (-119.0, 36.0)]]), ), ( dict::LAYER_BOUNDARIES, Class::line(dict::LAYER_BOUNDARIES, crate::schema::kind("country"), 0), Geometry::Lines(vec![vec![(-120.0, 35.0), (-119.0, 36.0)]]), ), ( dict::LAYER_BUILDINGS, Class::area(dict::LAYER_BUILDINGS, crate::schema::kind("building"), 14), square(-120.005, 35.005, 0.002), ), ( dict::LAYER_PLACES, Class::line(dict::LAYER_PLACES, crate::schema::kind("locality"), 0), Geometry::Points(vec![(-120.0, 35.0)]), ), ( dict::LAYER_POI, Class::line(dict::LAYER_POI, crate::schema::kind("cafe"), 0), Geometry::Points(vec![(-120.0, 35.0)]), ), ( dict::LAYER_TRAFFIC, crate::schema::traffic::traffic_class(), Geometry::Lines(vec![vec![(-120.0, 35.0), (-119.9996, 35.0004)]]), ), ( dict::LAYER_JUNCTION, crate::schema::junction::junction_class(), Geometry::Lines(vec![vec![(-120.0, 35.0), (-119.9996, 35.0004)]]), ), ( dict::LAYER_SAFETY, crate::schema::safety::classify(&[("highway", "stop")][..]).expect("stop"), Geometry::Points(vec![(-120.0, 35.0)]), ), ]; assert_eq!(LAYERS.len(), 10, "ten layers in the dictionary, nine tiled here"); let mut seen = std::collections::BTreeSet::new(); for (layer_id, class, geometry) in layers { let feature = Feature { class, geometry, name: None, id: if crate::extract::layer_tracks_ids(layer_id) { match layer_id { dict::LAYER_PLACES => { crate::extract::tagged_id(7, crate::extract::ELEMENT_NODE) } dict::LAYER_POI => { crate::extract::tagged_id(9, crate::extract::ELEMENT_NODE) } dict::LAYER_TRAFFIC => { crate::schema::traffic::pack_component_id(7, 0) } _ => tilecodec::mamaps::body::ID_NONE, } } else { 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(), }; let (bytes, _) = build(&spilled(&[feature]), &settings(14, 14)).expect("build"); let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read"); let mut count = 0usize; for (_, _, body) in &entries { let body = Body::parse(body).expect("parse"); count += body.layer(layer_id).map(|l| l.features.len()).unwrap_or(0); } assert!(count >= 1, "layer {layer_id} tiles at least one feature"); seen.insert(layer_id); } // And the running maximum saw every one of them through the real path. // `>= 1`: the static is process-global, so an earlier test's wider // tile may have already raised a layer's maximum. let widest: std::collections::BTreeMap = widest_layers().into_iter().collect(); for layer_id in seen { assert!( widest.get(&layer_id).copied().unwrap_or(0) >= 1, "layer {layer_id} reported through widest_layers", ); } } /// A measurement, not an assertion: build the same z14 region with and without a dense grid of /// traffic segments and print the compressed archive delta, so a real per-segment cost can be /// extrapolated to a region build without the California inputs to hand. Run explicitly: /// `cargo test traffic_layer_size_delta -- --ignored --nocapture`. #[test] #[ignore = "measurement; run with --ignored --nocapture"] fn traffic_layer_size_delta() { use crate::schema::traffic::{pack_component_id, traffic_class}; let n = 120i32; // 120x120 = 14,400 cells let coords = |c: i32, r: i32| { let x = -120.0 + c as f64 * 0.0016; let y = 35.0 + r as f64 * 0.0016; vec![vec![(x, y), (x + 0.0014, y)]] }; let road = |c: i32, r: i32| Feature { class: Class::line(dict::LAYER_ROADS, crate::schema::kind("minor_road"), 12), geometry: Geometry::Lines(coords(c, r)), 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(), }; let traffic = |c: i32, r: i32, i: u64| Feature { class: traffic_class(), geometry: Geometry::Lines(coords(c, r)), name: None, id: pack_component_id(i, 0), 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(), }; let mut base = Vec::new(); for r in 0..n { for c in 0..n { base.push(road(c, r)); } } let mut both = Vec::new(); for r in 0..n { for c in 0..n { both.push(road(c, r)); } } let mut i = 0u64; for r in 0..n { for c in 0..n { both.push(traffic(c, r, i)); i += 1; } } let without = build(&spilled(&base), &settings(12, 14)).expect("without").0; let with = build(&spilled(&both), &settings(12, 14)).expect("with").0; let segs = (n * n) as usize; let delta = with.len() as i64 - without.len() as i64; println!( "traffic size delta: {} segment(s) added {} compressed byte(s) ({:.1} B/segment); \ archive {} -> {} bytes (+{:.1}%)", segs, delta, delta as f64 / segs as f64, without.len(), with.len(), delta as f64 / without.len() as f64 * 100.0, ); } /// polygon per tile, so the id is the only thing that says those pieces are one region. This /// pins both halves: the id survives into `boundaries`, and it is the *same* id in every tile /// the region touches. Without the second half a mask can only punch out one tile. #[test] fn a_region_area_carries_one_id_across_every_tile_it_touches() { let osm = crate::extract::tagged_id(396_487, crate::extract::ELEMENT_RELATION); // Wide enough at z14 (a tile is ~0.022 deg) to be cut into several pieces. let region = Feature { class: Class::area( dict::LAYER_BOUNDARIES, crate::schema::kind("region_area"), 0, ), geometry: square(-120.0, 35.0, 0.1), name: None, id: osm, 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(), }; let border = Feature { class: Class::line( dict::LAYER_BOUNDARIES, crate::schema::kind("region"), 0, ), geometry: Geometry::Lines(vec![vec![ (-120.05, 34.95), (-119.95, 34.95), (-119.95, 35.05), ]]), 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(), }; let (bytes, _) = build(&spilled(&[region, border]), &settings(14, 14)).expect("build"); let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read"); let mut tiles_with_the_region = 0; for (_, _, body) in &entries { let body = Body::parse(body).expect("parse"); let Some(layer) = body.layer(dict::LAYER_BOUNDARIES) else { continue }; let mut here = false; for index in 0..layer.features.len() { match body.feature_id(dict::LAYER_BOUNDARIES, index) { // The region's shape. Every piece must name the same relation, or the pieces // cannot be gathered back into one region. Some(id) if id == osm => here = true, // A border line, which is coalesced and so has no id worth keeping. Some(id) => assert_eq!( id, tilecodec::mamaps::body::ID_NONE, "only the region shape may carry an id", ), None => panic!("the boundaries layer should have an id table"), } } if here { tiles_with_the_region += 1; } } assert!( tiles_with_the_region > 1, "the region should be clipped across several tiles, got {tiles_with_the_region}", ); } }