/// The tolerance to simplify a layer at: the per-zoom policy, with a floor for buildings /// and a multiplier for the boundary wash. /// /// Buildings live only at z14 and up, where [`simplify::tolerance_for`] returns 0.0 — full /// detail. Half an extent unit there is ~0.3 m on the ground, sub-pixel on any screen the style /// draws at, and only drops what is invisible anyway: exactly-collinear midpoints, whose /// significance is 0 and which any non-zero threshold already removes, plus sub-pixel jaggies. /// Every other layer keeps the policy tolerance untouched. A coarser global tolerance still wins /// (`max`, not replace), and a zero/negative policy for a non-building layer is unchanged. /// /// `boundaries` alone keeps the [`WASH_SIMPLIFICATION`] multiplier: a low-zoom line backdrop. /// `landtype` simplifies at 1.5x: natural boundaries (forest edges, shorelines, /// farmland) are washes, not navigation geometry — same reasoning as /// boundaries, smaller factor because parks and lakes are looked at closer. /// Roads and transit curves take the wash below z14: at z13 a 1.5x tolerance /// drops sub-10m wiggles (a 2x grid stretch could straighten them) while z14 /// stays at policy 0.0 — full detail where carriageway/lane alignment lives. /// Buildings and the point layers keep 1.0 — navigation geometry and /// labels, not a wash. /// /// The prototype at `analysis/mamaps_building_savings.py` estimates this plus the snap below at /// ~2% + ~4% of building-tile bytes; buildings are z14-only, 9.76M features, ~20% of the archive. fn tolerance_for_layer(layer: u8, z: u8, tolerance: f64) -> f64 { use tilecodec::mamaps::dict; if layer == dict::LAYER_BUILDINGS && z >= crate::schema::buildings::MIN_ZOOM { tolerance.max(BUILDING_TOLERANCE) } else if layer == dict::LAYER_BOUNDARIES { tolerance * WASH_SIMPLIFICATION } else if layer == dict::LAYER_LANDTYPE { // z13 tiles span ~5 km: triple the wash there, keep 1.5x at z14 // where parks sit under streets. if z == 13 { let o = size_opts(); if o.keep_z13_wash && o.simplify_scale > 0.0 { tolerance / o.simplify_scale * LANDTYPE_Z13_SIMPLIFICATION } else { tolerance * LANDTYPE_Z13_SIMPLIFICATION } } else { tolerance * LANDTYPE_SIMPLIFICATION } } else if z < crate::DEFAULT_MAX_ZOOM && (layer == dict::LAYER_ROADS || layer == dict::LAYER_TRANSIT) { // Below the archive max the policy tolerance is non-zero, so the wash // bites; at z14 policy is 0.0 and 1.5x of zero is zero — crisp. tolerance * WASH_SIMPLIFICATION } else { tolerance } } /// The tolerance for one feature: the layer policy, plus the z14 roads /// floor. Washable roads (not arterial, no lanes/split — see `needs_crisp`) /// take [`ROAD_DETAIL_FLOOR`] at the archive max, where the layer policy is /// 0.0; everything else keeps the layer value untouched. fn tolerance_for_feature(feature: &crate::extract::Feature, z: u8, tolerance: f64) -> f64 { use tilecodec::mamaps::dict; let base = tolerance_for_layer(feature.class.layer, z, tolerance); let base = if z == crate::DEFAULT_MAX_ZOOM && feature.class.layer == dict::LAYER_ROADS && !needs_crisp(feature) { base.max(ROAD_DETAIL_FLOOR) } else { base }; // `--z14-tolerance`: a z14 floor for the washable layers (crisp roads keep full detail). let floor = size_opts().z14_tolerance; if floor > 0.0 && z == crate::DEFAULT_MAX_ZOOM { let l = feature.class.layer; let washable = l == dict::LAYER_LANDTYPE || l == dict::LAYER_BOUNDARIES || l == dict::LAYER_BUILDINGS || (l == dict::LAYER_ROADS && !needs_crisp(feature)); if washable { return base.max(floor); } } base } /// Snap a building geometry's near-axis-aligned edges to exactly axis-aligned. /// /// Hand-digitised footprints are near-rectangles: edges within [`BUILDING_SNAP`] of horizontal /// or vertical snap flat, turning an 8-delta wobble into a repeated constant the varint arena /// encodes cheaply. Runs on `SigPt` survivors **after** [`simplify::filter`], via /// [`Vertex::moved`] so significance rides along untouched — nothing downstream re-measures. /// Only ever called for the buildings layer (the caller gates it); strictly per-edge — never /// a corner merge, never a vertex invented or removed — so winding, closure and hole /// containment are exactly what the filter left. /// /// Deterministic by construction: a pure function of the ring's coordinates, so two runs tile /// identically however features are chunked or threaded — the property /// `the_archive_is_identical_however_the_features_are_chunked` defends. fn snap_building_rings(geometry: &Geometry, layer: u8) -> Geometry { debug_assert_eq!(layer, tilecodec::mamaps::dict::LAYER_BUILDINGS); match geometry { Geometry::Points(_) | Geometry::Lines(_) => geometry.clone(), Geometry::Polygons(polygons) => Geometry::Polygons( polygons .iter() .map(|rings| rings.iter().map(|ring| snap_building_ring(ring)).collect()) .collect(), ), } } /// Snap one ring's near-axis-aligned edges flat, preserving length, order, closure and winding. fn snap_building_ring(ring: &[SigPt]) -> Vec { if ring.len() < 2 { return ring.to_vec(); } // The explicit close duplicates the first vertex; snapping the closing edge in the same // pass as the rest would compare the duplicate against its twin and average the corner // away. Snap the distinct vertices, then re-close with the surviving first. let closed = ring.len() > 1 && ring.first().map(|v| v.xy()) == ring.last().map(|v| v.xy()); let open_len = ring.len() - usize::from(closed); if open_len < 2 { return ring.to_vec(); } let open = &ring[..open_len]; let mut snapped: Vec<(f64, f64)> = open.iter().map(|v| v.xy()).collect(); // Forward pass: each edge votes its dominant direction; a near-horizontal edge levels both // endpoints to their mean y, a near-vertical one to their mean x. Means, not first-wins, so // a shared corner snapped from either side lands identically — the seam property the // annotate-then-filter design exists for. for i in 0..open_len { let j = (i + 1) % open_len; let ((x0, y0), (x1, y1)) = (snapped[i], snapped[j]); let (dx, dy) = ((x1 - x0).abs(), (y1 - y0).abs()); if dx <= BUILDING_SNAP && dy > BUILDING_SNAP { // Near-vertical: level x. let x = (x0 + x1) / 2.0; snapped[i].0 = x; snapped[j].0 = x; } else if dy <= BUILDING_SNAP && dx > BUILDING_SNAP { // Near-horizontal: level y. let y = (y0 + y1) / 2.0; snapped[i].1 = y; snapped[j].1 = y; } } let mut out: Vec = open .iter() .zip(snapped) .map(|(v, (x, y))| { // Round onto the building grid: hand jitter (±1 unit) collapses // into repeated constants. Runs here — after the filter, on // survivors only — so measured significance is never stale. let gx = (x / crate::tiler::BUILDING_GRID).round() * crate::tiler::BUILDING_GRID; let gy = (y / crate::tiler::BUILDING_GRID).round() * crate::tiler::BUILDING_GRID; v.moved((gx, gy)) }) .collect(); if closed { out.push(out[0]); } out } /// `--coord-quant`: snap tile-local coordinates to a `q`-unit grid, dropping consecutive /// duplicates. Max displacement per vertex is `q/2` per axis (`q/sqrt(2)` units). A line that /// collapses below 2 vertices or a ring below 4 is dropped (a ring that was the exterior takes /// its polygon with it) — the same floors `simplify::filter` applies. Points are untouched. /// Rings are re-normalised by stage C afterwards, so `rings_validated` stays true. fn snap_grid(g: tile_build::geom::IntGeometry, q: i32) -> tile_build::geom::IntGeometry { use tile_build::geom::IntGeometry; let s = |v: i32| (v + q / 2).div_euclid(q) * q; let snap = |pts: &[(i32, i32)]| -> Vec<(i32, i32)> { let mut out: Vec<(i32, i32)> = Vec::with_capacity(pts.len()); for &(x, y) in pts { let p = (s(x), s(y)); if out.last() != Some(&p) { out.push(p); } } out }; match g { IntGeometry::Points(p) => IntGeometry::Points(p), IntGeometry::Lines(lines) => { IntGeometry::Lines(lines.iter().map(|l| snap(l)).filter(|l| l.len() >= 2).collect()) } IntGeometry::Polygons(polys) => IntGeometry::Polygons( polys .iter() .filter_map(|rings| { let mut out = Vec::with_capacity(rings.len()); for (i, r) in rings.iter().enumerate() { let sr = snap(r); // Distinct corners: a closed ring repeats its first vertex. let closed = sr.len() > 1 && sr.first() == sr.last(); let distinct = sr.len() - usize::from(closed); if distinct >= 3 { out.push(sr); } else if i == 0 { return None; } } (!out.is_empty()).then_some(out) }) .collect(), ), } } fn is_empty_int(g: &tile_build::geom::IntGeometry) -> bool { use tile_build::geom::IntGeometry; match g { IntGeometry::Points(p) => p.is_empty(), IntGeometry::Lines(l) => l.is_empty(), IntGeometry::Polygons(p) => p.is_empty(), } }