maps: show a progress bar in every tiler and in the join
- Commit
349aa65b00479de4912e24925fb70c444103f2c4- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-24 04:10 UTC (3 months ago)
- Parents
a645f01
Diff
scripts/maps/tile_build/src/bin/tile_join.rs
diff --git a/scripts/maps/tile_build/src/bin/tile_join.rs b/scripts/maps/tile_build/src/bin/tile_join.rsindex 11b9a1e314..235a8ffbce 100644--- a/scripts/maps/tile_build/src/bin/tile_join.rs+++ b/scripts/maps/tile_build/src/bin/tile_join.rs@@ -15,7 +15,7 @@ use std::process::ExitCode; use tile_build::pmtiles::Archive;-use tile_build::tiling::merge_archives;+use tile_build::tiling::merge_archives_with; fn main() -> ExitCode { let args: Vec<String> = std::env::args().collect();@@ -83,7 +83,7 @@ fn main() -> ExitCode { } let refs: Vec<&Archive> = archives.iter().collect();- let merged = match merge_archives(&refs) {+ let merged = match merge_archives_with(&refs, true) { Ok(m) => m, Err(e) => { eprintln!("tile_join: {e}");
scripts/maps/tile_build/src/bin/tile_points.rs
diff --git a/scripts/maps/tile_build/src/bin/tile_points.rs b/scripts/maps/tile_build/src/bin/tile_points.rsindex f8d83c8c98..59e5f4ffb7 100644--- a/scripts/maps/tile_build/src/bin/tile_points.rs+++ b/scripts/maps/tile_build/src/bin/tile_points.rs@@ -19,7 +19,7 @@ use std::process::ExitCode; use tile_build::geojson; use tile_build::geom::Geometry;-use tile_build::tiling::{build_point_archive, Point};+use tile_build::tiling::{build_point_archive_with, Point}; fn main() -> ExitCode { let args: Vec<String> = std::env::args().collect();@@ -108,7 +108,7 @@ fn main() -> ExitCode { return ExitCode::FAILURE; } - let bytes = match build_point_archive(&layer, &points, minzoom, maxzoom) {+ let bytes = match build_point_archive_with(&layer, &points, minzoom, maxzoom, true) { Ok(b) => b, Err(e) => { eprintln!("tile_points: {e}");
scripts/maps/tile_build/src/lib.rs
diff --git a/scripts/maps/tile_build/src/lib.rs b/scripts/maps/tile_build/src/lib.rsindex 83b931ae4d..e266e2c728 100644--- a/scripts/maps/tile_build/src/lib.rs+++ b/scripts/maps/tile_build/src/lib.rs@@ -54,6 +54,7 @@ pub mod geom; pub mod gz; pub mod mvt; pub mod pmtiles;+pub mod progress; pub mod proto; pub mod pyramid; pub mod simplify;
scripts/maps/tile_build/src/progress.rs
diff --git a/scripts/maps/tile_build/src/progress.rs b/scripts/maps/tile_build/src/progress.rsnew file mode 100644index 0000000000..17e55b6f54--- /dev/null+++ b/scripts/maps/tile_build/src/progress.rs@@ -0,0 +1,59 @@+//! A one-line progress bar for the long tiling loops.+//!+//! 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. A planet layer spends minutes to hours per zoom and every tiler was+//! previously silent for all of it.+//!+//! Single-threaded, so no atomics: the tiling loops are one thread each.+use std::io::Write;++/// Progress over one zoom's tile loop.+pub struct Progress {+ label: String,+ total: usize,+ done: usize,+ last_pct: usize,+ on: bool,+}++impl Progress {+ /// `unit` names what is being counted, because the number is not always the tile+ /// count the report ends up printing — see [`Progress::new`]'s callers.+ pub fn new(label: String, total: usize, unit: &str, on: bool) -> Progress {+ let p = Progress {+ label,+ total,+ done: 0,+ last_pct: usize::MAX,+ on,+ };+ if p.on {+ print!("\r{:<28} [ 0%] {} {unit}", p.label, p.total);+ let _ = std::io::stdout().flush();+ }+ p+ }++ /// One unit done. Throttled to whole percent: a planet zoom has millions of tiles+ /// and a write per tile would cost more than the tiling.+ pub fn tick(&mut self, unit: &str) {+ 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}%] {} {unit}", self.label, self.total);+ let _ = std::io::stdout().flush();+ }+ }++ /// Leave the finished line on screen, so a multi-zoom build shows every zoom.+ pub fn finish(&self, unit: &str) {+ if self.on {+ println!("\r{:<28} [100%] {} {unit}", self.label, self.total);+ }+ }+}
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 31a93386ef..c3e288081a 100644--- a/scripts/maps/tile_build/src/pyramid.rs+++ b/scripts/maps/tile_build/src/pyramid.rs@@ -41,12 +41,11 @@ //! 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}; use crate::pmtiles::{self, Builder};+use crate::progress::Progress; use crate::proto::{err, Result}; use crate::simplify; @@ -97,60 +96,11 @@ impl Options { /// 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);- }- }-}+const CANDIDATES: &str = "candidate tile(s)"; /// What one zoom cost, for the per-zoom report the drop policy owes the operator. #[derive(Debug, Default, Clone, PartialEq)]@@ -223,9 +173,14 @@ 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);+ let mut bar = Progress::new(+ format!("{} z{z}", opts.layer),+ tiles.len(),+ CANDIDATES,+ opts.progress,+ ); for (tx, ty) in tiles {- bar.tick();+ bar.tick(CANDIDATES); let indices = &by_tile[&(tx, ty)]; let rect = geom::tile_rect(tx, ty, opts.extent, buffer); @@ -264,7 +219,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();+ bar.finish(CANDIDATES); report.push(stats); }
scripts/maps/tile_build/src/tiling.rs
diff --git a/scripts/maps/tile_build/src/tiling.rs b/scripts/maps/tile_build/src/tiling.rsindex b4d94145c5..13ee6c7b36 100644--- a/scripts/maps/tile_build/src/tiling.rs+++ b/scripts/maps/tile_build/src/tiling.rs@@ -10,6 +10,7 @@ use crate::geom::project; use crate::mvt::{self, Feature, GeomType, Layer, Tile, Value, DEFAULT_EXTENT}; use crate::pmtiles::{self, Archive, Builder};+use crate::progress::Progress; use crate::proto::Result; use std::collections::HashMap; @@ -75,12 +76,27 @@ pub fn tile_points( out } -/// Build a single-layer point archive across `min_zoom..=max_zoom`.+/// Build a single-layer point archive across `min_zoom..=max_zoom`, silently. pub fn build_point_archive( layer_name: &str, points: &[Point], min_zoom: u8, max_zoom: u8,+) -> Result<Vec<u8>> {+ build_point_archive_with(layer_name, points, min_zoom, max_zoom, false)+}++/// As [`build_point_archive`], with a per-zoom progress bar when `progress`.+///+/// The binaries pass true and the library defaults to false, so tests stay quiet.+/// Worth having for points and not only for lines: `ma_pois` is 22.6 M features at+/// planet scale and used to print nothing at all between its start and its report.+pub fn build_point_archive_with(+ layer_name: &str,+ points: &[Point],+ min_zoom: u8,+ max_zoom: u8,+ progress: bool, ) -> Result<Vec<u8>> { let mut b = Builder::new(); b.min_zoom = min_zoom;@@ -88,13 +104,24 @@ pub fn build_point_archive( b.center_zoom = min_zoom; b.metadata = point_metadata(layer_name, min_zoom, max_zoom).into_bytes(); for z in min_zoom..=max_zoom {- for (id, body) in tile_points(layer_name, points, z, DEFAULT_EXTENT) {+ // `tile_points` buckets and MVT-encodes the whole zoom before returning, so+ // the bar covers the gzip loop -- which is where the time goes -- and the+ // bucketing shows as a pause before the bar appears.+ let tiles = tile_points(layer_name, points, z, DEFAULT_EXTENT);+ let mut bar = Progress::new(format!("{layer_name} z{z}"), tiles.len(), TILES, progress);+ for (id, body) in tiles {+ bar.tick(TILES); b.add_tile_raw(id, crate::gz::compress(&body)); }+ bar.finish(TILES); } b.build() } +/// Points are bucketed before they are counted, so unlike the pyramid's candidates+/// every one of these really is written.+const TILES: &str = "tile(s)";+ /// The `json` metadata blob a PMTiles archive carries. MapLibre does not need it /// to render a styled layer, but `pmtiles show` and friends read it, so emitting a /// truthful vector_layers list keeps the archive introspectable.@@ -127,6 +154,11 @@ fn point_metadata(layer_name: &str, min_zoom: u8, max_zoom: u8) -> String { /// far the most expensive thing this function can do. It also keeps peak memory down, /// since those tiles stay compressed while they wait. pub fn merge_archives(inputs: &[&Archive]) -> Result<Vec<u8>> {+ merge_archives_with(inputs, false)+}++/// As [`merge_archives`], with a progress bar over the write loop when `progress`.+pub fn merge_archives_with(inputs: &[&Archive], progress: bool) -> Result<Vec<u8>> { let mut min_zoom = u8::MAX; let mut max_zoom = 0u8; // (west, south, east, north), in e7 degrees.@@ -176,7 +208,9 @@ pub fn merge_archives(inputs: &[&Archive]) -> Result<Vec<u8>> { let mut ids: Vec<u64> = collected.keys().copied().collect(); ids.sort_unstable();+ let mut bar = Progress::new("merging".to_string(), ids.len(), TILES, progress); for id in ids {+ bar.tick(TILES); let bodies = &collected[&id]; // Sole owner, already in the compression the builder writes: hand the // producer's bytes straight through. No inflate, no deflate, and the tile is@@ -200,6 +234,7 @@ pub fn merge_archives(inputs: &[&Archive]) -> Result<Vec<u8>> { }; b.add_tile_raw(id, crate::gz::compress(&merged)); }+ bar.finish(TILES); b.build() }