From a1a6739135cca160115bce6715efcb1d8d4815e0 Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 23 Nov 2026 11:46:45 -0600 Subject: [PATCH 1/6] perf(render): read a streamed Shapefile's view in its own units on a map in its system (#2788) A Shapefile in a projected system read every record on every frame whatever the map. On a map drawn in the file's own system (a UTM file on a UTM map, a Web Mercator file on Web Mercator), the map draws its geometry as it is, so the draw pass now asks for the cull extent itself, in the file's units (Request::own): the index narrows by it and each shape's own box is tested against it, converting nothing. A new FeatureSource::visit_request_chunks carries the whole request to a complete visit; a source that doesn't read an own box visits everything when given one. --- crates/data/src/source/mod.rs | 8 ++ crates/data/src/source/shp.rs | 11 ++- crates/render/src/lib.rs | 87 ++++++++++++-------- crates/render/src/stream_tests.rs | 127 ++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+), 35 deletions(-) diff --git a/crates/data/src/source/mod.rs b/crates/data/src/source/mod.rs index 5ad9697f1f..3d52ffa75c 100644 --- a/crates/data/src/source/mod.rs +++ b/crates/data/src/source/mod.rs @@ -419,6 +419,14 @@ pub trait FeatureSource: Send + Sync { fn visit_chunks(&self, _extent: Option, _bytes: usize, _visit: &mut dyn FnMut(Vec) -> Result<()>) -> Result { Err(DataError::Unsupported("a source that can't be read a chunk at a time".into())) } + /// [`FeatureSource::visit_chunks`] for a whole request, its box in the source's own system + /// ([`Request::own`]) too: a source that reads one this way tests each feature's own box + /// against it, converting nothing. One that doesn't visits every feature when given such a + /// box, never fewer than it holds (its extent, in longitude/latitude, may hold less), and + /// reads the extent otherwise. + fn visit_request_chunks(&self, req: &Request, bytes: usize, visit: &mut dyn FnMut(Vec) -> Result<()>) -> Result { + self.visit_chunks(if req.own.is_some() { None } else { req.extent }, bytes, visit) + } fn info(&self) -> &SourceInfo; /// The coordinate system of the features it gives: a file read in place gives them as the file /// holds them, in its own system (Shapefile, GeoPackage, FlatGeobuf, GeoParquet, GeoJSON); a diff --git a/crates/data/src/source/shp.rs b/crates/data/src/source/shp.rs index b5851c38f5..76fd08c18a 100644 --- a/crates/data/src/source/shp.rs +++ b/crates/data/src/source/shp.rs @@ -425,10 +425,15 @@ impl FeatureSource for ShpSource { /// Visits located shapes in a spatial request; geometry-less rows belong only to whole reads. /// Counts scanned records and checks Cancel before filtering them. fn visit_chunks(&self, extent: Option, bytes: usize, visit: &mut dyn FnMut(Vec) -> Result<()>) -> Result { + self.visit_request_chunks(&Request { extent, limit: usize::MAX, own: None }, bytes, visit) + } + /// As [`FeatureSource::visit_chunks`], narrowed by the request's box in the file's own system + /// when it gives one ([`Request::own`]), and each shape's own box tested against it. + fn visit_request_chunks(&self, req: &Request, bytes: usize, visit: &mut dyn FnMut(Vec) -> Result<()>) -> Result { let encoding = self.encoding.decoder(); let mut chunks = super::chunk_memory::Chunker::new(bytes, visit)?; - let req = Request { extent, limit: usize::MAX, own: None }; - let extent = extent.and_then(|e| self.reproject.extent_from_lonlat(&e)); + let req = Request { limit: usize::MAX, ..*req }; + let extent = self.reproject.request_box(&req); if extent.is_some_and(|e| e.is_empty()) { return Ok(Batch { complete: true, ..Batch::default() }); } @@ -438,7 +443,7 @@ impl FeatureSource for ShpSource { // As in `read`, an extent the file's system couldn't bound (#1925) visits every record. let late = req.extent.is_some() && bounds.is_none(); // A whole read looks at whether the stored boxes hold their shapes, once. - let checking = req.extent.is_none() && self.boxes.load(Relaxed) == BOXES_UNKNOWN; + let checking = req.extent.is_none() && req.own.is_none() && self.boxes.load(Relaxed) == BOXES_UNKNOWN; let mut missed = false; // A record a whole read couldn't read this time had its box left unchecked: the check // waits for a read that reads them all. diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 856104e762..d47dfb8de3 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -2566,14 +2566,17 @@ fn visit_complete( visit_within(source, None, visit) } -/// As [`visit_complete`], for the features whose boxes meet `extent` (longitude/latitude), or -/// every feature for `None`. +/// As [`visit_complete`], for the features a request gives (those whose boxes meet its extent, +/// or its box in the source's own system), or every feature for `None`. fn visit_within( source: &dyn rustgis_data::source::FeatureSource, - extent: Option, + request: Option, visit: &mut dyn FnMut(Vec) -> rustgis_data::Result<()>, ) -> rustgis_data::Result { - let result = source.visit_chunks(extent, COMPLETE_CHUNK_BYTES, visit)?; + let result = match request { + Some(req) => source.visit_request_chunks(&req, COMPLETE_CHUNK_BYTES, visit)?, + None => source.visit_chunks(None, COMPLETE_CHUNK_BYTES, visit)?, + }; if !result.complete { return Err(rustgis_data::DataError::Format("source traversal was incomplete".into())); } @@ -6436,29 +6439,53 @@ fn render_frame( chunk.clear_cache(); chunk.dataset.features = Vec::new().into(); // Only the features near the view are read when nothing the layer draws comes from - // beyond them (#1065): those whose boxes meet the cull extent's box in - // longitude/latitude. Otherwise every feature: labels, shadows and other effects reach - // in from beyond the view. Web Mercator maps longitude and latitude each on its own, - // so a feature held in longitude/latitude whose box misses that box is drawn wholly - // outside the cull extent, straight edges and all, short of the latitudes it clamps - // (a view reaching them reads everything). In another projection, or from another - // system, an edge drawn straight between two places can cross the view while the - // feature's box misses it, so those read everything. So does a source whose reads by - // area leave some features out (a Shapefile's records without a usable box), and a GPU - // frame, whose meshes are made a batch at a time: which features share a batch decides - // which fall back to the CPU. And a box over most of the layer reads it all, in file - // order: asking the index which records nearly all of them are costs more than - // reading the few left over. - let partial = |e: &Extent| !covers_most(e, &source.info().extent); + // beyond them (#1065): those whose boxes meet the cull extent. Otherwise every + // feature: labels, shadows and other effects reach in from beyond the view. The box + // must hold every feature drawn in the cull extent, straight edges and all: + // - on a map drawn in the source's own system (a UTM file on a UTM map, a Web Mercator + // file on Web Mercator), the cull extent itself, in the file's units (#2788): the + // map draws its geometry as it is; + // - on Web Mercator, for a source in longitude/latitude, the cull extent's box in + // longitude/latitude, short of the latitudes Web Mercator clamps (a view reaching + // them reads everything): Web Mercator maps longitude and latitude each on its own, + // so a feature whose box misses that box is drawn wholly outside the cull extent. + // In another projection, or from another system, an edge drawn straight between two + // places can cross the view while the feature's box misses it, so those read + // everything. So does a source whose reads by area leave some features out (a + // Shapefile's records without a usable box), and a GPU frame, whose meshes are made a + // batch at a time: which features share a batch decides which fall back to the CPU. + // And a box over most of the layer reads it all, in file order: asking the index which + // records nearly all of them are costs more than reading the few left over. + let crs = source.coord_crs(); + let own = + crs != rustgis_data::raster::Crs::LonLat && source.reads_own_box() && flat.held(crs).is_ok_and(|held| held.is_identity()); + let lonlat = !own && crs == rustgis_data::raster::Crs::LonLat && matches!(flat, rustgis_map::projection::Flat::Mercator); + // The box a read of the map extent `e` asks for, in its path's units. + let box_of = |e: &Extent| { + if own { + Some(*e) + } else if lonlat && within_mercator_lats(e) { + lonlat_box(&flat, e) + } else { + None + } + }; + let partial = |b: &Extent| { + let ll = if own { lonlat_box(&flat, b) } else { Some(*b) }; + ll.is_none_or(|ll| !covers_most(&ll, &source.info().extent)) + }; + // A box as a read asks for it. + let ask = |b: Extent| rustgis_data::source::Request { + extent: if own { lonlat_box(&flat, &b) } else { Some(b) }, + limit: usize::MAX, + own: own.then_some((crs, b)), + }; let near = (globe.is_none() && !gpu - && matches!(flat, rustgis_map::projection::Flat::Mercator) - && within_mercator_lats(&ctx.cull) - && source.coord_crs() == rustgis_data::raster::Crs::LonLat && source.finds_all_by_area() && matches!(labels_to, Labels::Own | Labels::Turned(_)) && draws_in_view_only(&chunk, !display.paused, shore.is_some())) - .then(|| lonlat_box(&flat, &ctx.cull)) + .then(|| box_of(&ctx.cull)) .flatten() .filter(partial); // The source's long loops that only look at Cancel see the frame's stop too. @@ -6485,18 +6512,14 @@ fn render_frame( }; let owner = layer.source.as_ref(); let grids = rustgis_data::raster::crs::grid_generation(); - let kept_key = format!("{key} {grids}"); + let kept_key = format!("{key} {grids} {}", if own { "own" } else { "lonlat" }); let room = owner.map_or(0, |o| o.budget().saturating_sub(layer.view_cache_bytes()).min(rustgis_map::source::KEPT_VIEW_BYTES)); let kept = near.filter(|_| unchanged).and_then(|n| owner?.kept_view(&kept_key, &n, room)); let wide = cull_extent(view, KEEP_MARGIN); - let keep = (near.is_some() - && unchanged - && kept.is_none() - && within_mercator_lats(&wide) - && owner.is_some_and(|o| o.view_worth_keeping(&kept_key, res))) - .then(|| lonlat_box(&flat, &wide)) - .flatten() - .filter(partial); + let keep = (near.is_some() && unchanged && kept.is_none() && owner.is_some_and(|o| o.view_worth_keeping(&kept_key, res))) + .then(|| box_of(&wide)) + .flatten() + .filter(partial); let mut keeping: Option<(Vec, usize)> = (keep.is_some() || kept.is_some()).then(|| (Vec::new(), 0)); let mut draw_rows = |chunk: &mut rustgis_map::Layer| -> rustgis_data::Result<()> { chunk.selection = chunk @@ -6528,7 +6551,7 @@ fn render_frame( chunk.draw_rows_of(rows); draw_rows(&mut chunk) }), - None => visit_within(source.as_ref(), keep.or(near), &mut |features| { + None => visit_within(source.as_ref(), keep.or(near).map(ask), &mut |features| { // A frame no longer wanted reads no further (see `cancel`). if cancel::requested() { return Err(rustgis_data::DataError::Cancelled); diff --git a/crates/render/src/stream_tests.rs b/crates/render/src/stream_tests.rs index c1eef6f042..1370de011f 100644 --- a/crates/render/src/stream_tests.rs +++ b/crates/render/src/stream_tests.rs @@ -1975,3 +1975,130 @@ fn a_kept_view_draws_the_frames_inside_it_without_reading_the_source() { assert_eq!(frame(&mut base, &mut actual, &view, "empty"), [true], "an empty view is read"); assert_eq!(frame(&mut base, &mut actual, &view, "empty again"), [false; 0], "and kept, with nothing in it"); } + +/// A source that keeps each request a complete read asks, and answers as the one it wraps. +struct Asked { + inner: Box, + asked: std::sync::Arc>>>, +} + +impl FeatureSource for Asked { + fn info(&self) -> &SourceInfo { + self.inner.info() + } + fn read(&self, r: &Request) -> rustgis_data::Result { + self.inner.read(r) + } + fn coord_crs(&self) -> rustgis_data::raster::Crs { + self.inner.coord_crs() + } + fn reads_own_box(&self) -> bool { + self.inner.reads_own_box() + } + fn streams_chunks(&self) -> bool { + self.inner.streams_chunks() + } + fn finds_all_by_area(&self) -> bool { + self.inner.finds_all_by_area() + } + fn same_as_opened(&self) -> rustgis_data::Result { + self.inner.same_as_opened() + } + fn visit_chunks( + &self, + extent: Option, + bytes: usize, + visit: &mut dyn FnMut(Vec) -> rustgis_data::Result<()>, + ) -> rustgis_data::Result { + self.asked.lock().unwrap().push(extent.map(|extent| Request { extent: Some(extent), limit: usize::MAX, own: None })); + self.inner.visit_chunks(extent, bytes, visit) + } + /// Kept as asked, then answered by the wrapped source. + fn visit_request_chunks( + &self, + req: &Request, + bytes: usize, + visit: &mut dyn FnMut(Vec) -> rustgis_data::Result<()>, + ) -> rustgis_data::Result { + self.asked.lock().unwrap().push(Some(*req)); + self.inner.visit_request_chunks(req, bytes, visit) + } +} + +/// A projected Shapefile on a map drawn in its own system reads only the view, by the view's box +/// in the file's units, and draws what reading the whole file drew (#2788); on a map in another +/// system it reads everything. A Web Mercator file on Web Mercator does the same. +#[test] +fn a_shapefile_on_a_map_in_its_own_system_reads_the_view_in_its_units() { + use rustgis_data::raster::Crs; + use std::sync::{Arc, Mutex}; + rustgis_data::source::shp::use_temporary_index_dir(); + let dir = tempfile::tempdir().unwrap(); + for (crs, projection, own) in [ + (Crs::from_epsg(32631).unwrap(), rustgis_map::Projection::Custom("EPSG:32631".into()), true), + (Crs::from_epsg(32631).unwrap(), rustgis_map::Projection::default(), false), + (Crs::WebMercator, rustgis_map::Projection::default(), true), + ] { + // Squares 600 m wide, 1 km apart, over 40 km around Paris, in the file's own units. + let (x0, y0) = if crs == Crs::WebMercator { (250_000.0, 6_230_000.0) } else { (440_000.0, 5_400_000.0) }; + let mut ds = Dataset::new("squares", GeometryType::Polygon, vec![Field::new("name", FieldType::Text)]); + for i in 0..40 { + for j in 0..40 { + let (x, y) = (x0 + f64::from(i) * 1000.0, y0 + f64::from(j) * 1000.0); + let ring = vec![ + Coord::new(x, y), + Coord::new(x + 600.0, y), + Coord::new(x + 600.0, y + 600.0), + Coord::new(x, y + 600.0), + Coord::new(x, y), + ]; + ds.push(Some(Geometry::Polygon(vec![vec![ring]])), vec![Value::Text(format!("s{i}-{j}"))]); + } + } + ds.held_in = crs; + let path = dir.path().join(format!("squares-{}.shp", crs.label().replace([':', ' ', '/'], "_"))); + rustgis_data::io::write_path_in(&ds, &path, crs).unwrap(); + let mut base = Map::new("own"); + base.basemap = rustgis_map::Basemap::Blank; + base.projection = projection.clone(); + let opened = rustgis_data::source::shp::ShpSource::open_path(&path, "squares").unwrap(); + let mut held = ds.clone(); + held.fields = opened.info().fields.clone(); + base.add_dataset(held); + let asked = Arc::new(Mutex::new(Vec::new())); + let mut actual = base.clone(); + let mut layer = streamed(&base.layers[0], usize::MAX); + layer.source = + Some(SourceLayer::from_path(path.to_string_lossy(), None, Box::new(Asked { inner: Box::new(opened), asked: asked.clone() }))); + actual.layers[0] = layer; + // A view of a few squares in the middle of the grid. + let middle = if crs == Crs::WebMercator { + Coord::new(x0 + 20_300.0, y0 + 20_300.0) + } else { + rustgis_geom::lonlat_to_merc(Coord::new(2.35, 48.85)) + }; + let center = if own && crs != Crs::WebMercator { Coord::new(x0 + 20_300.0, y0 + 20_300.0) } else { middle }; + let view = View { width: 320.0, height: 240.0, pixels_per_point: 1.0, camera: Camera::new(center, 15.0), sky: false }; + let what = format!("{} on {projection:?}", crs.label()); + // The first frame reads the whole file (its stored boxes checked); the ones after only + // the view, and draw what it drew. + let mut whole = None; + for frame in 0..3 { + let Some(RenderOutput::Cpu(got)) = render_with_backend(&mut actual.clone(), &view, true, Backend::Cpu) else { + panic!("{what}: a complete frame"); + }; + let whole = whole.get_or_insert_with(|| got.pixmap.data().to_vec()); + assert!(got.pixmap.data() == whole.as_slice(), "{what}, frame {frame}: the pixels changed"); + } + let asked = asked.lock().unwrap(); + let by_box: Vec<&Request> = asked.iter().flatten().collect(); + if own { + let req = by_box.last().unwrap_or_else(|| panic!("{what}: never read by a box")); + let (asked_crs, b) = req.own.unwrap_or_else(|| panic!("{what}: read by longitude/latitude")); + assert_eq!(asked_crs, crs, "{what}"); + assert!(b.width() < 10_000.0 && b.intersects(&Extent::new(x0, y0, x0 + 40_000.0, y0 + 40_000.0)), "{what}: {b:?}"); + } else { + assert!(by_box.is_empty(), "{what}: a map in another system reads everything, not {by_box:?}"); + } + } +} From f4055bcd093f3db854b583cf59c6bfed1199d3e6 Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 23 Nov 2026 13:10:27 -0600 Subject: [PATCH 2/6] fix(data): a Shapefile read by its own box gives no rows without a shape; check the cheap conditions first (#2788) --- crates/data/src/source/shp.rs | 5 ++-- crates/data/src/source/shp/tests.rs | 39 +++++++++++++++++++++++++++++ crates/render/src/lib.rs | 20 +++++++-------- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/crates/data/src/source/shp.rs b/crates/data/src/source/shp.rs index 76fd08c18a..29bc580090 100644 --- a/crates/data/src/source/shp.rs +++ b/crates/data/src/source/shp.rs @@ -502,8 +502,9 @@ impl FeatureSource for ShpSource { return Ok(()); } }; - // As in `read`, the shape itself is checked. - if geometry.as_ref().map_or(req.extent.is_some(), |g| !g.inside) { + // As in `read`, the shape itself is checked; a read by area, its box in the file's + // own system included, gives no rows without a shape. + if geometry.as_ref().map_or(req.extent.is_some() || req.own.is_some(), |g| !g.inside) { return Ok(()); } let geometry = geometry.map(|p| p.geometry); diff --git a/crates/data/src/source/shp/tests.rs b/crates/data/src/source/shp/tests.rs index 4c9deea203..97d63a3637 100644 --- a/crates/data/src/source/shp/tests.rs +++ b/crates/data/src/source/shp/tests.rs @@ -917,3 +917,42 @@ fn a_shapefile_says_whether_it_is_as_it_was_opened() { assert!(s.same_as_opened().is_err(), "gone"); }); } + +/// A complete read by a box in the file's own system (#2788) gives the shapes meeting it and no +/// row without a shape, as a read by area in longitude/latitude does, also when it gives no +/// longitude/latitude extent beside the box. +#[test] +fn a_complete_read_by_its_own_box_leaves_out_rows_without_a_shape() { + let utm = crate::raster::Crs::from_epsg(32631).unwrap(); + let tmp = tempfile::tempdir().unwrap(); + let path = tmp.path().join("pts.shp"); + let mut ds = Dataset::new("pts", GeometryType::Point, vec![Field::new("n", FieldType::Integer)]); + for (i, p) in + [Some((450_000.0, 5_410_000.0)), None, Some((460_000.0, 5_420_000.0)), Some((900_000.0, 5_000_000.0))].into_iter().enumerate() + { + ds.push(p.map(|(x, y)| Geometry::Point(Coord::new(x, y))), vec![Value::Int(i as i64)]); + } + ds.held_in = utm; + crate::io::write_path_in(&ds, &path, utm).unwrap(); + cache::with_index_dir(tmp.path().join("cache"), || { + let s = ShpSource::open_path(&path, "pts").unwrap(); + let own = Extent::new(440_000.0, 5_400_000.0, 470_000.0, 5_430_000.0); + let mut got = Vec::new(); + s.visit_request_chunks(&Request { extent: None, limit: usize::MAX, own: Some((utm, own)) }, 4096, &mut |chunk| { + got.extend(chunk.iter().map(|f| f.id)); + Ok(()) + }) + .unwrap(); + // What a whole read gives, the features with a shape in the box. + let mut expected = Vec::new(); + let mut rows = 0; + s.visit_chunks(None, 4096, &mut |chunk| { + rows += chunk.len(); + expected.extend(chunk.iter().filter(|f| f.geometry.as_ref().is_some_and(|g| g.extent().intersects(&own))).map(|f| f.id)); + Ok(()) + }) + .unwrap(); + assert!(rows > expected.len() && expected.len() == 2, "{rows} rows, {expected:?} in the box"); + assert_eq!(got, expected, "the points in the box, not a row without a shape or the far point"); + }); +} diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index d47dfb8de3..50df26a98c 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -6457,8 +6457,15 @@ fn render_frame( // And a box over most of the layer reads it all, in file order: asking the index which // records nearly all of them are costs more than reading the few left over. let crs = source.coord_crs(); - let own = - crs != rustgis_data::raster::Crs::LonLat && source.reads_own_box() && flat.held(crs).is_ok_and(|held| held.is_identity()); + let view_only = globe.is_none() + && !gpu + && source.finds_all_by_area() + && matches!(labels_to, Labels::Own | Labels::Turned(_)) + && draws_in_view_only(&chunk, !display.paused, shore.is_some()); + let own = view_only + && crs != rustgis_data::raster::Crs::LonLat + && source.reads_own_box() + && flat.held(crs).is_ok_and(|held| held.is_identity()); let lonlat = !own && crs == rustgis_data::raster::Crs::LonLat && matches!(flat, rustgis_map::projection::Flat::Mercator); // The box a read of the map extent `e` asks for, in its path's units. let box_of = |e: &Extent| { @@ -6480,14 +6487,7 @@ fn render_frame( limit: usize::MAX, own: own.then_some((crs, b)), }; - let near = (globe.is_none() - && !gpu - && source.finds_all_by_area() - && matches!(labels_to, Labels::Own | Labels::Turned(_)) - && draws_in_view_only(&chunk, !display.paused, shore.is_some())) - .then(|| box_of(&ctx.cull)) - .flatten() - .filter(partial); + let near = view_only.then(|| box_of(&ctx.cull)).flatten().filter(partial); // The source's long loops that only look at Cancel see the frame's stop too. let _stop = cancel::current().map(rustgis_data::progress::also_stop_when); // A view read this way is kept with the geometry drawn from it, for the frames of the From 115ade74a700f02dc337ca8cbaa40c48e7fcbf55 Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 23 Nov 2026 14:19:05 -0600 Subject: [PATCH 3/6] fix(render): read every feature on a map in a Shapefile's own system as its view is read (#2788) A read of the view on a map in the file's own system keeps a shape that longitude/latitude can't place, as #2778 has it; a read of every feature dropped it, so the frames could differ. Such a read now asks with a box over the whole plane in the file's own system (source::whole_plane), read as a whole read: every record, the stored boxes checked. An own box in another system is read as if absent, rows without a shape included. reads_own_box's doc says it is about read(). --- crates/data/src/source/mod.rs | 16 ++++++++++++---- crates/data/src/source/shp.rs | 10 +++++++--- crates/data/src/source/shp/tests.rs | 24 +++++++++++++++++++++++- crates/render/src/lib.rs | 10 ++++++++-- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/crates/data/src/source/mod.rs b/crates/data/src/source/mod.rs index 3d52ffa75c..036fce4a58 100644 --- a/crates/data/src/source/mod.rs +++ b/crates/data/src/source/mod.rs @@ -233,6 +233,12 @@ impl Request { } } +/// A box over the whole plane: as [`Request::own`], every feature in the source's own system, +/// those longitude/latitude can't place included (a map in that system draws them as they are). +pub fn whole_plane() -> Extent { + Extent::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::INFINITY, f64::INFINITY) +} + /// Refuses an unknown count rather than reporting an unbounded view as empty. A zero-limit /// request does not start the full geometry scan needed to determine its matches. pub(crate) fn unbounded_count() -> DataError { @@ -420,7 +426,8 @@ pub trait FeatureSource: Send + Sync { Err(DataError::Unsupported("a source that can't be read a chunk at a time".into())) } /// [`FeatureSource::visit_chunks`] for a whole request, its box in the source's own system - /// ([`Request::own`]) too: a source that reads one this way tests each feature's own box + /// ([`Request::own`]) too (one over the whole plane, [`whole_plane`], reads every feature as a + /// map in that system draws them): a source that reads one this way tests each feature's own box /// against it, converting nothing. One that doesn't visits every feature when given such a /// box, never fewer than it holds (its extent, in longitude/latitude, may hold less), and /// reads the extent otherwise. @@ -437,9 +444,10 @@ pub trait FeatureSource: Send + Sync { crate::raster::Crs::LonLat } - /// Whether a read given its box in the source's own system ([`Request::own`]) tests its - /// features against that box (a file read in place, PostGIS), so a map in that system keeps - /// its window in map units for it. A source that reads by the longitude/latitude extent + /// Whether a read ([`FeatureSource::read`]) given its box in the source's own system + /// ([`Request::own`]) tests its features against that box (a file read in place, PostGIS), so + /// a map in that system keeps its window in map units for it. A complete visit does so only + /// where the source says so through [`FeatureSource::visit_request_chunks`]. A source that reads by the longitude/latitude extent /// whatever it is given (another database, a service, a converted copy) says no: its window is /// kept in longitude/latitude, as it reads. fn reads_own_box(&self) -> bool { diff --git a/crates/data/src/source/shp.rs b/crates/data/src/source/shp.rs index 29bc580090..a3231b0a9c 100644 --- a/crates/data/src/source/shp.rs +++ b/crates/data/src/source/shp.rs @@ -433,7 +433,8 @@ impl FeatureSource for ShpSource { let encoding = self.encoding.decoder(); let mut chunks = super::chunk_memory::Chunker::new(bytes, visit)?; let req = Request { limit: usize::MAX, ..*req }; - let extent = self.reproject.request_box(&req); + // A box over the whole plane narrows nothing: every record, those without a box too. + let extent = self.reproject.request_box(&req).filter(|e| *e != super::whole_plane()); if extent.is_some_and(|e| e.is_empty()) { return Ok(Batch { complete: true, ..Batch::default() }); } @@ -443,7 +444,10 @@ impl FeatureSource for ShpSource { // As in `read`, an extent the file's system couldn't bound (#1925) visits every record. let late = req.extent.is_some() && bounds.is_none(); // A whole read looks at whether the stored boxes hold their shapes, once. - let checking = req.extent.is_none() && req.own.is_none() && self.boxes.load(Relaxed) == BOXES_UNKNOWN; + // The request's box in the file's own system, when it is one (a box in another system + // is read as if absent). + let own = self.reproject.own_box(&req); + let checking = req.extent.is_none() && own.is_none_or(|e| e == super::whole_plane()) && self.boxes.load(Relaxed) == BOXES_UNKNOWN; let mut missed = false; // A record a whole read couldn't read this time had its box left unchecked: the check // waits for a read that reads them all. @@ -504,7 +508,7 @@ impl FeatureSource for ShpSource { }; // As in `read`, the shape itself is checked; a read by area, its box in the file's // own system included, gives no rows without a shape. - if geometry.as_ref().map_or(req.extent.is_some() || req.own.is_some(), |g| !g.inside) { + if geometry.as_ref().map_or(req.extent.is_some() || own.is_some_and(|e| e != super::whole_plane()), |g| !g.inside) { return Ok(()); } let geometry = geometry.map(|p| p.geometry); diff --git a/crates/data/src/source/shp/tests.rs b/crates/data/src/source/shp/tests.rs index 97d63a3637..6b0e6b5eba 100644 --- a/crates/data/src/source/shp/tests.rs +++ b/crates/data/src/source/shp/tests.rs @@ -918,6 +918,17 @@ fn a_shapefile_says_whether_it_is_as_it_was_opened() { }); } +/// The ids a complete read with this own box (no longitude/latitude extent) gives. +fn without_shape(s: &ShpSource, own: Option<(crate::raster::Crs, Extent)>) -> Vec { + let mut ids = Vec::new(); + s.visit_request_chunks(&Request { extent: None, limit: usize::MAX, own }, 4096, &mut |chunk| { + ids.extend(chunk.iter().map(|f| f.id)); + Ok(()) + }) + .unwrap(); + ids +} + /// A complete read by a box in the file's own system (#2788) gives the shapes meeting it and no /// row without a shape, as a read by area in longitude/latitude does, also when it gives no /// longitude/latitude extent beside the box. @@ -934,6 +945,11 @@ fn a_complete_read_by_its_own_box_leaves_out_rows_without_a_shape() { } ds.held_in = utm; crate::io::write_path_in(&ds, &path, utm).unwrap(); + // The first record made a shape of no geometry (its type, at 108, 0), as other writers give + // a feature without one. + let mut b = std::fs::read(&path).unwrap(); + b[108..112].copy_from_slice(&0i32.to_le_bytes()); + std::fs::write(&path, b).unwrap(); cache::with_index_dir(tmp.path().join("cache"), || { let s = ShpSource::open_path(&path, "pts").unwrap(); let own = Extent::new(440_000.0, 5_400_000.0, 470_000.0, 5_430_000.0); @@ -952,7 +968,13 @@ fn a_complete_read_by_its_own_box_leaves_out_rows_without_a_shape() { Ok(()) }) .unwrap(); - assert!(rows > expected.len() && expected.len() == 2, "{rows} rows, {expected:?} in the box"); + assert!(rows > expected.len() && !expected.is_empty(), "{rows} rows, {expected:?} in the box"); assert_eq!(got, expected, "the points in the box, not a row without a shape or the far point"); + assert!(without_shape(&s, None).contains(&1), "a whole read gives the row without a shape"); + // A box in another system is read as if absent: every row, the one without a shape too. + let lonlat = Some((crate::raster::Crs::LonLat, own)); + assert_eq!(without_shape(&s, lonlat), without_shape(&s, None), "a box in another system reads everything"); + // A box over the whole plane reads everything too, as a whole read does. + assert_eq!(without_shape(&s, Some((utm, super::super::whole_plane()))), without_shape(&s, None)); }); } diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 50df26a98c..aee86bb9fd 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -6462,10 +6462,14 @@ fn render_frame( && source.finds_all_by_area() && matches!(labels_to, Labels::Own | Labels::Turned(_)) && draws_in_view_only(&chunk, !display.paused, shore.is_some()); - let own = view_only + // On a map drawn in the file's own system. + let own_units = globe.is_none() && crs != rustgis_data::raster::Crs::LonLat && source.reads_own_box() && flat.held(crs).is_ok_and(|held| held.is_identity()); + let own = view_only && own_units; + let whole = + || rustgis_data::source::Request { extent: None, limit: usize::MAX, own: Some((crs, rustgis_data::source::whole_plane())) }; let lonlat = !own && crs == rustgis_data::raster::Crs::LonLat && matches!(flat, rustgis_map::projection::Flat::Mercator); // The box a read of the map extent `e` asks for, in its path's units. let box_of = |e: &Extent| { @@ -6551,7 +6555,9 @@ fn render_frame( chunk.draw_rows_of(rows); draw_rows(&mut chunk) }), - None => visit_within(source.as_ref(), keep.or(near).map(ask), &mut |features| { + // A read of every feature on a map in the file's own system takes those + // longitude/latitude can't place too, as a read of the view there does. + None => visit_within(source.as_ref(), keep.or(near).map(ask).or_else(|| own_units.then(whole)), &mut |features| { // A frame no longer wanted reads no further (see `cancel`). if cancel::requested() { return Err(rustgis_data::DataError::Cancelled); From d6677ad476d43456d812909da02aa11a4b54c8e8 Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 23 Nov 2026 14:58:25 -0600 Subject: [PATCH 4/6] fix(data): a box over the whole plane holds a shape with no place too (#2788) --- crates/data/src/source/mod.rs | 3 ++- crates/data/src/source/own_box_tests.rs | 15 +++++++++++++++ crates/data/src/source/shp.rs | 15 ++++++++------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/crates/data/src/source/mod.rs b/crates/data/src/source/mod.rs index 036fce4a58..436f60d922 100644 --- a/crates/data/src/source/mod.rs +++ b/crates/data/src/source/mod.rs @@ -1030,7 +1030,8 @@ impl Reproject { if !g.is_finite() { return None; } - let inside = g.extent().intersects(&own); + // A box over the whole plane holds every shape, one with no place (an empty part) too. + let inside = own == whole_plane() || g.extent().intersects(&own); return Some(Fitted { geometry: g, inside }); } let Placed { geometry, lonlat } = self.read(g)?; diff --git a/crates/data/src/source/own_box_tests.rs b/crates/data/src/source/own_box_tests.rs index b583dd7f2a..960eb24918 100644 --- a/crates/data/src/source/own_box_tests.rs +++ b/crates/data/src/source/own_box_tests.rs @@ -152,3 +152,18 @@ fn a_coordinate_that_isnt_a_number_is_left_out_in_its_own_system_too() { let clean = Geometry::Line(vec![vec![Coord::new(451_500.0, 5_411_500.0), Coord::new(452_000.0, 5_411_500.0)]]); assert!(r.place(clean, &req).unwrap().inside); } + +/// A box over the whole plane holds every shape, one with no place (a polygon of no parts) +/// included, as a read without a box keeps it (#2788). +#[test] +fn the_whole_plane_holds_a_shape_with_no_place() { + let utm = Crs::from_epsg(32631).unwrap(); + let reproject = Reproject::known(Some((utm, utm.transformer().unwrap()))); + let empty = Geometry::Polygon(Vec::new()); + let whole = Request { extent: None, limit: usize::MAX, own: Some((utm, whole_plane())) }; + let none = Request { extent: None, limit: usize::MAX, own: None }; + let small = Request { extent: None, limit: usize::MAX, own: Some((utm, Extent::new(0.0, 0.0, 1.0, 1.0))) }; + assert!(reproject.place(empty.clone(), &whole).is_some_and(|f| f.inside), "the whole plane"); + assert!(reproject.place(empty.clone(), &none).is_some_and(|f| f.inside), "no box"); + assert!(reproject.place(empty, &small).is_some_and(|f| !f.inside), "a box of somewhere"); +} diff --git a/crates/data/src/source/shp.rs b/crates/data/src/source/shp.rs index a3231b0a9c..12d67ac935 100644 --- a/crates/data/src/source/shp.rs +++ b/crates/data/src/source/shp.rs @@ -433,8 +433,12 @@ impl FeatureSource for ShpSource { let encoding = self.encoding.decoder(); let mut chunks = super::chunk_memory::Chunker::new(bytes, visit)?; let req = Request { limit: usize::MAX, ..*req }; - // A box over the whole plane narrows nothing: every record, those without a box too. - let extent = self.reproject.request_box(&req).filter(|e| *e != super::whole_plane()); + // The request's box in the file's own system, when it is one (a box in another system + // is read as if absent), and whether it is one over the whole plane, which narrows + // nothing: every record, those without a box too, as a whole read. + let own = self.reproject.own_box(&req); + let everywhere = own == Some(super::whole_plane()); + let extent = self.reproject.request_box(&req).filter(|_| !everywhere); if extent.is_some_and(|e| e.is_empty()) { return Ok(Batch { complete: true, ..Batch::default() }); } @@ -444,10 +448,7 @@ impl FeatureSource for ShpSource { // As in `read`, an extent the file's system couldn't bound (#1925) visits every record. let late = req.extent.is_some() && bounds.is_none(); // A whole read looks at whether the stored boxes hold their shapes, once. - // The request's box in the file's own system, when it is one (a box in another system - // is read as if absent). - let own = self.reproject.own_box(&req); - let checking = req.extent.is_none() && own.is_none_or(|e| e == super::whole_plane()) && self.boxes.load(Relaxed) == BOXES_UNKNOWN; + let checking = req.extent.is_none() && (own.is_none() || everywhere) && self.boxes.load(Relaxed) == BOXES_UNKNOWN; let mut missed = false; // A record a whole read couldn't read this time had its box left unchecked: the check // waits for a read that reads them all. @@ -508,7 +509,7 @@ impl FeatureSource for ShpSource { }; // As in `read`, the shape itself is checked; a read by area, its box in the file's // own system included, gives no rows without a shape. - if geometry.as_ref().map_or(req.extent.is_some() || own.is_some_and(|e| e != super::whole_plane()), |g| !g.inside) { + if geometry.as_ref().map_or(req.extent.is_some() || (own.is_some() && !everywhere), |g| !g.inside) { return Ok(()); } let geometry = geometry.map(|p| p.geometry); From ff92ba7ee718fef4464f75a2acbef95e4fec01cf Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 23 Nov 2026 15:02:45 -0600 Subject: [PATCH 5/6] fix(render): a mask from a Shapefile on a map in its own system takes every shape the layer draws (#2788) --- crates/render/src/lib.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index aee86bb9fd..7c057c7e45 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -6967,7 +6967,18 @@ fn layer_masks( let crs = source.coord_crs(); let to_lonlat = crs.to(rustgis_data::raster::Crs::LonLat).ok(); let held = flat.held(crs).ok(); - let result = visit_complete(source.as_ref(), &mut |features| { + // On a map drawn in the file's own system, every shape it draws, those + // longitude/latitude can't place too, as the layer's own read takes them. + let whole = (ctx.globe.is_none() + && crs != rustgis_data::raster::Crs::LonLat + && source.reads_own_box() + && held.as_ref().is_some_and(|h| h.is_identity())) + .then(|| rustgis_data::source::Request { + extent: None, + limit: usize::MAX, + own: Some((crs, rustgis_data::source::whole_plane())), + }); + let result = visit_within(source.as_ref(), whole, &mut |features| { for feature in features { if let Some(g) = feature.geometry { let g = match (ctx.globe.is_some(), crs == rustgis_data::raster::Crs::LonLat) { From eb0141972ad4b4d53685ba67df4ab26838a552ec Mon Sep 17 00:00:00 2001 From: mavrick Date: Mon, 23 Nov 2026 15:51:48 -0600 Subject: [PATCH 6/6] fix(data): an own-box read follows the own box for rows without a shape, and the default reads the extent for a box in another system (#2788) --- crates/data/src/source/mod.rs | 10 ++++++---- crates/data/src/source/shp.rs | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/data/src/source/mod.rs b/crates/data/src/source/mod.rs index 436f60d922..bfd9fe4fd4 100644 --- a/crates/data/src/source/mod.rs +++ b/crates/data/src/source/mod.rs @@ -428,11 +428,13 @@ pub trait FeatureSource: Send + Sync { /// [`FeatureSource::visit_chunks`] for a whole request, its box in the source's own system /// ([`Request::own`]) too (one over the whole plane, [`whole_plane`], reads every feature as a /// map in that system draws them): a source that reads one this way tests each feature's own box - /// against it, converting nothing. One that doesn't visits every feature when given such a - /// box, never fewer than it holds (its extent, in longitude/latitude, may hold less), and - /// reads the extent otherwise. + /// against it, converting nothing. One that doesn't visits every feature when given a box in + /// its own system, never fewer than it holds (its extent, in longitude/latitude, may hold + /// less), and reads the extent otherwise (no box, or one in another system). fn visit_request_chunks(&self, req: &Request, bytes: usize, visit: &mut dyn FnMut(Vec) -> Result<()>) -> Result { - self.visit_chunks(if req.own.is_some() { None } else { req.extent }, bytes, visit) + // A box in another system is read as if absent, as `Request::own` has it. + let in_own = req.own.is_some_and(|(crs, _)| crs == self.coord_crs()); + self.visit_chunks(if in_own { None } else { req.extent }, bytes, visit) } fn info(&self) -> &SourceInfo; /// The coordinate system of the features it gives: a file read in place gives them as the file diff --git a/crates/data/src/source/shp.rs b/crates/data/src/source/shp.rs index 12d67ac935..af40ca4c61 100644 --- a/crates/data/src/source/shp.rs +++ b/crates/data/src/source/shp.rs @@ -509,7 +509,7 @@ impl FeatureSource for ShpSource { }; // As in `read`, the shape itself is checked; a read by area, its box in the file's // own system included, gives no rows without a shape. - if geometry.as_ref().map_or(req.extent.is_some() || (own.is_some() && !everywhere), |g| !g.inside) { + if geometry.as_ref().map_or(if own.is_some() { !everywhere } else { req.extent.is_some() }, |g| !g.inside) { return Ok(()); } let geometry = geometry.map(|p| p.geometry);