Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / scripts / maps / mamaps_build / src / rings_part1_s1.rs

rings_part1_s1.rs179 lines · 8.0 KiBRaw
1 /// **The zoom-in island flooding, end to end.** Lake Superior at survey scale:
2 /// a ~7°-wide lake through the real pipeline — classify (named → min_zoom 6),
3 /// simplify, clip per tile, stage C — asserting the water survives at z0, z5
4 /// and z14. Before the area floor, an unnamed lake vanished below z12 and a
5 /// named one below z6; the renderer then drew earth over the whole basin.
6 ///
7 /// Superior's real shoreline is thousands of vertices; ten per edge keeps the
8 /// shape (and its tile-edge crossings) while the test stays instant.
9 #[test]
10 fn lake_superior_survives_coarse_and_fine() {
11 use tilecodec::mamaps::body::Body;
12 use tilecodec::mamaps::dict;
13
14 // Lake Superior's real bbox (~-92.1..-84.4 lon, 46.4..48.9 lat), ten
15 // vertices per edge: the tile-edge crossings are what the clipper and
16 // stage C must survive, not the shoreline detail.
17 let edge = |x0: f64, y0: f64, x1: f64, y1: f64| -> Vec<(f64, f64)> {
18 (0..10)
19 .map(|i| {
20 let t = i as f64 / 9.0;
21 (x0 + (x1 - x0) * t, y0 + (y1 - y0) * t)
22 })
23 .collect()
24 };
25 let (x0, y0, x1, y1) = (-92.1, 46.4, -84.4, 48.9);
26 let mut ring = edge(x0, y0, x1, y0);
27 ring.extend(edge(x1, y0, x1, y1).into_iter().skip(1));
28 ring.extend(edge(x1, y1, x0, y1).into_iter().skip(1));
29 ring.extend(edge(x0, y1, x0, y0).into_iter().skip(1));
30 // Classify exactly as a named lake classifies: min_zoom 0 start with an area
31 // floor, which is what carries great water to the world tile.
32 let tags: &[(&str, &str)] =
33 &[("natural", "water"), ("water", "lake"), ("name", "Lake Superior")];
34 let class = crate::schema::landtype::classify_early(tags, true)
35 .expect("a named lake classifies");
36 assert_eq!(class.min_zoom, 0, "the named-lake start zoom");
37 let features = vec![crate::extract::Feature {
38 class,
39 geometry: tile_build::geom::Geometry::Polygons(vec![vec![ring]]),
40 name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0,
41 turn_fwd: Vec::new(),
42 turn_bwd: Vec::new(),
43 carriageway: tilecodec::mamaps::body::Carriageway::default(),
44 building: None,
45 }];
46 let settings = crate::tiler::Settings {
47 build_id: 1,
48 scratch: std::env::temp_dir()
49 .join(format!("mamaps_superior_{}.tilechunks", std::process::id())),
50 force_chunk_spill_file: false,
51 dem: crate::dem::Dem::from_grids(14, 17, Vec::new()),
52 region_links: std::collections::HashMap::new(),
53 country_zooms: std::collections::HashMap::new(),
54 };
55 let store = crate::store::Store::of(&features).expect("spill");
56 let (bytes, _) = crate::tiler::build(&store, &settings).expect("build");
57
58 // A lake-kind polygon must be present at z0 (whole basin in a handful of
59 // tiles), z5 and z14. Count tiles carrying any lake-kind feature.
60 let mut saw = [false, false, false];
61 for (id, _, body) in tilecodec::mamaps::read::read_all(&bytes).expect("read") {
62 let (z, _, _) = tilecodec::pmtiles::tile_zxy(id);
63 let slot = match z {
64 0 => 0,
65 5 => 1,
66 14 => 2,
67 _ => continue,
68 };
69 let body = Body::parse(&body).expect("parse");
70 let Some(layer) = body.layer(dict::LAYER_LANDTYPE) else {
71 continue;
72 };
73 let lake_kind = crate::schema::kind("lake");
74 if layer.features.iter().any(|f| {
75 f.geom_type == tilecodec::mamaps::body::GEOM_POLYGON && f.kind == lake_kind
76 }) {
77 saw[slot] = true;
78 }
79 }
80 assert!(saw[0], " Superior water reaches z0");
81 assert!(saw[1], "Superior water reaches z5");
82 if crate::tiler::wash_cap_enabled() {
83 assert!(!saw[2], "wash cap drops lake water at z14 (overzoomed from z13)");
84 } else {
85 assert!(saw[2], "Superior water reaches z14");
86 }
87 }
88
89 /// **Country label gating, end to end.** Two `country` places through the real
90 /// pipeline with scored start zooms: a Russia-scale label (z0) and a Vatican-scale
91 /// label (z4). The z0 tile carries only Russia; the z4 tile carries both. A third
92 /// unscored country keeps the schema floor and rides every zoom — a missing score
93 /// must never hide a country.
94 #[test]
95 fn country_labels_start_at_their_scored_zoom() {
96 use tilecodec::mamaps::body::Body;
97 use tilecodec::mamaps::dict;
98
99 fn country(id: u64) -> crate::extract::Feature {
100 let class = crate::schema::places::classify(
101 &[("place", "country"), ("name", "Test")] as &[(&str, &str)],
102 )
103 .expect("classified");
104 crate::extract::Feature {
105 class,
106 geometry: tile_build::geom::Geometry::Points(vec![(10.0, 10.0)]),
107 name: Some("Test".to_string()),
108 id,
109 transit_color: 0,
110 transit_ordinal: 0,
111 transit_lanes: 0,
112 transit_taper: 0,
113 lane_count: 0,
114 turn_fwd: Vec::new(),
115 turn_bwd: Vec::new(),
116 carriageway: tilecodec::mamaps::body::Carriageway::default(),
117 building: None,
118 }
119 }
120
121 let russia = 1001u64;
122 let vatican = 1002u64;
123 let unscored = 1003u64;
124 let features = vec![country(russia), country(vatican), country(unscored)];
125 let mut zooms: std::collections::HashMap<u64, u8> = std::collections::HashMap::new();
126 zooms.insert(russia, 0);
127 zooms.insert(vatican, 4);
128 let settings = crate::tiler::Settings {
129 build_id: 1,
130 scratch: std::env::temp_dir()
131 .join(format!("mamaps_countries_{}.tilechunks", std::process::id())),
132 force_chunk_spill_file: false,
133 dem: crate::dem::Dem::from_grids(14, 17, Vec::new()),
134 region_links: std::collections::HashMap::new(),
135 country_zooms: zooms,
136 };
137 let store = crate::store::Store::of(&features).expect("spill");
138 let (bytes, _) = crate::tiler::build(&store, &settings).expect("build");
139
140 // Which ids does each zoom's `places` layer carry?
141 let mut at_zoom: std::collections::HashMap<u8, Vec<u64>> =
142 std::collections::HashMap::new();
143 for (id, _, body) in tilecodec::mamaps::read::read_all(&bytes).expect("read") {
144 let (z, _, _) = tilecodec::pmtiles::tile_zxy(id);
145 if z != 0 && z != 4 {
146 continue;
147 }
148 let body = Body::parse(&body).expect("parse");
149 let Some(layer) = body.layer(dict::LAYER_PLACES) else {
150 continue;
151 };
152 let country_kind = crate::schema::kind("country");
153 let has = |want: u64| {
154 layer
155 .features
156 .iter()
157 .enumerate()
158 .filter(|(_, f)| f.kind == country_kind)
159 .any(|(i, _)| body.feature_id(dict::LAYER_PLACES, i) == Some(want))
160 };
161 for want in [russia, vatican, unscored] {
162 if has(want) {
163 at_zoom.entry(z).or_default().push(want);
164 }
165 }
166 }
167 let mut z0 = at_zoom.get(&0).cloned().unwrap_or_default();
168 let mut z4 = at_zoom.get(&4).cloned().unwrap_or_default();
169 z0.sort_unstable();
170 z4.sort_unstable();
171 z0.dedup();
172 z4.dedup();
173 assert_eq!(z0, vec![russia, unscored], "z0 carries Russia and the unscored country");
174 assert_eq!(
175 z4,
176 vec![russia, vatican, unscored],
177 "z4 carries all three — Vatican joins, nothing leaves"
178 );
179 }