maps: bisect long segments when tiling, and show a per-zoom progress bar
- Commit
a85f664c9e4f2f7725334cb775416cfd6248da82- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-24 02:54 UTC (3 months ago)
- Parents
3b09777
Diff
scripts/maps/tile_build/src/geom.rs
diff --git a/scripts/maps/tile_build/src/geom.rs b/scripts/maps/tile_build/src/geom.rsindex ead28521c8..e794f1e4ee 100644--- a/scripts/maps/tile_build/src/geom.rs+++ b/scripts/maps/tile_build/src/geom.rs@@ -224,27 +224,41 @@ impl TileRange { /// /// Polygons keep whole-ring boxes: a polygon legitimately covers its interior tiles, /// and per-segment boxes would drop every tile strictly inside the ring.+/// Every tile a geometry can reach at zoom `z`, sorted and deduplicated.+///+/// The point of this over `tile_range(bounds(g))` is long thin diagonal features. A+/// bounding box is a terrible approximation of a route. Measured on one+/// transcontinental rail relation (40° of longitude) at z16:+///+/// | | tiles |+/// |---|---|+/// | its bounding box | 34,535,986 |+/// | tiles it actually crosses | 16,632 |+///+/// A 2077x difference, and each of those 34.5 million tiles ran a full clip and+/// simplify of the whole geometry. That is why `transit_lines` at planet scale took+/// hours.+///+/// Each SEGMENT's own box is used instead, and their union taken. A segment whose box+/// still spans many tiles is bisected until it does not, so the result does not depend+/// on the input's vertex density: the same path with only 2 vertices walks 16,626+/// tiles, within 0.1% of the 2000-vertex version. Without bisection a sparse line's+/// segment boxes are nearly as bad as the whole feature's.+///+/// It stays a conservative superset — a segment's box always contains the segment, and+/// bisecting preserves that — so no tile a feature reaches can be missed. A+/// line-walking algorithm would have to be exactly right to be safe; this only has to+/// be tight.+///+/// Polygons keep whole-ring boxes: a polygon legitimately covers its interior tiles,+/// and per-segment boxes would drop every tile strictly inside the ring. pub fn tiles_touched(g: &Geometry, z: u8, extent: u32, pad: f64, out: &mut Vec<(u64, u64)>) { out.clear();- let mut push_box = |ax: f64, ay: f64, bx: f64, by: f64| {- if !ax.is_finite() || !ay.is_finite() || !bx.is_finite() || !by.is_finite() {- return;- }- let b = Rect {- min_x: ax.min(bx),- min_y: ay.min(by),- max_x: ax.max(bx),- max_y: ay.max(by),- };- if let Some(r) = tile_range(&b, z, extent, pad) {- out.extend(r.iter());- }- }; match g { // A point's box is a point; one range call each is already tight. Geometry::Points(pts) => { for &(x, y) in pts {- push_box(x, y, x, y);+ push_box(out, x, y, x, y, z, extent, pad); } } Geometry::Lines(lines) => {@@ -252,10 +266,10 @@ pub fn tiles_touched(g: &Geometry, z: u8, extent: u32, pad: f64, out: &mut Vec<( match line.as_slice() { [] => {} // A degenerate one-point "line" still occupies a tile.- [(x, y)] => push_box(*x, *y, *x, *y),+ [(x, y)] => push_box(out, *x, *y, *x, *y, z, extent, pad), _ => { for seg in line.windows(2) {- push_box(seg[0].0, seg[0].1, seg[1].0, seg[1].1);+ push_segment(out, seg[0], seg[1], z, extent, pad, 0); } } }@@ -275,7 +289,7 @@ pub fn tiles_touched(g: &Geometry, z: u8, extent: u32, pad: f64, out: &mut Vec<( min = (min.0.min(x), min.1.min(y)); max = (max.0.max(x), max.1.max(y)); }- push_box(min.0, min.1, max.0, max.1);+ push_box(out, min.0, min.1, max.0, max.1, z, extent, pad); } } }@@ -285,6 +299,76 @@ pub fn tiles_touched(g: &Geometry, z: u8, extent: u32, pad: f64, out: &mut Vec<( out.dedup(); } +/// A segment box this wide or tall is worth bisecting rather than filling.+///+/// 2 means "no more than a 2x2 tile block", which is the smallest box a segment+/// crossing a tile corner can have — so bisection stops as soon as it is tight rather+/// than recursing forever on a segment that genuinely straddles a corner.+const MAX_SEGMENT_TILES: u64 = 2;++/// Recursion cap, so a pathological coordinate cannot spin. At depth 24 a segment has+/// been bisected into 16 million pieces; anything still too wide is a broken input and+/// filling its box is the safe answer.+const MAX_BISECT_DEPTH: u32 = 24;++fn push_segment(+ out: &mut Vec<(u64, u64)>,+ a: Pt,+ b: Pt,+ z: u8,+ extent: u32,+ pad: f64,+ depth: u32,+) {+ let (ax, ay) = a;+ let (bx, by) = b;+ if !ax.is_finite() || !ay.is_finite() || !bx.is_finite() || !by.is_finite() {+ return;+ }+ let rect = Rect {+ min_x: ax.min(bx),+ min_y: ay.min(by),+ max_x: ax.max(bx),+ max_y: ay.max(by),+ };+ let Some(r) = tile_range(&rect, z, extent, pad) else { return };+ let spans_many = (r.x1 - r.x0) >= MAX_SEGMENT_TILES || (r.y1 - r.y0) >= MAX_SEGMENT_TILES;+ if spans_many && depth < MAX_BISECT_DEPTH {+ // Bisecting at the midpoint keeps the superset property: the two halves+ // together cover exactly the same line.+ let mid = ((ax + bx) / 2.0, (ay + by) / 2.0);+ push_segment(out, a, mid, z, extent, pad, depth + 1);+ push_segment(out, mid, b, z, extent, pad, depth + 1);+ return;+ }+ out.extend(r.iter());+}++#[allow(clippy::too_many_arguments)]+fn push_box(+ out: &mut Vec<(u64, u64)>,+ ax: f64,+ ay: f64,+ bx: f64,+ by: f64,+ z: u8,+ extent: u32,+ pad: f64,+) {+ if !ax.is_finite() || !ay.is_finite() || !bx.is_finite() || !by.is_finite() {+ return;+ }+ let b = Rect {+ min_x: ax.min(bx),+ min_y: ay.min(by),+ max_x: ax.max(bx),+ max_y: ay.max(by),+ };+ if let Some(r) = tile_range(&b, z, extent, pad) {+ out.extend(r.iter());+ }+}+ /// The tiles a world-space bounding box touches at zoom `z`, grown by `pad` /// world units so a feature just outside a tile still reaches into its buffer. ///@@ -555,45 +639,87 @@ mod tests { assert!(!touched.contains(&(40, 0)), "top-right corner is empty"); } - /// Safety property: it may over-include, never under-include. Anything it lists- /// is inside the feature's bounding box, and every segment's own box is covered.+ /// Safety property: it may over-include, never under-include. Everything it lists+ /// is inside the feature's bounding box, and every point ON the line lands in a+ /// tile it listed.+ ///+ /// Sampling the line rather than asserting each segment's whole box, because+ /// bisection deliberately produces a tighter set than those boxes — that is the+ /// optimisation. What must not change is that no part of the line is unreachable. #[test]- fn tiles_touched_is_a_subset_of_the_bounding_box_and_covers_every_segment() {+ fn tiles_touched_covers_every_point_on_the_line_and_nothing_outside_the_box() { let extent = 4096u32; let e = extent as f64;- let g = Geometry::Lines(vec![vec![+ let pts = vec![ (0.5 * e, 0.5 * e), (3.5 * e, 1.5 * e), (2.0 * e, 6.0 * e),- ]]);+ ];+ let g = Geometry::Lines(vec![pts.clone()]); let mut touched = Vec::new(); tiles_touched(&g, 8, extent, 0.0, &mut touched);+ let bbox = tile_range(&bounds(&g).unwrap(), 8, extent, 0.0).unwrap(); let inside: Vec<(u64, u64)> = bbox.iter().collect(); for t in &touched { assert!(inside.contains(t), "{t:?} is outside the bounding box"); } - // Each segment's own tile range must be fully present, which is what makes- // this a conservative superset of the tiles the line actually enters.- let pts = match &g {- Geometry::Lines(l) => l[0].clone(),- _ => unreachable!(),- };+ // Walk each segment finely; every sample's tile must have been listed. for seg in pts.windows(2) {- let b = Rect {- min_x: seg[0].0.min(seg[1].0),- min_y: seg[0].1.min(seg[1].1),- max_x: seg[0].0.max(seg[1].0),- max_y: seg[0].1.max(seg[1].1),- };- for t in tile_range(&b, 8, extent, 0.0).unwrap().iter() {- assert!(touched.contains(&t), "segment tile {t:?} was dropped");+ let steps = 2000;+ for i in 0..=steps {+ let t = i as f64 / steps as f64;+ let x = seg[0].0 + (seg[1].0 - seg[0].0) * t;+ let y = seg[0].1 + (seg[1].1 - seg[0].1) * t;+ let tile = ((x / e).floor() as u64, (y / e).floor() as u64);+ assert!(+ touched.contains(&tile),+ "point ({x}, {y}) is in tile {tile:?}, which was not listed"+ ); } } } + /// Bisection is what makes the result independent of vertex density: a 4-vertex+ /// line across a continent must walk about as many tiles as a 400-vertex one along+ /// the same path, rather than filling its segment boxes.+ #[test]+ fn a_sparse_line_walks_as_tightly_as_a_dense_one() {+ let extent = 4096u32;+ let e = extent as f64;+ let end = 40.0;++ let sparse = Geometry::Lines(vec![vec![(0.0, 0.0), (end * e, end * e)]]);+ let dense: Vec<Pt> = (0..=400)+ .map(|i| {+ let t = i as f64 / 400.0;+ (end * e * t, end * e * t)+ })+ .collect();+ let dense = Geometry::Lines(vec![dense]);++ let (mut a, mut b) = (Vec::new(), Vec::new());+ tiles_touched(&sparse, 8, extent, 0.0, &mut a);+ tiles_touched(&dense, 8, extent, 0.0, &mut b);++ let bbox = tile_range(&bounds(&sparse).unwrap(), 8, extent, 0.0).unwrap();+ assert!(+ (a.len() as u64) * 10 < bbox.len(),+ "sparse walked {} of the box's {}",+ a.len(),+ bbox.len()+ );+ // Within a small factor of each other, not orders apart.+ assert!(+ a.len() < b.len() * 3 && b.len() < a.len() * 3,+ "sparse {} vs dense {} should be comparable",+ a.len(),+ b.len()+ );+ }+ /// A polygon covers its interior, so its footprint stays the whole ring box -- /// per-segment boxes would drop every tile strictly inside it. #[test]
scripts/maps/tile_build/src/pyramid.rs
diff --git a/scripts/maps/tile_build/src/pyramid.rs b/scripts/maps/tile_build/src/pyramid.rsindex 358b6f0ee1..31a93386ef 100644--- a/scripts/maps/tile_build/src/pyramid.rs+++ b/scripts/maps/tile_build/src/pyramid.rs@@ -41,6 +41,8 @@ //! gap or a hairline overlap. Reproducing it needs a shared topology pass across //! features, which is a project of its own. +use std::io::Write;+ use crate::clip::clip_geometry; use crate::geom::{self, Geometry, IntGeometry}; use crate::mvt::{self, Feature as MvtFeature, GeomType, Layer, Tile, Value, DEFAULT_EXTENT};@@ -67,8 +69,12 @@ pub struct Options { /// Simplification tolerance multiplier; 1.0 is the default policy. pub simplification: f64, pub max_tile_bytes: usize,+ /// Print a per-zoom progress bar to stdout while tiling.+ ///+ /// Off by default so the library stays silent: the binaries turn it on, and the+ /// tests would otherwise interleave bars with their output.+ pub progress: bool, }- impl Options { pub fn new(layer: impl Into<String>, min_zoom: u8, max_zoom: u8) -> Options { Options {@@ -78,6 +84,70 @@ impl Options { extent: DEFAULT_EXTENT, simplification: 1.0, max_tile_bytes: DEFAULT_MAX_TILE_BYTES,+ progress: false,+ }+ }++ /// Show the tiling progress bar. What the `tile_*` binaries call.+ pub fn with_progress(mut self) -> Options {+ self.progress = true;+ self+ }+}++/// A one-line progress bar for one zoom's tile loop.+///+/// Deliberately the same shape `osm_ingest`'s passes print (`label [42%]`, carriage+/// return, no newline until the end) so a build log reads consistently across the two+/// crates. Single-threaded, so no atomics are needed.+///+/// The count is CANDIDATE tiles, which is the loop's length -- larger than the `tiles`+/// column in the report below, because a candidate whose geometry clips away to+/// nothing is never written. Saying "candidate" keeps the two numbers from looking+/// like a contradiction.+///+/// Throttled to whole percent: a planet zoom has millions of tiles and a write per+/// tile would cost more than the tiling.+struct Progress {+ label: String,+ total: usize,+ done: usize,+ last_pct: usize,+ on: bool,+}++impl Progress {+ fn new(layer: &str, z: u8, total: usize, on: bool) -> Progress {+ let p = Progress {+ label: format!("{layer} z{z}"),+ total,+ done: 0,+ last_pct: usize::MAX,+ on,+ };+ if p.on {+ print!("\r{:<28} [ 0%] {} candidate tile(s)", p.label, total);+ let _ = std::io::stdout().flush();+ }+ p+ }++ fn tick(&mut self) {+ self.done += 1;+ if !self.on || self.total == 0 {+ return;+ }+ let pct = self.done * 100 / self.total;+ if pct != self.last_pct {+ self.last_pct = pct;+ print!("\r{:<28} [{pct:>3}%] {} candidate tile(s)", self.label, self.total);+ let _ = std::io::stdout().flush();+ }+ }++ fn finish(&self) {+ if self.on {+ println!("\r{:<28} [100%] {} candidate tile(s)", self.label, self.total); } } }@@ -153,7 +223,9 @@ pub fn build_archive(features: &[Feature], opts: &Options) -> Result<(Vec<u8>, V let mut tiles: Vec<(u64, u64)> = by_tile.keys().copied().collect(); tiles.sort_unstable();+ let mut bar = Progress::new(&opts.layer, z, tiles.len(), opts.progress); for (tx, ty) in tiles {+ bar.tick(); let indices = &by_tile[&(tx, ty)]; let rect = geom::tile_rect(tx, ty, opts.extent, buffer); @@ -192,6 +264,7 @@ pub fn build_archive(features: &[Feature], opts: &Options) -> Result<(Vec<u8>, V stats.tiles += 1; builder.add_tile_raw(pmtiles::tile_id(z, tx, ty), body); }+ bar.finish(); report.push(stats); } @@ -507,6 +580,8 @@ pub fn cli_main(name: &str, accept: Accept, argv: &[String]) -> std::process::Ex extent, simplification, max_tile_bytes,+ // A planet layer spends minutes to hours per zoom, so the operator gets a bar.+ progress: true, }; let (bytes, report) = match build_archive(&features, &opts) { Ok(v) => v,