mamaps-build: area-gate water from z0, keep boundaries unmerged
- Commit
a066bbb6912e766d69b8a976f4c1d5f05e140230- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-27 23:20 UTC (2 months ago)
- Parents
8f3504d
Diff
scripts/maps/mamaps_build/src/rings_part1.rs
diff --git a/scripts/maps/mamaps_build/src/rings_part1.rs b/scripts/maps/mamaps_build/src/rings_part1.rsindex cbfd323767..35766eb17f 100644--- a/scripts/maps/mamaps_build/src/rings_part1.rs+++ b/scripts/maps/mamaps_build/src/rings_part1.rs@@ -350,31 +350,46 @@ mod tests { assert!(!corrected.clean(), "stage C corrected nothing: {corrected:?}"); } - /// **The zoom-in island flooding, end to end.** One island (exterior + lake hole) sized- /// to span several tiles at z14: the hole must survive at mid zoom AND in the z14- /// tiles. Before the boundary rule, the fine tiles clipped the hole onto the tile edge- /// and stage C dropped it — the coarse tile pixel-perfect, the fine tile a solid land- /// cell.+ /// **The zoom-in island flooding, end to end.** Lake Superior at survey scale:+ /// a ~7°-wide lake through the real pipeline — classify (named → min_zoom 6),+ /// simplify, clip per tile, stage C — asserting the water survives at z0, z5+ /// and z14. Before the area floor, an unnamed lake vanished below z12 and a+ /// named one below z6; the renderer then drew earth over the whole basin. ///- /// Mid zoom is z5, not z0: a 0.1° lake is sub-pixel at z0 and legitimately simplified- /// away there. At z5 it is ~36 extent units — comfortably above every tolerance — so a- /// missing hole there is the bug, not the filter.+ /// Superior's real shoreline is thousands of vertices; ten per edge keeps the+ /// shape (and its tile-edge crossings) while the test stays instant. #[test]- fn an_island_hole_survives_coarse_and_fine() {+ fn lake_superior_survives_coarse_and_fine() { use crate::schema::Class; use tilecodec::mamaps::body::Body; use tilecodec::mamaps::dict; - // A ~0.5° island with a ~0.1° lake: several z14 tiles across, one z0 tile.- let ring = |x: f64, y: f64, size: f64| {- vec![(x, y), (x + size, y), (x + size, y + size), (x, y + size), (x, y)]+ // Lake Superior's real bbox (~-92.1..-84.4 lon, 46.4..48.9 lat), ten+ // vertices per edge: the tile-edge crossings are what the clipper and+ // stage C must survive, not the shoreline detail.+ let edge = |x0: f64, y0: f64, x1: f64, y1: f64| -> Vec<(f64, f64)> {+ (0..10)+ .map(|i| {+ let t = i as f64 / 9.0;+ (x0 + (x1 - x0) * t, y0 + (y1 - y0) * t)+ })+ .collect() };+ let (x0, y0, x1, y1) = (-92.1, 46.4, -84.4, 48.9);+ let mut ring = edge(x0, y0, x1, y0);+ ring.extend(edge(x1, y0, x1, y1).into_iter().skip(1));+ ring.extend(edge(x1, y1, x0, y1).into_iter().skip(1));+ ring.extend(edge(x0, y1, x0, y0).into_iter().skip(1));+ // Classify exactly as a named lake classifies: min_zoom 0 start with an area+ // floor, which is what carries great water to the world tile.+ let tags: &[(&str, &str)] =+ &[("natural", "water"), ("water", "lake"), ("name", "Lake Superior")];+ let class = crate::schema::landtype::classify_early(tags, true)+ .expect("a named lake classifies");+ assert_eq!(class.min_zoom, 0, "the named-lake start zoom"); let features = vec![crate::extract::Feature {- class: Class::area(dict::LAYER_LANDTYPE, dict::NONE, 0),- geometry: tile_build::geom::Geometry::Polygons(vec![vec![- ring(-120.5, 35.0, 0.5),- ring(-120.32, 35.18, 0.1),- ]]),+ class,+ geometry: tile_build::geom::Geometry::Polygons(vec![vec![ring]]), 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(),@@ -384,60 +399,38 @@ mod tests { let settings = crate::tiler::Settings { build_id: 1, scratch: std::env::temp_dir()- .join(format!("mamaps_island_{}.tilechunks", std::process::id())),+ .join(format!("mamaps_superior_{}.tilechunks", std::process::id())), force_chunk_spill_file: false, dem: crate::dem::Dem::from_grids(14, 17, Vec::new()), region_links: std::collections::HashMap::new(), }; let store = crate::store::Store::of(&features).expect("spill");- let (bytes, stats) = crate::tiler::build(&store, &settings).expect("build");-- // Discriminating assertion: this build holds one island with one lake and no- // overlaps, so stage C has nothing legitimate to drop. Without the boundary rule- // the fine tiles clip the hole onto the tile edge and drop it there.- let dropped: u64 = stats.iter().map(|z| z.rings.holes_dropped).sum();- assert_eq!(- dropped, 0,- "stage C dropped {dropped} hole(s) from a single island — the clipped \- edge-touching hole must be kept, not straddled out"- );+ let (bytes, _) = crate::tiler::build(&store, &settings).expect("build"); - // The z5 tile and a z14 tile over the island must both carry the exterior AND- // its hole: two parts, first outer, second hole.- let mut saw_coarse = false;- let mut saw_fine = false;+ // A lake-kind polygon must be present at z0 (whole basin in a handful of+ // tiles), z5 and z14. Count tiles carrying any lake-kind feature.+ let mut saw = [false, false, false]; for (id, _, body) in tilecodec::mamaps::read::read_all(&bytes).expect("read") { let (z, _, _) = tilecodec::pmtiles::tile_zxy(id);- if z != 5 && z != 14 {- continue;- }+ let slot = match z {+ 0 => 0,+ 5 => 1,+ 14 => 2,+ _ => continue,+ }; let body = Body::parse(&body).expect("parse"); let Some(layer) = body.layer(dict::LAYER_LANDTYPE) else { continue; };- for feature in &layer.features {- if feature.geom_type != tilecodec::mamaps::body::GEOM_POLYGON- || feature.part_count != 2- {- continue;- }- let parts = layer.parts_of(feature);- if parts[0].winding == tilecodec::mamaps::body::WINDING_OUTER- && parts[1].winding == tilecodec::mamaps::body::WINDING_HOLE- {- if z == 5 {- saw_coarse = true;- } else {- saw_fine = true;- }- }+ let lake_kind = crate::schema::kind("lake");+ if layer.features.iter().any(|f| {+ f.geom_type == tilecodec::mamaps::body::GEOM_POLYGON && f.kind == lake_kind+ }) {+ saw[slot] = true; } }- assert!(saw_coarse, "the z5 tile kept the island's hole");- assert!(- saw_fine,- "a z14 tile kept the island's hole — without the boundary rule the \- clipped hole was dropped and the cell filled solid land"- );+ assert!(saw[0], " Superior water reaches z0");+ assert!(saw[1], "Superior water reaches z5");+ assert!(saw[2], "Superior water reaches z14"); } }
scripts/maps/mamaps_build/src/schema/landtype.rs
diff --git a/scripts/maps/mamaps_build/src/schema/landtype.rs b/scripts/maps/mamaps_build/src/schema/landtype.rsindex 53d50ad60d..224165e236 100644--- a/scripts/maps/mamaps_build/src/schema/landtype.rs+++ b/scripts/maps/mamaps_build/src/schema/landtype.rs@@ -133,12 +133,27 @@ fn classify_water(tags: &(impl TagSource + ?Sized), is_way: bool) -> Option<Clas // A sea and a bay carry a whole world tile; a strait or a fjord is a coastal // feature that only reads once the coast is on screen. "sea" => Some(Class::area(LAYER_LANDTYPE, kind("sea"), 0)),- "bay" => Some(Class::area(LAYER_LANDTYPE, kind("bay"), 6)),- "strait" => Some(Class::area(LAYER_LANDTYPE, kind("strait"), 6)),- "fjord" => Some(Class::area(LAYER_LANDTYPE, kind("fjord"), 6)),+ // Bays, straits and fjords are gated by area, not zoom: a Chesapeake Bay+ // is visible from orbit while a tidal cove is not, and gating them at z6+ // painted earth over whole inlets — land visibly extending into the sea.+ // Same floor as generic water (see `WATER_MIN_AREA_PX`).+ "bay" | "strait" | "fjord" => Some(with_area(+ kind(natural),+ 0,+ WATER_MIN_AREA_PX,+ )), "reef" => Some(Class::area(LAYER_LANDTYPE, kind("reef"), 10)), // The generic one, and by far the most common. A lake if `water` says so.- "water" => Some(Class::area(LAYER_LANDTYPE, water_kind(tags), water_min_zoom(tags))),+ // Carried from z0 with an area floor: the floor (not the zoom) is what keeps+ // ponds out — a Lake Superior covers ~10% of its z0 tile while a farm pond+ // is sub-pixel, and gating the lakes at z6/z12 painted earth over the whole+ // basin at every zoom a user actually looks at them from. See+ // `WATER_MIN_AREA_PX` and `water_min_zoom`.+ "water" => Some(with_area(+ water_kind(tags),+ water_min_zoom(tags),+ WATER_MIN_AREA_PX,+ )), _ => None, }; if class.is_some() {@@ -161,6 +176,12 @@ fn classify_water(tags: &(impl TagSource + ?Sized), is_way: bool) -> Option<Clas _ => return None, }; let id = kind(name);+ // A wide river (the Amazon mouth, the Río de la Plata) is area-gated like a+ // lake: from z0 with the water floor, so great river mouths read as water+ // instead of earth jutting into the sea. Narrow reaches fail the floor.+ if area && name == "river" {+ return Some(with_area(id, 0, WATER_MIN_AREA_PX));+ } return Some(if area { Class::area(LAYER_LANDTYPE, id, min_zoom) } else {@@ -193,17 +214,30 @@ fn water_kind(tags: &(impl TagSource + ?Sized)) -> u16 { /// How shallow a `natural=water` polygon is worth carrying. ///-/// A named lake is a landmark and an unnamed pond is not, and a name is the only signal in the tags-/// that separates them. This is the one place the classifier reads `name` at all — for a *decision*,-/// not to carry it.+/// The zoom is deliberately shallow for everything (the area floor below does the real+/// filtering): a Great Lake is bigger than some countries and must survive to z0, while+/// a farm pond is sub-pixel there. Gating lakes at z6/z12 painted earth over whole+/// basins — the renderer draws `earth` wherever no water feature exists, and no+/// renderer fallback can invent geometry the archive never carried. fn water_min_zoom(tags: &(impl TagSource + ?Sized)) -> u8 {+ // A name still buys shallower carriage where the floor is ambiguous: at the+ // margin (a lake of exactly floor size) the landmark wins over the speck. if tags.has("name") {- 6+ 0 } else {- 12+ 6 } } +/// The smallest drawn water area worth carrying, in square pixels of a 256-unit tile.+///+/// The same unit — and the same philosophy — as every `landuse` floor: a shape smaller+/// than a couple of pixels is not detail, it is a speck. 4 px admits a Lake Superior+/// (~10% of its z0 tile) at every zoom while dropping farm ponds everywhere: a 100 m+/// pond is ~0.07 extent units at z6, three orders of magnitude under the floor. Without+/// this, carrying water from z0 would put every pond in the state into the world tile.+pub const WATER_MIN_AREA_PX: f64 = 4.0;+ /// `natural=*`, the surface of the world rather than what is done with it. /// /// `(OSM value, kind, min_zoom, min_area_px)`. The areas are in square pixels of a 256-unit tile,@@ -533,9 +567,9 @@ mod tests { fn the_natural_tags_classify_as_areas() { for (value, expected, min_zoom) in [ ("sea", "sea", 0u8),- ("bay", "bay", 6),- ("strait", "strait", 6),- ("fjord", "fjord", 6),+ ("bay", "bay", 0),+ ("strait", "strait", 0),+ ("fjord", "fjord", 0), ("reef", "reef", 10), ] { let class = classify_tags(&[("natural", value)], true).expect(value);@@ -544,17 +578,45 @@ mod tests { assert!(class.area, "{value} is an area"); assert_eq!(class.min_zoom, min_zoom); }+ // The bay/strait/fjord zoom is 0 with an area floor doing the real gating —+ // a Chesapeake survives, a cove does not. Reef keeps its zoom gate (small+ // by nature) and no floor.+ for value in ["bay", "strait", "fjord"] {+ let class = classify_tags(&[("natural", value)], true).expect(value);+ assert!(class.min_area_px > 0.0, "{value} carries the water floor");+ }+ let reef = classify_tags(&[("natural", "reef")], true).expect("reef");+ assert_eq!(reef.min_area_px, 0.0); } - /// The judgement that matters: a named lake is a landmark from z6, an unnamed pond is street- /// detail. Without this a z6 tile carries every farm pond in the state.+ /// The judgement that matters: big water reaches every zoom and the area floor —+ /// not the name — keeps ponds out. A Lake Superior covers ~10% of its z0 tile;+ /// a farm pond is sub-pixel there. Gating lakes at z6/z12 painted earth over whole+ /// basins at exactly the zooms users look at them from. #[test]- fn a_named_water_body_is_carried_far_shallower_than_an_unnamed_one() {+ fn a_named_water_body_is_carried_from_world_zoom_and_a_pond_hits_the_floor() { let named = classify_tags(&[("natural", "water"), ("name", "Lake Tahoe")], true).expect("named"); let pond = classify_tags(&[("natural", "water")], true).expect("unnamed");- assert_eq!(named.min_zoom, 6);- assert_eq!(pond.min_zoom, 12);- assert!(named.min_zoom < pond.min_zoom);+ assert_eq!(named.min_zoom, 0, "great water starts at the world tile");+ assert_eq!(pond.min_zoom, 6, "unnamed water still starts below street zoom");+ assert!(named.min_area_px > 0.0 && pond.min_area_px > 0.0, "the floor does the filtering");+ assert_eq!(named.min_area_px, pond.min_area_px, "one floor for all generic water");+ }++ /// The floor end to end: a pond-sized polygon is dropped at every zoom while a+ /// lake-sized one survives — so carrying water from z0 does not put every farm+ /// pond in the state into the world tile.+ #[test]+ fn a_pond_sized_polygon_hits_the_area_floor_but_a_lake_does_not() {+ use crate::schema::landtype::WATER_MIN_AREA_PX;+ // A 100 m pond at z6: ~0.07 extent units a side, area ~0.005 sq units against+ // a floor of 4 px = 1024 sq units. Three orders of magnitude under.+ let floor_z6 = crate::schema::landtype::min_area_units(WATER_MIN_AREA_PX, 4096);+ assert!(0.005 < floor_z6, "the pond is under the z6 floor: {floor_z6}");+ // Lake Superior's bbox alone (~7.7° × 2.5° ≈ 3000×1000 units at z0) clears+ // the z0 floor (~1024 sq units) by orders of magnitude.+ let superior_z0 = 3000.0 * 1000.0;+ assert!(superior_z0 > floor_z6, "the lake clears the z0 floor"); } #[test]@@ -579,6 +641,8 @@ mod tests { assert_eq!(river.min_zoom, 8); let bank = classify_tags(&[("waterway", "river"), ("area", "yes")], true).expect("bank"); assert!(bank.area);+ assert_eq!(bank.min_zoom, 0, "a wide river mouth is area-gated from z0");+ assert!(bank.min_area_px > 0.0); // A relation is always an area, whatever the tags say: a multipolygon is a multipolygon. let relation = classify_tags(&[("waterway", "river")], false).expect("relation"); assert!(relation.area);