Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / tiler_part9.rs
tiler_part9.rs284 lines · 11.5 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests_part9 { |
| 3 | use super::*; |
| 4 | use super::tests::*; |
| 5 | use crate::schema::Class; |
| 6 | use tilecodec::mamaps::dict; |
| 7 | /// **Lane C: the widest tile-layer check covers all eight content layers.** |
| 8 | /// A tile per layer is built through the real encode path and |
| 9 | /// [`widest_layers`] reports each one — so a codec that dropped a layer's |
| 10 | /// accounting would fail here rather than at the 65,535-feature cap on |
| 11 | /// device. |
| 12 | /// |
| 13 | /// Fail-watch: delete a layer's `fetch_max` sample in `encode_batch` and |
| 14 | /// this quotes `left: 0, right: 1` on that layer's widest count; restore |
| 15 | /// after quoting. |
| 16 | #[test] |
| 17 | fn widest_tile_layer_reports_all_eight_layers() { |
| 18 | use tilecodec::mamaps::dict::LAYERS; |
| 19 | // Eight content layers: landtype..junction minus transit, whose geometry |
| 20 | // comes from a GTFS export the fixture path does not carry. |
| 21 | let layers: [(u8, Class, Geometry); 9] = [ |
| 22 | ( |
| 23 | dict::LAYER_LANDTYPE, |
| 24 | Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("lake"), 0), |
| 25 | square(-120.0, 35.0, 0.4), |
| 26 | ), |
| 27 | ( |
| 28 | dict::LAYER_ROADS, |
| 29 | Class::line(dict::LAYER_ROADS, crate::schema::kind("minor_road"), 0), |
| 30 | Geometry::Lines(vec![vec![(-120.0, 35.0), (-119.0, 36.0)]]), |
| 31 | ), |
| 32 | ( |
| 33 | dict::LAYER_BOUNDARIES, |
| 34 | Class::line(dict::LAYER_BOUNDARIES, crate::schema::kind("country"), 0), |
| 35 | Geometry::Lines(vec![vec![(-120.0, 35.0), (-119.0, 36.0)]]), |
| 36 | ), |
| 37 | ( |
| 38 | dict::LAYER_BUILDINGS, |
| 39 | Class::area(dict::LAYER_BUILDINGS, crate::schema::kind("building"), 14), |
| 40 | square(-120.005, 35.005, 0.002), |
| 41 | ), |
| 42 | ( |
| 43 | dict::LAYER_PLACES, |
| 44 | Class::line(dict::LAYER_PLACES, crate::schema::kind("locality"), 0), |
| 45 | Geometry::Points(vec![(-120.0, 35.0)]), |
| 46 | ), |
| 47 | ( |
| 48 | dict::LAYER_POI, |
| 49 | Class::line(dict::LAYER_POI, crate::schema::kind("cafe"), 0), |
| 50 | Geometry::Points(vec![(-120.0, 35.0)]), |
| 51 | ), |
| 52 | ( |
| 53 | dict::LAYER_TRAFFIC, |
| 54 | crate::schema::traffic::traffic_class(), |
| 55 | Geometry::Lines(vec![vec![(-120.0, 35.0), (-119.9996, 35.0004)]]), |
| 56 | ), |
| 57 | ( |
| 58 | dict::LAYER_JUNCTION, |
| 59 | crate::schema::junction::junction_class(), |
| 60 | Geometry::Lines(vec![vec![(-120.0, 35.0), (-119.9996, 35.0004)]]), |
| 61 | ), |
| 62 | ( |
| 63 | dict::LAYER_SAFETY, |
| 64 | crate::schema::safety::classify(&[("highway", "stop")][..]).expect("stop"), |
| 65 | Geometry::Points(vec![(-120.0, 35.0)]), |
| 66 | ), |
| 67 | ]; |
| 68 | assert_eq!(LAYERS.len(), 10, "ten layers in the dictionary, nine tiled here"); |
| 69 | let mut seen = std::collections::BTreeSet::new(); |
| 70 | for (layer_id, class, geometry) in layers { |
| 71 | let feature = Feature { |
| 72 | class, |
| 73 | geometry, |
| 74 | name: None, |
| 75 | id: if crate::extract::layer_tracks_ids(layer_id) { |
| 76 | match layer_id { |
| 77 | dict::LAYER_PLACES => { |
| 78 | crate::extract::tagged_id(7, crate::extract::ELEMENT_NODE) |
| 79 | } |
| 80 | dict::LAYER_POI => { |
| 81 | crate::extract::tagged_id(9, crate::extract::ELEMENT_NODE) |
| 82 | } |
| 83 | dict::LAYER_TRAFFIC => { |
| 84 | crate::schema::traffic::pack_component_id(7, 0) |
| 85 | } |
| 86 | _ => tilecodec::mamaps::body::ID_NONE, |
| 87 | } |
| 88 | } else { |
| 89 | tilecodec::mamaps::body::ID_NONE |
| 90 | }, |
| 91 | transit_color: 0, |
| 92 | transit_ordinal: 0, |
| 93 | transit_lanes: 0, |
| 94 | transit_taper: 0, |
| 95 | lane_count: 0, |
| 96 | turn_fwd: Vec::new(), |
| 97 | turn_bwd: Vec::new(), |
| 98 | building: None, |
| 99 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 100 | }; |
| 101 | let (bytes, _) = build(&spilled(&[feature]), &settings(14, 14)).expect("build"); |
| 102 | let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read"); |
| 103 | let mut count = 0usize; |
| 104 | for (_, _, body) in &entries { |
| 105 | let body = Body::parse(body).expect("parse"); |
| 106 | count += body.layer(layer_id).map(|l| l.features.len()).unwrap_or(0); |
| 107 | } |
| 108 | assert!(count >= 1, "layer {layer_id} tiles at least one feature"); |
| 109 | seen.insert(layer_id); |
| 110 | } |
| 111 | // And the running maximum saw every one of them through the real path. |
| 112 | // `>= 1`: the static is process-global, so an earlier test's wider |
| 113 | // tile may have already raised a layer's maximum. |
| 114 | let widest: std::collections::BTreeMap<u8, u64> = |
| 115 | widest_layers().into_iter().collect(); |
| 116 | for layer_id in seen { |
| 117 | assert!( |
| 118 | widest.get(&layer_id).copied().unwrap_or(0) >= 1, |
| 119 | "layer {layer_id} reported through widest_layers", |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// A measurement, not an assertion: build the same z14 region with and without a dense grid of |
| 125 | /// traffic segments and print the compressed archive delta, so a real per-segment cost can be |
| 126 | /// extrapolated to a region build without the California inputs to hand. Run explicitly: |
| 127 | /// `cargo test traffic_layer_size_delta -- --ignored --nocapture`. |
| 128 | #[test] |
| 129 | #[ignore = "measurement; run with --ignored --nocapture"] |
| 130 | fn traffic_layer_size_delta() { |
| 131 | use crate::schema::traffic::{pack_component_id, traffic_class}; |
| 132 | let n = 120i32; // 120x120 = 14,400 cells |
| 133 | let coords = |c: i32, r: i32| { |
| 134 | let x = -120.0 + c as f64 * 0.0016; |
| 135 | let y = 35.0 + r as f64 * 0.0016; |
| 136 | vec![vec![(x, y), (x + 0.0014, y)]] |
| 137 | }; |
| 138 | let road = |c: i32, r: i32| Feature { |
| 139 | class: Class::line(dict::LAYER_ROADS, crate::schema::kind("minor_road"), 12), |
| 140 | geometry: Geometry::Lines(coords(c, r)), |
| 141 | name: None, |
| 142 | id: tilecodec::mamaps::body::ID_NONE, |
| 143 | transit_color: 0, |
| 144 | transit_ordinal: 0, |
| 145 | transit_lanes: 0, |
| 146 | transit_taper: 0, |
| 147 | lane_count: 0, |
| 148 | turn_fwd: Vec::new(), |
| 149 | turn_bwd: Vec::new(), |
| 150 | building: None, |
| 151 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 152 | }; |
| 153 | let traffic = |c: i32, r: i32, i: u64| Feature { |
| 154 | class: traffic_class(), |
| 155 | geometry: Geometry::Lines(coords(c, r)), |
| 156 | name: None, |
| 157 | id: pack_component_id(i, 0), |
| 158 | transit_color: 0, |
| 159 | transit_ordinal: 0, |
| 160 | transit_lanes: 0, |
| 161 | transit_taper: 0, |
| 162 | lane_count: 0, |
| 163 | turn_fwd: Vec::new(), |
| 164 | turn_bwd: Vec::new(), |
| 165 | building: None, |
| 166 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 167 | }; |
| 168 | |
| 169 | let mut base = Vec::new(); |
| 170 | for r in 0..n { |
| 171 | for c in 0..n { |
| 172 | base.push(road(c, r)); |
| 173 | } |
| 174 | } |
| 175 | let mut both = Vec::new(); |
| 176 | for r in 0..n { |
| 177 | for c in 0..n { |
| 178 | both.push(road(c, r)); |
| 179 | } |
| 180 | } |
| 181 | let mut i = 0u64; |
| 182 | for r in 0..n { |
| 183 | for c in 0..n { |
| 184 | both.push(traffic(c, r, i)); |
| 185 | i += 1; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | let without = build(&spilled(&base), &settings(12, 14)).expect("without").0; |
| 190 | let with = build(&spilled(&both), &settings(12, 14)).expect("with").0; |
| 191 | let segs = (n * n) as usize; |
| 192 | let delta = with.len() as i64 - without.len() as i64; |
| 193 | println!( |
| 194 | "traffic size delta: {} segment(s) added {} compressed byte(s) ({:.1} B/segment); \ |
| 195 | archive {} -> {} bytes (+{:.1}%)", |
| 196 | segs, |
| 197 | delta, |
| 198 | delta as f64 / segs as f64, |
| 199 | without.len(), |
| 200 | with.len(), |
| 201 | delta as f64 / without.len() as f64 * 100.0, |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | /// polygon per tile, so the id is the only thing that says those pieces are one region. This |
| 206 | /// pins both halves: the id survives into `boundaries`, and it is the *same* id in every tile |
| 207 | /// the region touches. Without the second half a mask can only punch out one tile. |
| 208 | #[test] |
| 209 | fn a_region_area_carries_one_id_across_every_tile_it_touches() { |
| 210 | let osm = crate::extract::tagged_id(396_487, crate::extract::ELEMENT_RELATION); |
| 211 | // Wide enough at z14 (a tile is ~0.022 deg) to be cut into several pieces. |
| 212 | let region = Feature { |
| 213 | class: Class::area( |
| 214 | dict::LAYER_BOUNDARIES, |
| 215 | crate::schema::kind("region_area"), |
| 216 | 0, |
| 217 | ), |
| 218 | geometry: square(-120.0, 35.0, 0.1), |
| 219 | name: None, |
| 220 | id: osm, |
| 221 | transit_color: 0, |
| 222 | transit_ordinal: 0, |
| 223 | transit_lanes: 0, |
| 224 | transit_taper: 0, |
| 225 | lane_count: 0, |
| 226 | turn_fwd: Vec::new(), |
| 227 | turn_bwd: Vec::new(), |
| 228 | building: None, |
| 229 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 230 | }; |
| 231 | let border = Feature { |
| 232 | class: Class::line( |
| 233 | dict::LAYER_BOUNDARIES, |
| 234 | crate::schema::kind("region"), |
| 235 | 0, |
| 236 | ), |
| 237 | geometry: Geometry::Lines(vec![vec![ |
| 238 | (-120.05, 34.95), |
| 239 | (-119.95, 34.95), |
| 240 | (-119.95, 35.05), |
| 241 | ]]), |
| 242 | name: None, |
| 243 | id: tilecodec::mamaps::body::ID_NONE, |
| 244 | transit_color: 0, |
| 245 | transit_ordinal: 0, |
| 246 | transit_lanes: 0, |
| 247 | transit_taper: 0, |
| 248 | lane_count: 0, |
| 249 | turn_fwd: Vec::new(), |
| 250 | turn_bwd: Vec::new(), |
| 251 | building: None, |
| 252 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 253 | }; |
| 254 | let (bytes, _) = build(&spilled(&[region, border]), &settings(14, 14)).expect("build"); |
| 255 | let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read"); |
| 256 | let mut tiles_with_the_region = 0; |
| 257 | for (_, _, body) in &entries { |
| 258 | let body = Body::parse(body).expect("parse"); |
| 259 | let Some(layer) = body.layer(dict::LAYER_BOUNDARIES) else { continue }; |
| 260 | let mut here = false; |
| 261 | for index in 0..layer.features.len() { |
| 262 | match body.feature_id(dict::LAYER_BOUNDARIES, index) { |
| 263 | // The region's shape. Every piece must name the same relation, or the pieces |
| 264 | // cannot be gathered back into one region. |
| 265 | Some(id) if id == osm => here = true, |
| 266 | // A border line, which is coalesced and so has no id worth keeping. |
| 267 | Some(id) => assert_eq!( |
| 268 | id, |
| 269 | tilecodec::mamaps::body::ID_NONE, |
| 270 | "only the region shape may carry an id", |
| 271 | ), |
| 272 | None => panic!("the boundaries layer should have an id table"), |
| 273 | } |
| 274 | } |
| 275 | if here { |
| 276 | tiles_with_the_region += 1; |
| 277 | } |
| 278 | } |
| 279 | assert!( |
| 280 | tiles_with_the_region > 1, |
| 281 | "the region should be clipped across several tiles, got {tiles_with_the_region}", |
| 282 | ); |
| 283 | } |
| 284 | } |