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

Modern-Apps.git / scripts / maps / mamaps_build / src / schema / boundaries_part1.rs

boundaries_part1.rs301 lines · 14.0 KiBRaw
1#[cfg(test)]
2mod tests {
3 use super::*;
4 use tilecodec::mamaps::dict;
5
6 fn classify_tags(pairs: &[(&str, &str)]) -> Option<Class> {
7 super::classify(pairs)
8 }
9
10 /// **What the style actually reads.** `boundaries_country` filters `kind_detail <= 2`, so the
11 /// level has to arrive as a number that compares correctly — not as an interned id whose
12 /// ordering is an accident of table position.
13 #[test]
14 fn the_level_is_carried_as_a_number_not_an_id() {
15 for level in 1..=8u16 {
16 let class =
17 classify_tags(&[("boundary", "administrative"), ("admin_level", &level.to_string())])
18 .expect("a level");
19 assert_eq!(class.kind_detail, level, "the field holds the level itself");
20 assert_eq!(class.flags, FLAG_DETAIL_NUMERIC, "and says so");
21 }
22 // The comparison the style makes.
23 let country = classify_tags(&[("boundary", "administrative"), ("admin_level", "2")])
24 .expect("country");
25 let city = classify_tags(&[("boundary", "administrative"), ("admin_level", "8")])
26 .expect("city");
27 assert!(country.kind_detail <= 2, "drawn by boundaries_country");
28 assert!(city.kind_detail > 2, "drawn by the other layer");
29 }
30
31 #[test]
32 fn a_level_maps_to_the_name_upstream_uses() {
33 let name = |level: &str| {
34 let class = classify_tags(&[("boundary", "administrative"), ("admin_level", level)])
35 .expect(level);
36 dict::KINDS[class.kind as usize - 1]
37 };
38 assert_eq!(name("2"), "country");
39 assert_eq!(name("4"), "region");
40 assert_eq!(name("6"), "county");
41 assert_eq!(name("8"), "locality");
42 }
43
44 /// A relation yields the border *and* the region's shape, as two features.
45 ///
46 /// They cannot be one. The border must stay a line, because clipping a polygon to a tile adds
47 /// segments along the tile edge and the style's `boundaries` layer strokes polygon outlines —
48 /// which drew a grid across the whole map when this was tried as a single area feature.
49 #[test]
50 fn a_relation_yields_a_border_line_and_a_region_shape() {
51 let tags = [("boundary", "administrative"), ("admin_level", "8")];
52 let line = classify_tags(&tags).expect("the border");
53 assert!(!line.area, "the border is drawn, so it stays a line");
54
55 let shape = super::region_area(&tags[..], false).expect("the region");
56 assert!(shape.area, "the region is a shape, and nothing draws it");
57 assert_eq!(shape.layer, dict::LAYER_BOUNDARIES);
58 assert_eq!(
59 dict::KINDS[shape.kind as usize - 1],
60 "region_area",
61 "a kind the boundaries layer's whitelist excludes, or it would be stroked",
62 );
63 assert_eq!(shape.kind_detail, line.kind_detail, "same level, so the mask can tell a city from a country");
64 assert!(shape.min_zoom < line.min_zoom, "a mask is wanted where the region fits on screen");
65 }
66
67 /// A single way is one segment of a border and encloses nothing.
68 #[test]
69 fn a_way_has_no_region_shape() {
70 let tags = [("boundary", "administrative"), ("admin_level", "2")];
71 assert!(super::region_area(&tags[..], true).is_none());
72 assert!(super::region_area(&tags[..], false).is_some());
73 }
74
75 /// A border is a line even when it closes. Filling it would paint over every layer inside the
76 /// country — and a polygon clipped to a tile grows edges along the tile boundary, which the
77 /// style's `line` layers stroke as a grid across the map. That is not theoretical: it was
78 /// tried, and the tile grid was immediately visible.
79 #[test]
80 fn a_boundary_is_never_an_area() {
81 let class = classify_tags(&[("boundary", "administrative"), ("admin_level", "2")])
82 .expect("country");
83 assert!(!class.area);
84 assert_eq!(class.layer, dict::LAYER_BOUNDARIES);
85 }
86
87 #[test]
88 fn a_country_border_is_carried_at_world_zoom_and_a_city_limit_is_not() {
89 let at = |level: &str| {
90 classify_tags(&[("boundary", "administrative"), ("admin_level", level)])
91 .expect(level)
92 .min_zoom
93 };
94 assert_eq!(at("2"), 0, "a country border carries a world tile");
95 assert!(at("2") < at("4"));
96 assert_eq!(at("4"), 3, "a state border");
97 // County and below start at z12: the diet moved them (boundaries were
98 // 15.3GB retiled from z6/z9 up).
99 assert_eq!(at("6"), 12, "a county line");
100 assert_eq!(at("8"), 12, "there are a hundred thousand city limits");
101 }
102
103 #[test]
104 fn a_level_below_a_city_is_not_drawn() {
105 // Level 9 and 10 are wards and neighbourhoods: real data, and a line nobody wants.
106 for level in ["9", "10", "11"] {
107 assert!(
108 classify_tags(&[("boundary", "administrative"), ("admin_level", level)]).is_none(),
109 "level {level} should not be drawn",
110 );
111 }
112 assert!(
113 classify_tags(&[("boundary", "administrative"), ("admin_level", "0")]).is_none(),
114 "level 0 is not a level",
115 );
116 }
117
118 #[test]
119 fn a_maritime_administrative_boundary_is_not_drawn() {
120 // An EEZ / territorial-water limit: `boundary=administrative` over water, tagged
121 // `maritime=yes`. The reference style's boundary layers show inland/coastline admin
122 // only, so the tiler drops these rather than drawing lines across open sea.
123 assert!(
124 classify_tags(&[
125 ("boundary", "administrative"),
126 ("admin_level", "2"),
127 ("maritime", "yes"),
128 ])
129 .is_none(),
130 "a maritime boundary should not be a boundary",
131 );
132 // And the inland equivalent still classifies.
133 assert!(
134 classify_tags(&[("boundary", "administrative"), ("admin_level", "2")]).is_some(),
135 "an inland country border still counts",
136 );
137 }
138
139 /// The convention is a property of a *country*, so nothing below level 2 carries one: a state
140 /// has no driving side of its own anywhere this build covers.
141 #[test]
142 fn only_a_level_two_relation_with_an_iso_code_is_a_country() {
143 let code = |pairs: &[(&str, &str)]| super::country_code(pairs).map(str::to_string);
144 assert_eq!(
145 code(&[("boundary", "administrative"), ("admin_level", "2"), ("ISO3166-1", "JP")]),
146 Some("JP".to_string()),
147 );
148 // Whitespace around a level is common in real data, as `classify` already allows.
149 assert_eq!(
150 code(&[("boundary", "administrative"), ("admin_level", " 2 "), ("ISO3166-1", "GB")]),
151 Some("GB".to_string()),
152 );
153 for tags in [
154 // A state, which has no convention of its own.
155 vec![("boundary", "administrative"), ("admin_level", "4"), ("ISO3166-1", "US")],
156 // A country with no code to look up.
157 vec![("boundary", "administrative"), ("admin_level", "2")],
158 // A three-letter code is the alpha-3 field under the wrong key.
159 vec![("boundary", "administrative"), ("admin_level", "2"), ("ISO3166-1", "JPN")],
160 vec![("boundary", "protected_area"), ("admin_level", "2"), ("ISO3166-1", "JP")],
161 vec![("ISO3166-1", "JP")],
162 ] {
163 assert!(super::country_code(&tags[..]).is_none(), "{tags:?} is not a country");
164 }
165 }
166
167 #[test]
168 fn the_convention_table_carries_the_driving_side_and_the_centre_line_colour() {
169 let at = super::convention_for;
170 assert_eq!(at("GB"), MarkingConvention { left_hand: true, yellow_centre: false });
171 assert_eq!(at("JP"), MarkingConvention { left_hand: true, yellow_centre: true });
172 assert_eq!(at("US"), MarkingConvention { left_hand: false, yellow_centre: true });
173 assert_eq!(at("FR"), MarkingConvention::default(), "right-hand and white");
174 // An unrecognised code draws the same as an unresolved tile, rather than differently for
175 // no reason a reader could see.
176 assert_eq!(at("ZZ"), MarkingConvention::default());
177 assert_eq!(at("gb"), at("GB"), "OSM's own casing is not the only casing in the wild");
178 }
179
180 /// A square degree box as the relation assembler would hand one over: one polygon, one closed
181 /// outer ring, lon/lat.
182 fn box_over(west: f64, south: f64, east: f64, north: f64) -> Vec<Vec<Vec<(f64, f64)>>> {
183 vec![vec![vec![
184 (west, south),
185 (east, south),
186 (east, north),
187 (west, north),
188 (west, south),
189 ]]]
190 }
191
192 /// The tile a lon/lat falls in at `z`.
193 fn tile_at(lon: f64, lat: f64, z: u8) -> (u64, u64) {
194 let (x, y) = tile_build::geom::project(lon, lat, z);
195 (x as u64, y as u64)
196 }
197
198 /// **Resolved coarse, inherited downward.** A California build is 12.7 M z14 tiles and the
199 /// answer is the same across all of them, so the country is decided once per z6 cell and every
200 /// tile below reads its ancestor's.
201 #[test]
202 fn a_country_claims_the_coarse_tiles_it_covers_and_every_tile_below_inherits() {
203 let mut grid = Conventions::default();
204 grid.add("JP", &box_over(130.0, 30.0, 145.0, 45.0));
205
206 let inside = MarkingConvention { left_hand: true, yellow_centre: true };
207 let (x, y) = tile_at(137.0, 37.0, 14);
208 assert_eq!(grid.at_tile(14, x, y), inside, "a z14 tile reads its z6 ancestor");
209 // Its neighbour is a different z14 tile in the same coarse cell, so it must agree.
210 assert_eq!(grid.at_tile(14, x + 1, y + 1), inside);
211 let (cx, cy) = tile_at(137.0, 37.0, COARSE_ZOOM);
212 assert_eq!(grid.at_tile(COARSE_ZOOM, cx, cy), inside, "and so does the cell itself");
213 }
214
215 /// Nothing claimed it, so it draws right-hand and white — which is most of the world by land
216 /// area and the safer thing to be wrong about.
217 #[test]
218 fn an_unclaimed_tile_falls_back_to_right_hand_and_white() {
219 let mut grid = Conventions::default();
220 grid.add("JP", &box_over(130.0, 30.0, 145.0, 45.0));
221 let (x, y) = tile_at(-100.0, 40.0, 14);
222 assert_eq!(grid.at_tile(14, x, y), MarkingConvention::default());
223 assert!(!Conventions::default().at_tile(14, 0, 0).left_hand, "and so does an empty grid");
224 }
225
226 /// A coastal or border cell is claimed by every country with a boundary vertex in it, and only
227 /// one of them covers its centre. Covering the centre is the unambiguous claim, so it wins
228 /// whatever order the relations arrived in.
229 #[test]
230 fn covering_a_cells_centre_outranks_merely_having_a_border_in_it() {
231 let mut grid = Conventions::default();
232 // A shape too small to cover any cell centre: it can only ever be a border claim.
233 grid.add("JP", &box_over(-100.02, 40.0, -100.0, 40.02));
234 // And a shape that swallows it whole.
235 grid.add("US", &box_over(-110.0, 35.0, -95.0, 45.0));
236 let (x, y) = tile_at(-100.01, 40.01, 14);
237 assert_eq!(
238 grid.at_tile(14, x, y),
239 MarkingConvention { left_hand: false, yellow_centre: true },
240 "the interior claim came second and still won",
241 );
242 }
243
244 /// The store index has to reproduce the run that built the spill. A grid that did not survive
245 /// the index would leave a build drawing every road right-hand and white, which is not
246 /// a difference anything downstream could see.
247 #[test]
248 fn the_grid_survives_the_store_index() {
249 let mut grid = Conventions::default();
250 grid.add("JP", &box_over(130.0, 30.0, 145.0, 45.0));
251 grid.add("GB", &box_over(-8.0, 50.0, 2.0, 59.0));
252 assert!(!grid.is_empty());
253
254 let bytes = grid.to_bytes();
255 let (back, used) = Conventions::from_bytes(&bytes).expect("read the grid back");
256 assert_eq!(used, bytes.len(), "the whole grid was consumed");
257 for (lon, lat) in [(137.0, 37.0), (-3.0, 54.0), (-100.0, 40.0)] {
258 let (x, y) = tile_at(lon, lat, 14);
259 assert_eq!(back.at_tile(14, x, y), grid.at_tile(14, x, y), "at {lon},{lat}");
260 }
261 // Ascending by cell key, so two runs of the same build write the same index bytes.
262 assert_eq!(bytes, back.to_bytes());
263 }
264
265 #[test]
266 fn a_truncated_convention_grid_is_an_error_rather_than_a_short_read() {
267 let mut grid = Conventions::default();
268 grid.add("GB", &box_over(-8.0, 50.0, 2.0, 59.0));
269 let bytes = grid.to_bytes();
270 assert!(Conventions::from_bytes(&bytes[..bytes.len() - 1]).is_err(), "a cut cell");
271 assert!(Conventions::from_bytes(&bytes[..2]).is_err(), "a cut count");
272 // And the empty grid, which is what a build with no country relation writes.
273 let (empty, used) = Conventions::from_bytes(&Conventions::default().to_bytes())
274 .expect("an empty grid is readable");
275 assert!(empty.is_empty());
276 assert_eq!(used, 4);
277 }
278
279 #[test]
280 fn only_an_administrative_boundary_with_a_readable_level_counts() {
281 for tags in [
282 // A protected area or a maritime boundary is a boundary and not an administrative one.
283 vec![("boundary", "protected_area"), ("admin_level", "2")],
284 vec![("boundary", "maritime"), ("admin_level", "2")],
285 // Administrative but with no level, or an unreadable one.
286 vec![("boundary", "administrative")],
287 vec![("boundary", "administrative"), ("admin_level", "")],
288 vec![("boundary", "administrative"), ("admin_level", "two")],
289 vec![("boundary", "administrative"), ("admin_level", "4;6")],
290 vec![("admin_level", "2")],
291 vec![],
292 ] {
293 assert!(classify_tags(&tags).is_none(), "{tags:?} should not be a boundary");
294 }
295 // Whitespace around a level is common in real data and is not a reason to drop a border.
296 assert!(
297 classify_tags(&[("boundary", "administrative"), ("admin_level", " 4 ")]).is_some(),
298 "a padded level still parses",
299 );
300 }
301}