From 5b7f4c432cec20d414c881f5c06a24bcf5e11d6b Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 2 Dec 2026 19:16:00 -0600 Subject: [PATCH 1/4] fix(render): draw in-place exports in bands --- crates/render/src/lib.rs | 39 ++-- crates/render/src/raster.rs | 426 ++++++++++++++++++++++++++++++++++-- 2 files changed, 436 insertions(+), 29 deletions(-) diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index c405463070..13aca45661 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -5615,23 +5615,34 @@ fn render_frame( let mut draw_it = |pm: &mut Pixmap, opacity: f32| match address.as_deref() { // Drawn from less than the view needs (a coarser level, tiles that couldn't be // read): shown, and said with the tile errors. - Some(a) => match raster::draw_in_place(pm, a, data, &rl.style, stats, view, opacity, full_res_rasters, place, light) { - Ok(pending) => { - tile_out.pending |= pending; - if pending && !tile_out.pending_in_place.contains(&layer.id) { - tile_out.pending_in_place.push(layer.id); - } - } - Err(e) => { - let e = format!("{}: {e}", layer.name); - if !tile_out.errors.contains(&e) { - tile_out.errors.push(e.clone()); + Some(a) => { + #[cfg(not(target_arch = "wasm32"))] + let result = + if full_res_rasters && !NO_TILE_WAIT.with(std::cell::Cell::get) && !NO_SHADOW_WAIT.with(std::cell::Cell::get) { + raster::export_in_place(pm, a, data, &rl.style, stats, view, opacity, place, light) + } else { + raster::draw_in_place(pm, a, data, &rl.style, stats, view, opacity, full_res_rasters, place, light) + }; + #[cfg(target_arch = "wasm32")] + let result = raster::draw_in_place(pm, a, data, &rl.style, stats, view, opacity, full_res_rasters, place, light); + match result { + Ok(pending) => { + tile_out.pending |= pending; + if pending && !tile_out.pending_in_place.contains(&layer.id) { + tile_out.pending_in_place.push(layer.id); + } } - if !tile_out.partial.iter().any(|(id, _)| *id == layer.id) { - tile_out.partial.push((layer.id, e)); + Err(e) => { + let e = format!("{}: {e}", layer.name); + if !tile_out.errors.contains(&e) { + tile_out.errors.push(e.clone()); + } + if !tile_out.partial.iter().any(|(id, _)| *id == layer.id) { + tile_out.partial.push((layer.id, e)); + } } } - }, + } None => raster::draw(pm, data, &rl.style, stats, view, opacity, full_res_rasters, place, light), }; if offscreen { diff --git a/crates/render/src/raster.rs b/crates/render/src/raster.rs index 32899f8b0d..d2e3300486 100644 --- a/crates/render/src/raster.rs +++ b/crates/render/src/raster.rs @@ -135,6 +135,157 @@ pub(crate) fn draw( draw_with(pm, r, style, stats, view, opacity, full_res, place, threads, light); } +/// One output band, in the whole output's coordinates. Statistics passes retain only +/// their accumulators; the sampled values belong to this band alone. +struct OutputBand<'a> { + rows: (usize, usize), + samples: Option<&'a std::cell::RefCell>, +} + +/// Two bounded passes over the sampled view: range and sums, then histogram. Sums +/// follow screen order, as `BandStats::from_values` does for a whole draw. +#[derive(Default)] +struct ViewStats { + bands: Vec, + sums: Vec<(f64, f64)>, + histogram: bool, +} + +impl ViewStats { + fn add(&mut self, values: &[f32], nb: usize) { + if self.bands.is_empty() { + self.bands = + vec![BandStats { min: f64::INFINITY, max: f64::NEG_INFINITY, categories: Some(Vec::new()), ..BandStats::default() }; nb]; + self.sums = vec![(0.0, 0.0); nb]; + } + for row in values.chunks_exact(nb) { + for ((s, sum), &v) in self.bands.iter_mut().zip(&mut self.sums).zip(row) { + if !v.is_finite() { + continue; + } + let x = f64::from(v); + if self.histogram { + let span = s.max - s.min; + let last = rustgis_data::raster::HISTOGRAM_BINS - 1; + let k = last as f64 / if span > 0.0 { span } else { 1.0 }; + let bin = rustgis_data::raster::round_positive((x - s.min) * k) as usize; + if let Some(n) = s.histogram.get_mut(bin.min(last)) { + *n += 1; + } + } else { + s.count += 1; + if x < s.min { + s.min = x; + } + if x > s.max { + s.max = x; + } + sum.0 += x; + sum.1 += x * x; + if x.fract() != 0.0 { + s.categories = None; + } + if let Some(c) = &mut s.categories { + let category = x as i64 as f64; + if let Err(i) = c.binary_search_by(|v| v.total_cmp(&category)) { + c.insert(i, category); + } + if c.len() > 256 { + s.categories = None; + } + } + } + } + } + } + + #[cfg(any(test, not(target_arch = "wasm32")))] + fn histogram(&mut self) { + for (s, sum) in self.bands.iter_mut().zip(&self.sums) { + if s.count == 0 { + *s = BandStats::default(); + continue; + } + s.mean = sum.0 / s.count as f64; + s.std_dev = (sum.1 / s.count as f64 - s.mean * s.mean).max(0.0).sqrt(); + s.histogram = vec![0; rustgis_data::raster::HISTOGRAM_BINS]; + if let Some(c) = &mut s.categories { + c.sort_by(f64::total_cmp); + } + } + self.histogram = true; + } +} + +/// Completes each native export band before asking for the next one's tiles. A +/// band that exceeds the window budget is split, never read from a coarser level. +/// One tile read or one band's sampling can run between cancellation checks. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn export_in_place( + pm: &mut Pixmap, + address: &str, + r: &Raster, + style: &RasterStyle, + stats: Option<&[BandStats]>, + view: &View, + opacity: f32, + place: Place, + light: Lights, +) -> Result { + let deadline = web_time::Instant::now() + rustgis_data::raster::IN_PLACE_WAIT; + let samples = std::cell::RefCell::new(ViewStats::default()); + let mut fixed = style.clone(); + fixed.dra = false; + let passes = if style.dra { 3 } else { 1 }; + let height = pm.height() as usize; + rustgis_data::progress::begin(height * passes); + for pass in 0..passes { + let collect = style.dra && pass < 2; + let final_stats = samples.borrow().bands.clone(); + let stats = if style.dra && !collect { Some(final_stats.as_slice()) } else { stats }; + let mut first = 0; + while first < height { + let mut end = (first + 128).min(height); + let mut idle = 0; + loop { + rustgis_data::progress::check().map_err(|e| e.to_string())?; + if crate::cancel::requested() { + return Err(rustgis_data::DataError::Cancelled.to_string()); + } + let band = OutputBand { rows: (first, end), samples: collect.then_some(&samples) }; + let generation = rustgis_tiles::cache::generation(); + match draw_in_place_region(pm, address, r, &fixed, stats, view, opacity, true, place, light, Some(&band)) { + Ok(false) => break, + Err(e) if (e == TOO_LARGE || e == GENERALISE_CUT) && end - first > 1 => { + end = first + (end - first).div_ceil(2); + continue; + } + Err(e) => return Err(e), + Ok(true) => {} + } + let left = deadline.saturating_duration_since(web_time::Instant::now()); + if left.is_zero() || idle >= 6 { + return Err("its tiles didn't all arrive before the export stopped waiting".into()); + } + rustgis_tiles::cache::wait_for_news_unless(generation, left.min(web_time::Duration::from_secs(30)), || { + crate::cancel::requested() || rustgis_data::progress::check().is_err() + }); + if generation == rustgis_tiles::cache::generation() { + idle += 1; + } else { + idle = 0; + } + } + rustgis_data::progress::step(end - first).map_err(|e| e.to_string())?; + first = end; + } + if collect && pass == 0 { + samples.borrow_mut().histogram(); + } + } + Ok(false) +} + /// Screen points a side sampled to find the part of a raster read in place that is in view, and /// how finely. const WINDOW_GRID: usize = 17; @@ -176,6 +327,22 @@ pub(crate) fn draw_in_place( full_res: bool, place: Place, light: Lights, +) -> Result { + draw_in_place_region(pm, address, r, style, stats, view, opacity, full_res, place, light, None) +} + +fn draw_in_place_region( + pm: &mut Pixmap, + address: &str, + r: &Raster, + style: &RasterStyle, + stats: Option<&[BandStats]>, + view: &View, + opacity: f32, + full_res: bool, + place: Place, + light: Lights, + band: Option<&OutputBand<'_>>, ) -> Result { let bands = style.renderer.bands(); let own: Vec = match stats { @@ -185,7 +352,21 @@ pub(crate) fn draw_in_place( let stats = stats.unwrap_or(&own); // Its cast shadows are only worked out when the overview is the whole image (see // `RasterLayer::cast_shadows`), so only it is drawn with them. - let whole = |pm: &mut Pixmap| draw(pm, r, style, Some(stats), view, opacity, full_res, place, light); + let whole = |pm: &mut Pixmap| { + draw_region( + pm, + r, + style, + Some(stats), + view, + opacity, + full_res, + place, + threads_for(pm.width() as usize * pm.height() as usize), + light, + band, + ) + }; let full = r.overview.unwrap_or((r.width, r.height)); if full.0 <= r.width { whole(pm); @@ -202,18 +383,27 @@ pub(crate) fn draw_in_place( let info = match rustgis_tiles::cog::info(address) { Ok(Some(info)) => info, Ok(None) => { - whole(pm); + if band.is_none() { + whole(pm); + } return Ok(true); } Err(e) => { - whole(pm); + if band.is_none() { + whole(pm); + } return Err(e); } }; - let Some(want) = in_view(r, view, place, pm.width(), pm.height(), full_res) else { + let Some(mut want) = in_view(r, view, place, pm.width(), pm.height(), full_res) else { whole(pm); return Ok(false); }; + if let Some(band) = band { + let Some(clipped) = in_view_rows(r, view, place, pm.width(), pm.height(), full_res, band.rows) else { return Ok(false) }; + // Pick the level with the whole view; only its read window is clipped. + want = InView { per_px: want.per_px, ..clipped }; + } // The coarsest level at least as fine as the view (within a quarter); zoomed in past the full // image, the full image. let levels = &info.layout.levels; @@ -265,6 +455,10 @@ pub(crate) fn draw_in_place( window = Some((level, cols, rows)); break; } + // An export splits again before giving up any of the required padding. + if band.is_some() { + return Err(if full > least && part(least).is_some() { GENERALISE_CUT } else { TOO_LARGE }.into()); + } // A generalisation reaching farther past the view than the tiles kept hold at the level // the view needs (#3145) is read there with as much of its reach as fits, the most that // does, rather than the view from a coarser level: said. @@ -284,7 +478,9 @@ pub(crate) fn draw_in_place( level += 1; } let Some((level, cols, rows)) = window else { - whole(pm); + if band.is_none() { + whole(pm); + } return Err(TOO_LARGE.into()); }; // The image at the overview's level is the overview: reached from a finer level the view @@ -315,8 +511,18 @@ pub(crate) fn draw_in_place( } } } + if band.is_some() { + if pending { + return Ok(true); + } + if failed { + return Err("some of its tiles couldn't be read".into()); + } + } if parts.is_empty() { - whole(pm); + if band.is_none() { + whole(pm); + } return Ok(pending); } let mut own_style = style.clone(); @@ -344,7 +550,19 @@ pub(crate) fn draw_in_place( } } else { for part in &parts { - draw(pm, part, &own_style, Some(stats), view, opacity, full_res, place, Lights::default()); + draw_region( + pm, + part, + &own_style, + Some(stats), + view, + opacity, + full_res, + place, + threads_for(pm.width() as usize * pm.height() as usize), + Lights::default(), + band, + ); } } match (pending, failed) { @@ -388,12 +606,17 @@ struct InView { /// [`InView`] of a raster drawn into a `width × height` pixmap, from a grid of screen points; /// `None` when it isn't in view. fn in_view(r: &Raster, view: &View, place: Place, width: u32, height: u32, full_res: bool) -> Option { + in_view_rows(r, view, place, width, height, full_res, (0, height as usize)) +} + +fn in_view_rows(r: &Raster, view: &View, place: Place, width: u32, height: u32, full_res: bool, rows: (usize, usize)) -> Option { let k = if view.pixels_per_point >= 1.5 && !full_res { view.pixels_per_point } else { 1.0 }; - let (vw, vh) = (f64::from(width) / k, f64::from(height) / k); + let (vw, vh) = (f64::from(width) / k, (rows.1.min(height as usize) - rows.0) as f64 / k); let n = WINDOW_GRID; let direct = direct_of(r, place); - let at = - |i: usize, j: usize| source(r, view, place, direct.as_ref(), k, vw * i as f64 / (n - 1) as f64, vh * j as f64 / (n - 1) as f64); + let at = |i: usize, j: usize| { + source(r, view, place, direct.as_ref(), k, vw * i as f64 / (n - 1) as f64, rows.0 as f64 / k + vh * j as f64 / (n - 1) as f64) + }; let grid: Vec> = (0..n * n).map(|p| at(p % n, p / n)).collect(); let (mut c0, mut c1, mut r0, mut r1) = (f64::INFINITY, f64::NEG_INFINITY, f64::INFINITY, f64::NEG_INFINITY); let (rw, rh) = (f64::from(r.width), f64::from(r.height)); @@ -456,6 +679,22 @@ fn draw_with( place: Place, threads: usize, light: Lights, +) { + draw_region(pm, r, style, stats, view, opacity, full_res, place, threads, light, None); +} + +fn draw_region( + pm: &mut Pixmap, + r: &Raster, + style: &RasterStyle, + stats: Option<&[BandStats]>, + view: &View, + opacity: f32, + full_res: bool, + place: Place, + threads: usize, + light: Lights, + band: Option<&OutputBand<'_>>, ) { // On high-density screens, compute at logical-pixel resolution and scale up (unless asked // for full resolution, as exports are): a quarter of the work at 2×. @@ -525,6 +764,7 @@ fn draw_with( clip((ay.max(by) * ppp).ceil(), h) as usize, ) }; + let (y0, y1) = band.map_or((y0, y1), |b| (y0.max(b.rows.0), y1.min(b.rows.1))); if x0 >= x1 || y0 >= y1 { return; } @@ -533,7 +773,10 @@ fn draw_with( // Exact source positions on a coarse grid, its rows spread over the threads (a projected // raster's transformation is most of a frame's work on one, #1792): each position is worked // out alone, so the grid is the same on any number. - let (gw, gh) = (bw / STEP + 2, bh / STEP + 2); + // Exports anchor their interpolation grid to the output, including when a + // read window or an output band starts between its grid lines. + let (gx0, gy0) = if full_res { (x0 / STEP * STEP, y0 / STEP * STEP) } else { (x0, y0) }; + let (gw, gh) = ((x1 - gx0) / STEP + 2, (y1 - gy0) / STEP + 2); let mut grid = vec![None; gw * gh]; // (A few points aren't worth a thread.) let grid_threads = if gw * gh < 1024 { 1 } else { threads.clamp(1, gh) }; @@ -541,7 +784,7 @@ fn draw_with( each_chunk(grid_threads, grid.chunks_mut(grid_rows * gw).enumerate(), |(i, chunk)| { for (j, cell) in chunk.iter_mut().enumerate() { let (gx, gy) = (j % gw, i * grid_rows + j / gw); - *cell = source(r, view, place, direct.as_ref(), k, (x0 + gx * STEP) as f64, (y0 + gy * STEP) as f64); + *cell = source(r, view, place, direct.as_ref(), k, (gx0 + gx * STEP) as f64, (gy0 + gy * STEP) as f64); } }); @@ -592,7 +835,8 @@ fn draw_with( k, rotated: rotated.then_some((centre, sin, cos)), x0, - y0, + y0: gy0, + gx0, bw, grid: &grid, gw, @@ -623,7 +867,7 @@ fn draw_with( let (dx, dy) = (x - centre.0, y - centre.1); source(r, view, place, direct.as_ref(), k, centre.0 + dx * cos - dy * sin, centre.1 + dx * sin + dy * cos) }; - (0..gw * gh).map(|i| back(((x0 + i % gw * STEP) as f64, (y0 + i / gw * STEP) as f64))).collect() + (0..gw * gh).map(|i| back(((gx0 + i % gw * STEP) as f64, (gy0 + i / gw * STEP) as f64))).collect() } else { Vec::new() }; @@ -659,6 +903,11 @@ fn draw_with( |(i, (((((v, sh), al), am), fl), ro))| pass1.rows(y0 + i * rows, v, sh, al, am, fl, ro), ); + if let Some(samples) = band.and_then(|b| b.samples) { + samples.borrow_mut().add(&vals, nb); + return; + } + // Dynamic range adjustment: statistics of the values in view. let view_stats: Option> = style.dra.then(|| (0..nb).map(|k| BandStats::from_values(&vals.iter().skip(k).step_by(nb).copied().collect::>())).collect()); @@ -1151,6 +1400,7 @@ struct Pass1<'a> { /// A rotated raster's centre on screen and the sine and cosine of its back-rotation. rotated: Option<((f64, f64), f64, f64)>, x0: usize, + gx0: usize, y0: usize, bw: usize, /// Exact source positions every `STEP` pixels, `gw` to a row. @@ -1247,7 +1497,7 @@ impl Pass1<'_> { ) } None => { - let u = dx as f64 + 0.5; + let u = (px - self.gx0) as f64 + 0.5; let gx = (u / STEP as f64) as usize; let fx = u / STEP as f64 - gx as f64; match (cols.get(gx).copied().flatten(), cols.get(gx + 1).copied().flatten()) { @@ -1557,6 +1807,152 @@ mod tests { use super::*; + #[test] + fn output_bands_statistics_match_whole_view_statistics() { + let values: Vec = + (0..1517).flat_map(|i| [if i % 17 == 0 { f32::NAN } else { (i % 311) as f32 * 0.17 }, (i % 23) as f32]).collect(); + let mut got = ViewStats::default(); + for band in values.chunks(182) { + got.add(band, 2); + } + got.histogram(); + for band in values.chunks(182) { + got.add(band, 2); + } + for (b, s) in got.bands.iter().enumerate() { + let whole = BandStats::from_values(&values.iter().skip(b).step_by(2).copied().collect::>()); + assert_eq!(*s, whole); + } + } + + /// Both DRA passes use all screen rows, including the last, short band. The + /// changing range makes a stretch computed separately in each band fail. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn output_bands_match_a_whole_draw_with_and_without_dra() { + use rustgis_map::raster::Stretch; + let r = + crate::testing::grid(Crs::WebMercator, (-700.0, 500.0), 2.0, (700, 500), |x, y| (x * 0.13 + y * 0.7).sin() * 50.0 + y).unwrap(); + let view = + View { width: 617.0, height: 389.0, pixels_per_point: 1.0, camera: Camera::new(Coord::new(17.3, 23.7), 2.3), sky: false }; + let place = Place::Flat(&Flat::Mercator, "EPSG:3857"); + for dra in [false, true] { + for stretch in [Stretch::MinMax, Stretch::HistogramEqualize, Stretch::StandardDeviation { n: 2.0 }] { + let style = RasterStyle { dra, stretch, resampling: Resampling::Bilinear, ..RasterStyle::default_for(&r) }; + let mut whole = Pixmap::new(617, 389).unwrap(); + let mut bands = whole.clone(); + draw(&mut whole, &r, &style, None, &view, 0.7, true, place, Lights::default()); + assert!(!export_in_place(&mut bands, "unused", &r, &style, None, &view, 0.7, place, Lights::default()).unwrap()); + let max = whole.data().iter().zip(bands.data()).map(|(a, b)| a.abs_diff(*b)).max().unwrap(); + assert_eq!(max, 0, "DRA {dra}, {stretch:?}"); + assert!(bands.pixels().iter().any(|p| p.alpha() > 0)); + } + } + } + + /// More decoded samples than one window allows, even though the output is + /// small. Interleaved unused bands count against the same tile budget. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn output_bands_complete_an_image_too_large_for_one_window() { + use rustgis_data::raster::cog::{self, Example}; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("many.tif"); + std::fs::write(&path, cog::example_with(&Example { size: 1024, tile: 256, bands: 33, ..Example::default() })).unwrap(); + let address = path.to_str().unwrap(); + let r = rustgis_tiles::cog::raster::overview_blocking(address).unwrap(); + assert!(r.width < 1024); + let center = lonlat_to_merc(Coord::new(10.5, 45.5)); + let west = lonlat_to_merc(Coord::new(10.0, 45.5)); + let view = View { + width: 1024.0, + height: 1024.0, + pixels_per_point: 1.0, + camera: Camera::new(center, (center.x - west.x) * 2.0 / 1024.0), + sky: false, + }; + let style = RasterStyle::default_for(&r); + let place = Place::Flat(&Flat::Mercator, "EPSG:3857"); + let mut whole = Pixmap::new(1024, 1024).unwrap(); + let mut refused = false; + for _ in 0..20 { + let generation = rustgis_tiles::cache::generation(); + match draw_in_place(&mut whole, address, &r, &style, None, &view, 1.0, true, place, Lights::default()) { + Err(e) => { + assert_eq!(e, TOO_LARGE); + refused = true; + break; + } + Ok(false) => panic!("the whole window should exceed the budget"), + Ok(true) => { + // test-wait: completion of the legacy draw's requested local tiles. + rustgis_tiles::cache::wait_for_news_unless(generation, web_time::Duration::from_secs(5), || false); + } + } + } + assert!(refused); + let mut bands = Pixmap::new(1024, 1024).unwrap(); + assert!(!export_in_place(&mut bands, address, &r, &style, None, &view, 1.0, place, Lights::default()).unwrap()); + assert!(bands.pixels().iter().any(|p| p.alpha() > 0)); + // The same in-place file at a size whose whole read fits, with a + // nonlinear stretch so separately adjusted bands would differ. + let fit = View { width: 512.0, height: 512.0, pixels_per_point: 1.0, camera: view.camera, sky: false }; + for dra in [false, true] { + let mut style = style.clone(); + style.dra = dra; + style.stretch = rustgis_map::raster::Stretch::HistogramEqualize; + let mut bands = Pixmap::new(512, 512).unwrap(); + export_in_place(&mut bands, address, &r, &style, None, &fit, 1.0, place, Lights::default()).unwrap(); + let mut whole = Pixmap::new(512, 512).unwrap(); + assert!(!draw_in_place(&mut whole, address, &r, &style, None, &fit, 1.0, true, place, Lights::default()).unwrap()); + let max = whole.data().iter().zip(bands.data()).map(|(a, b)| a.abs_diff(*b)).max().unwrap(); + assert_eq!(max, 0, "in-place DRA {dra}"); + } + rustgis_tiles::cog::forget_local_file(address); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn output_bands_stop_when_the_export_is_cancelled() { + use std::sync::{ + Arc, + atomic::{AtomicU64, Ordering}, + }; + struct AfterBand(AtomicU64); + impl rustgis_data::progress::Probe for AfterBand { + fn cancelled(&self) -> bool { + self.0.load(Ordering::Relaxed) >= 128 + } + fn begin(&self, _: u64) { + self.0.store(0, Ordering::Relaxed); + } + fn advance(&self, n: u64) { + self.0.fetch_add(n, Ordering::Relaxed); + } + } + let probe = Arc::new(AfterBand(AtomicU64::new(0))); + let _installed = rustgis_data::progress::install(Some(probe.clone())); + let r = crate::testing::grid(Crs::WebMercator, (-150.0, 150.0), 10.0, (30, 30), |_, _| 50.0).unwrap(); + let style = RasterStyle::default_for(&r); + let view = View { width: 300.0, height: 300.0, pixels_per_point: 1.0, camera: Camera::new(Coord::new(0.0, 0.0), 1.0), sky: false }; + let mut image = Pixmap::new(300, 300).unwrap(); + let result = export_in_place( + &mut image, + "unused", + &r, + &style, + None, + &view, + 1.0, + Place::Flat(&Flat::Mercator, "EPSG:3857"), + Lights::default(), + ); + assert_eq!(result, Err(rustgis_data::DataError::Cancelled.to_string())); + assert_eq!(probe.0.load(Ordering::Relaxed), 128); + assert!(image.pixels().iter().take(128 * 300).any(|p| p.alpha() > 0)); + assert!(image.pixels().iter().skip(128 * 300).all(|p| p.alpha() == 0)); + } + /// A Shaded Relief without a ramp or rock, lit from `azimuth` and `altitude`. fn grey_relief(azimuth: f64, altitude: f64, z_factor: f64, shadow: f64, sky: f64) -> RasterRenderer { RasterRenderer::ShadedRelief { From 52df732735dcdec06951c18ca283ad4e681ff625 Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 2 Dec 2026 21:06:54 -0600 Subject: [PATCH 2/4] fix(render): preserve export completion and relief parity --- crates/render/src/lib.rs | 38 +++++++++ crates/render/src/raster.rs | 149 +++++++++++++++++++++++++++++++++--- 2 files changed, 178 insertions(+), 9 deletions(-) diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 13aca45661..ad461776ea 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -9956,6 +9956,10 @@ pub fn render_drawing(map: &mut Map, view: &View) -> Result) -> Option { let r = render_complete_recorded(map, view, failed_source)?; + rustgis_data::progress::check().ok()?; + if cancel::requested() { + return None; + } let mut pm = match r.recorded { Some(c) => c, None => { @@ -10233,6 +10237,40 @@ mod tests { } } + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn output_bands_cancelled_vector_export_returns_no_drawing() { + use std::sync::{ + Arc, + atomic::{AtomicU64, Ordering}, + }; + struct AfterBand(AtomicU64); + impl rustgis_data::progress::Probe for AfterBand { + fn cancelled(&self) -> bool { + self.0.load(Ordering::Relaxed) >= 128 + } + fn begin(&self, _: u64) { + self.0.store(0, Ordering::Relaxed); + } + fn advance(&self, n: u64) { + self.0.fetch_add(n, Ordering::Relaxed); + } + } + let r = crate::testing::grid(rustgis_data::raster::Crs::WebMercator, (-150.0, 150.0), 10.0, (30, 30), |_, _| 50.0).unwrap(); + let mut layer = rustgis_map::RasterLayer::from_path(r, "unused.tif"); + layer.in_place = true; + let mut map = Map::new("Map"); + map.basemap = rustgis_map::Basemap::Blank; + map.add_raster("raster", layer); + let view = View { width: 300.0, height: 300.0, pixels_per_point: 1.0, camera: Camera::new(Coord::new(0.0, 0.0), 1.0), sky: false }; + // The same real vector-export entry point succeeds before cancellation is installed. + assert!(render_drawing(&mut map, &view).is_ok()); + let probe = Arc::new(AfterBand(AtomicU64::new(0))); + let _installed = rustgis_data::progress::install(Some(probe.clone())); + assert!(render_drawing(&mut map, &view).is_err()); + assert_eq!(probe.0.load(Ordering::Relaxed), 128); + } + /// Labels of two map layers that overlap (a class that shows all, a pinned label) are /// recorded in the placement's order, as the pixels draw them, so the one on top stays on /// top: each run of one layer's labels is marked as that layer, and its runs are one PDF diff --git a/crates/render/src/raster.rs b/crates/render/src/raster.rs index d2e3300486..15620a003a 100644 --- a/crates/render/src/raster.rs +++ b/crates/render/src/raster.rs @@ -232,7 +232,19 @@ pub(crate) fn export_in_place( place: Place, light: Lights, ) -> Result { - let deadline = web_time::Instant::now() + rustgis_data::raster::IN_PLACE_WAIT; + export_bands(pm, style, stats, rustgis_data::raster::IN_PLACE_WAIT, |pm, style, stats, band| { + draw_in_place_region(pm, address, r, style, stats, view, opacity, true, place, light, Some(band)) + }) +} + +#[cfg(not(target_arch = "wasm32"))] +fn export_bands( + pm: &mut Pixmap, + style: &RasterStyle, + stats: Option<&[BandStats]>, + wait_budget: web_time::Duration, + mut draw: impl FnMut(&mut Pixmap, &RasterStyle, Option<&[BandStats]>, &OutputBand<'_>) -> Result, +) -> Result { let samples = std::cell::RefCell::new(ViewStats::default()); let mut fixed = style.clone(); fixed.dra = false; @@ -247,6 +259,7 @@ pub(crate) fn export_in_place( while first < height { let mut end = (first + 128).min(height); let mut idle = 0; + let mut wait_left = wait_budget; loop { rustgis_data::progress::check().map_err(|e| e.to_string())?; if crate::cancel::requested() { @@ -254,7 +267,7 @@ pub(crate) fn export_in_place( } let band = OutputBand { rows: (first, end), samples: collect.then_some(&samples) }; let generation = rustgis_tiles::cache::generation(); - match draw_in_place_region(pm, address, r, &fixed, stats, view, opacity, true, place, light, Some(&band)) { + match draw(pm, &fixed, stats, &band) { Ok(false) => break, Err(e) if (e == TOO_LARGE || e == GENERALISE_CUT) && end - first > 1 => { end = first + (end - first).div_ceil(2); @@ -263,13 +276,14 @@ pub(crate) fn export_in_place( Err(e) => return Err(e), Ok(true) => {} } - let left = deadline.saturating_duration_since(web_time::Instant::now()); - if left.is_zero() || idle >= 6 { + if wait_left.is_zero() || idle >= 6 { return Err("its tiles didn't all arrive before the export stopped waiting".into()); } - rustgis_tiles::cache::wait_for_news_unless(generation, left.min(web_time::Duration::from_secs(30)), || { + let waiting = web_time::Instant::now(); + rustgis_tiles::cache::wait_for_news_unless(generation, wait_left.min(web_time::Duration::from_secs(30)), || { crate::cancel::requested() || rustgis_data::progress::check().is_err() }); + wait_left = wait_left.saturating_sub(waiting.elapsed()); if generation == rustgis_tiles::cache::generation() { idle += 1; } else { @@ -399,7 +413,11 @@ fn draw_in_place_region( whole(pm); return Ok(false); }; - if let Some(band) = band { + // Generalised relief retains the whole read window: its smoothing lattice, + // block size and read stride depend on that window and the whole sampling grid. + // Until those can be shared across clipped windows, refuse an oversized window + // rather than changing the relief at band boundaries. + if let Some(band) = band.filter(|_| generalisation(&style.renderer).is_none()) { let Some(clipped) = in_view_rows(r, view, place, pm.width(), pm.height(), full_res, band.rows) else { return Ok(false) }; // Pick the level with the whole view; only its read window is clipped. want = InView { per_px: want.per_px, ..clipped }; @@ -764,7 +782,8 @@ fn draw_region( clip((ay.max(by) * ppp).ceil(), h) as usize, ) }; - let (y0, y1) = band.map_or((y0, y1), |b| (y0.max(b.rows.0), y1.min(b.rows.1))); + let whole_rows = (y0, y1); + let (y0, y1) = band.map_or(whole_rows, |b| (y0.max(b.rows.0), y1.min(b.rows.1))); if x0 >= x1 || y0 >= y1 { return; } @@ -775,8 +794,10 @@ fn draw_region( // out alone, so the grid is the same on any number. // Exports anchor their interpolation grid to the output, including when a // read window or an output band starts between its grid lines. - let (gx0, gy0) = if full_res { (x0 / STEP * STEP, y0 / STEP * STEP) } else { (x0, y0) }; - let (gw, gh) = ((x1 - gx0) / STEP + 2, (y1 - gy0) / STEP + 2); + // Smoothing chooses its block size and sampling stride from the whole grid. + let (grid_y0, grid_y1) = if generalisation(&style.renderer).is_some() { whole_rows } else { (y0, y1) }; + let (gx0, gy0) = if full_res { (x0 / STEP * STEP, grid_y0 / STEP * STEP) } else { (x0, grid_y0) }; + let (gw, gh) = ((x1 - gx0) / STEP + 2, (grid_y1 - gy0) / STEP + 2); let mut grid = vec![None; gw * gh]; // (A few points aren't worth a thread.) let grid_threads = if gw * gh < 1024 { 1 } else { threads.clamp(1, gh) }; @@ -1953,6 +1974,116 @@ mod tests { assert!(image.pixels().iter().skip(128 * 300).all(|p| p.alpha() == 0)); } + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn output_bands_keep_the_whole_generalised_relief() { + let r = + crate::testing::grid(Crs::WebMercator, (-2300.0, 2100.0), 2.0, (2300, 2100), |x, y| (x * 0.03 + y * 0.07).sin() * 500.0 + y) + .unwrap(); + let view = + View { width: 317.0, height: 389.0, pixels_per_point: 1.0, camera: Camera::new(Coord::new(17.3, 23.7), 16.3), sky: false }; + let place = Place::Flat(&Flat::Mercator, "EPSG:3857"); + for metres in [8.0, 600.0] { + let mut renderer = grey_relief(315.0, 45.0, 1.0, 0.0, 0.0); + if let RasterRenderer::ShadedRelief { generalize, .. } = &mut renderer { + *generalize = metres; + } + let style = RasterStyle { renderer, ..RasterStyle::default_for(&r) }; + let mut whole = Pixmap::new(317, 389).unwrap(); + let mut bands = whole.clone(); + draw(&mut whole, &r, &style, None, &view, 1.0, true, place, Lights::default()); + export_in_place(&mut bands, "unused", &r, &style, None, &view, 1.0, place, Lights::default()).unwrap(); + let max = whole.data().iter().zip(bands.data()).map(|(a, b)| a.abs_diff(*b)).max().unwrap(); + assert_eq!(max, 0, "generalisation {metres}"); + assert!(bands.pixels().iter().any(|p| p.alpha() > 0)); + } + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn output_bands_keep_in_place_generalised_relief() { + use rustgis_data::raster::cog::{self, Example}; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("relief.tif"); + std::fs::write(&path, cog::example_with(&Example { size: 4096, tile: 128, ..Example::default() })).unwrap(); + let address = path.to_str().unwrap(); + let r = rustgis_tiles::cog::raster::overview_blocking(address).unwrap(); + let view = View { + width: 317.0, + height: 389.0, + pixels_per_point: 1.0, + camera: Camera::new(lonlat_to_merc(Coord::new(10.47, 45.4)), 60.0), + sky: false, + }; + let place = Place::Flat(&Flat::Mercator, "EPSG:3857"); + // Make a shifted lattice visible even after smoothing the byte-valued fixture. + let mut renderer = grey_relief(315.0, 45.0, 100_000.0, 0.0, 0.0); + if let RasterRenderer::ShadedRelief { generalize, .. } = &mut renderer { + *generalize = 8_000.0; + } + let style = RasterStyle { renderer, ..RasterStyle::default_for(&r) }; + let mut bands = Pixmap::new(317, 389).unwrap(); + export_in_place(&mut bands, address, &r, &style, None, &view, 1.0, place, Lights::default()).unwrap(); + let mut whole = Pixmap::new(317, 389).unwrap(); + assert!(!draw_in_place(&mut whole, address, &r, &style, None, &view, 1.0, true, place, Lights::default()).unwrap()); + let max = whole.data().iter().zip(bands.data()).map(|(a, b)| a.abs_diff(*b)).max().unwrap(); + assert_eq!(max, 0); + assert!(bands.pixels().iter().any(|p| p.alpha() > 0)); + rustgis_tiles::cog::forget_local_file(address); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn output_bands_give_later_tiles_their_own_wait_budget() { + use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + }; + struct SlowDraw(AtomicBool); + impl rustgis_data::progress::Probe for SlowDraw { + fn cancelled(&self) -> bool { + false + } + fn begin(&self, _: u64) {} + fn advance(&self, _: u64) { + if !self.0.swap(true, Ordering::Relaxed) { + std::thread::sleep(web_time::Duration::from_millis(40)); // test-wait: simulate a slow draw + } + } + } + let r = crate::testing::grid(Crs::WebMercator, (-150.0, 150.0), 10.0, (30, 30), |_, _| 50.0).unwrap(); + let view = View { width: 300.0, height: 300.0, pixels_per_point: 1.0, camera: Camera::new(Coord::new(0.0, 0.0), 1.0), sky: false }; + let probe = Arc::new(SlowDraw(AtomicBool::new(false))); + let _installed = rustgis_data::progress::install(Some(probe.clone())); + let mut image = Pixmap::new(300, 300).unwrap(); + let mut pending = false; + export_bands(&mut image, &RasterStyle::default_for(&r), None, web_time::Duration::from_millis(20), |pm, style, stats, band| { + // A later band's first read is pending; its retry completes. The real + // export loop must allow it to wait even though the first band was slow. + if band.rows.0 == 128 && !pending { + pending = true; + return Ok(true); + } + draw_in_place_region( + pm, + "unused", + &r, + style, + stats, + &view, + 1.0, + true, + Place::Flat(&Flat::Mercator, "EPSG:3857"), + Lights::default(), + Some(band), + ) + }) + .unwrap(); + assert!(pending); + assert!(probe.0.load(Ordering::Relaxed)); + assert!(image.pixels().iter().skip(256 * 300).any(|p| p.alpha() > 0)); + } + /// A Shaded Relief without a ramp or rock, lit from `azimuth` and `altitude`. fn grey_relief(azimuth: f64, altitude: f64, z_factor: f64, shadow: f64, sky: f64) -> RasterRenderer { RasterRenderer::ShadedRelief { From dbbbef3e58b1679ef9538f5a230c963ac989d027 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 3 Dec 2026 00:04:56 -0600 Subject: [PATCH 3/4] fix(render): reject incomplete banded exports and relief seams --- crates/render/src/lib.rs | 73 +++++++++++++++++++++++++++++++++---- crates/render/src/raster.rs | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 8 deletions(-) diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index ad461776ea..794b412150 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -9949,22 +9949,31 @@ fn complete_passes(map: &mut Map, view: &View, record: bool, failed_source: &mut pub fn render_drawing(map: &mut Map, view: &View) -> Result { let revisions = archive_revisions([&*map]); let mut failed_source = None; - let result = render_drawing_inner(map, view, &mut failed_source); - revisions.finish(map, failed_source, result, "could not draw the map") + match render_drawing_inner(map, view, &mut failed_source) { + Ok(drawing) => revisions.finish(map, failed_source, Some(drawing), "could not draw the map"), + Err(why) => revisions.finish(map, failed_source, None, &why), + } } /// Completes the recording, retaining a fatal source's identity through the pixel fallback. -fn render_drawing_inner(map: &mut Map, view: &View, failed_source: &mut Option) -> Option { - let r = render_complete_recorded(map, view, failed_source)?; - rustgis_data::progress::check().ok()?; +fn render_drawing_inner(map: &mut Map, view: &View, failed_source: &mut Option) -> Result { + let r = render_complete_recorded(map, view, failed_source).ok_or_else(|| "could not draw the map".to_string())?; + rustgis_data::progress::check().map_err(|e| e.to_string())?; if cancel::requested() { - return None; + return Err(rustgis_data::DataError::Cancelled.to_string()); + } + if let Some((_, why)) = r.partial.first() { + return Err(format!("{why}, so the drawing isn't written")); + } + if let Some(l) = r.pending_in_place.first().and_then(|id| map.layers.iter().find(|l| l.id == *id)) { + return Err(format!("{}: its tiles are still on their way: export again once the map has drawn it", l.name)); } let mut pm = match r.recorded { Some(c) => c, None => { // Not recorded: its pixels, placed on a recording. - let mut c = Canvas::recording(r.pixmap.width(), r.pixmap.height())?; + let mut c = + Canvas::recording(r.pixmap.width(), r.pixmap.height()).ok_or_else(|| "could not allocate the drawing".to_string())?; c.draw_pixmap(0, 0, r.pixmap.as_ref(), &PixmapPaint::default(), Transform::identity(), None); c } @@ -9972,7 +9981,7 @@ fn render_drawing_inner(map: &mut Map, view: &View, failed_source: &mut Option bool { + false + } + fn begin(&self, _: u64) {} + fn advance(&self, n: u64) { + if self.rows.fetch_add(n, Ordering::Relaxed) == 0 { + std::fs::remove_file(&self.path).unwrap(); + rustgis_tiles::cog::forget_local_file(self.path.to_str().unwrap()); + } + } + } + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("fails-after-band.tif"); + std::fs::write(&path, cog::example_with(&Example { size: 4096, tile: 128, ..Example::default() })).unwrap(); + let r = rustgis_tiles::cog::raster::overview_blocking(path.to_str().unwrap()).unwrap(); + let mut layer = rustgis_map::RasterLayer::from_path(r, path.to_str().unwrap()); + layer.in_place = true; + let mut map = Map::new("Map"); + map.basemap = rustgis_map::Basemap::Blank; + map.add_raster("raster", layer); + let view = View { + width: 317.0, + height: 389.0, + pixels_per_point: 1.0, + camera: Camera::new(rustgis_geom::lonlat_to_merc(Coord::new(10.47, 45.4)), 60.0), + sky: false, + }; + assert!(render_drawing(&mut map, &view).is_ok()); + let probe = Arc::new(RemoveAfterBand { path, rows: AtomicU64::new(0) }); + let _installed = rustgis_data::progress::install(Some(probe.clone())); + let why = render_drawing(&mut map, &view).err().expect("an incomplete raster must fail the drawing"); + assert!(why.starts_with("raster: "), "{why}"); + assert_eq!(probe.rows.load(Ordering::Relaxed), 128); + } + /// Labels of two map layers that overlap (a class that shows all, a pinned label) are /// recorded in the placement's order, as the pixels draw them, so the one on top stays on /// top: each run of one layer's labels is marked as that layer, and its runs are one PDF diff --git a/crates/render/src/raster.rs b/crates/render/src/raster.rs index 15620a003a..3f86561eb9 100644 --- a/crates/render/src/raster.rs +++ b/crates/render/src/raster.rs @@ -297,6 +297,10 @@ fn export_bands( samples.borrow_mut().histogram(); } } + rustgis_data::progress::check().map_err(|e| e.to_string())?; + if crate::cancel::requested() { + return Err(rustgis_data::DataError::Cancelled.to_string()); + } Ok(false) } @@ -445,6 +449,7 @@ fn draw_in_place_region( // image's. let least = match style.renderer { RasterRenderer::ShadedRelief { flats: Some(f), .. } if f.amount > 0.0 => f.size.min(Flats::MAX_SIZE) + 1, + RasterRenderer::ShadedRelief { .. } if generalisation(&style.renderer).is_none() => 2, _ => 1, }; let pad = |l: &rustgis_data::raster::cog::Level| { @@ -2084,6 +2089,71 @@ mod tests { assert!(image.pixels().iter().skip(256 * 300).any(|p| p.alpha() > 0)); } + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn output_bands_check_cancellation_after_the_final_band() { + use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + }; + struct LastBand(AtomicBool); + impl rustgis_data::progress::Probe for LastBand { + fn cancelled(&self) -> bool { + self.0.load(Ordering::Relaxed) + } + fn begin(&self, _: u64) {} + fn advance(&self, _: u64) { + self.0.store(true, Ordering::Relaxed); + } + } + let probe = Arc::new(LastBand(AtomicBool::new(false))); + let _installed = rustgis_data::progress::install(Some(probe.clone())); + let mut pm = Pixmap::new(32, 128).unwrap(); + let result = export_bands( + &mut pm, + &RasterStyle::default_for(&crate::testing::grid(Crs::WebMercator, (0.0, 1.0), 1.0, (1, 1), |_, _| 0.0).unwrap()), + None, + web_time::Duration::ZERO, + |_, _, _, band| { + assert_eq!(band.rows, (0, 128)); + Ok(false) + }, + ); + assert_eq!(result, Err(rustgis_data::DataError::Cancelled.to_string())); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn output_bands_keep_in_place_plain_relief() { + use rustgis_data::raster::cog::{self, Example}; + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("plain-relief.tif"); + std::fs::write(&path, cog::example_with(&Example { size: 4096, tile: 128, ..Example::default() })).unwrap(); + let address = path.to_str().unwrap(); + let r = rustgis_tiles::cog::raster::overview_blocking(address).unwrap(); + let view = View { + width: 317.0, + height: 389.0, + pixels_per_point: 1.0, + camera: Camera::new(lonlat_to_merc(Coord::new(10.47, 45.4)), 10.3), + sky: false, + }; + let place = Place::Flat(&Flat::Mercator, "EPSG:3857"); + let style = RasterStyle { + renderer: grey_relief(315.0, 45.0, 100.0, 0.0, 0.0), + resampling: Resampling::Bilinear, + ..RasterStyle::default_for(&r) + }; + let mut bands = Pixmap::new(317, 389).unwrap(); + export_in_place(&mut bands, address, &r, &style, None, &view, 1.0, place, Lights::default()).unwrap(); + let mut whole = Pixmap::new(317, 389).unwrap(); + assert!(!draw_in_place(&mut whole, address, &r, &style, None, &view, 1.0, true, place, Lights::default()).unwrap()); + let max = whole.data().iter().zip(bands.data()).map(|(a, b)| a.abs_diff(*b)).max().unwrap(); + assert_eq!(max, 0); + assert!(bands.pixels().iter().any(|p| p.alpha() > 0)); + rustgis_tiles::cog::forget_local_file(address); + } + /// A Shaded Relief without a ramp or rock, lit from `azimuth` and `altitude`. fn grey_relief(azimuth: f64, altitude: f64, z_factor: f64, shadow: f64, sky: f64) -> RasterRenderer { RasterRenderer::ShadedRelief { From 720fa008ed1ac6400470e46ee7fd80aac759450b Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 3 Dec 2026 01:15:19 -0600 Subject: [PATCH 4/4] test(render): require incomplete map PDFs to fail --- crates/render/src/layout/tests.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/render/src/layout/tests.rs b/crates/render/src/layout/tests.rs index 1d047805fe..34a2c4d464 100644 --- a/crates/render/src/layout/tests.rs +++ b/crates/render/src/layout/tests.rs @@ -3304,10 +3304,9 @@ fn a_refused_export_says_the_refused_layers_own_reason() { assert_eq!(refused, "dem: dem.tif: only http:// and https:// addresses are fetched, so the image isn't written"); } -/// A map PDF says a raster read in place that is drawn from less than it needs: unlike a PNG or a -/// GeoTIFF, it doesn't refuse one. +/// A map PDF refuses an incomplete raster, preserving its reason and its drawing notice. #[test] -fn a_map_pdf_says_a_raster_read_in_place_drawn_from_less() { +fn a_map_pdf_refuses_a_raster_read_in_place_drawn_from_less() { let mut map = ramp_map(rustgis_map::raster::RasterRenderer::Rgb { red: 0, green: 0, blue: 0 }, rustgis_map::raster::Stretch::MinMax); let rl = map.layers.iter_mut().find_map(|l| l.raster.as_mut()).unwrap(); rl.in_place = true; @@ -3317,7 +3316,7 @@ fn a_map_pdf_says_a_raster_read_in_place_drawn_from_less() { let camera = rustgis_map::Camera { center: rustgis_geom::lonlat_to_merc(Coord::new(0.5, 0.995)), res: 100.0, bearing: 0.0, pitch: 0.0 }; let view = crate::View { width: 64.0, height: 64.0, pixels_per_point: 1.0, camera, sky: false }; let (pdf, said) = crate::shadows_said(|| crate::render_drawing(&mut map, &view)); - assert!(pdf.is_ok()); + assert_eq!(pdf.err().unwrap(), "dem: dem.tif: only http:// and https:// addresses are fetched, so the drawing isn't written"); let undrawn = "a layer couldn't be drawn in full, so parts of it may be empty or coarse: dem: dem.tif: "; assert_eq!(said.notices.iter().filter(|n| n.starts_with(undrawn)).count(), 1, "{:?}", said.notices); }