Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/data/src/source/gpkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,22 @@ impl FeatureSource for GpkgSource {
true
}

/// Asked only of a count GDAL keeps (`count_kept`), which may be past the rows or short of
/// them: whether the table has a row past the first `n`. SQLite steps over `n` rows to answer,
/// without counting the rest. A file that can't answer is taken to hold more, so what depends
/// on it says it may not have every feature rather than going by a count that may be wrong.
fn holds_more_than(&self, n: u64) -> Option<bool> {
if !self.count_kept {
return None;
}
let sql = format!("SELECT EXISTS(SELECT 1 FROM {} LIMIT 1 OFFSET ?1)", quote(&self.table));
let asked = |offset: i64| {
let guard = self.conn.lock().ok()?;
guard.as_ref()?.query_row(&sql, [offset], |r| r.get(0)).ok()
};
Some(i64::try_from(n).ok().and_then(asked).unwrap_or(true))
}

/// Its own system, in which it gives its features as the file holds them.
fn coord_crs(&self) -> crate::raster::Crs {
self.reproject.crs()
Expand Down
10 changes: 10 additions & 0 deletions crates/data/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,12 @@ pub trait FeatureSource: Send + Sync {
fn field_range(&self, _field: &str) -> Option<(f64, f64)> {
None
}
/// Whether the source holds more features than the `n` read, asked of it when its count
/// ([`SourceInfo::count`]) may be wrong and it can tell without reading them all (a
/// GeoPackage's kept count, #2898); `None` when its count is taken as it is, or it can't tell.
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
fn holds_more_than(&self, _n: u64) -> Option<bool> {
None
}
/// Whether the source's notes ([`SourceInfo::notes`]) already count the features it keeps
/// with a place that can't be given in longitude/latitude, so a whole read doesn't count them
/// again.
Expand Down Expand Up @@ -902,6 +908,10 @@ impl FeatureSource for Fetching {
self.0.field_range(field)
}

fn holds_more_than(&self, n: u64) -> Option<bool> {
self.0.holds_more_than(n)
}

fn says_unplaced_at_open(&self) -> bool {
self.0.says_unplaced_at_open()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8324,7 +8324,7 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult {
};
let order = if rest.by_number { "by number" } else { "in text order" };
// A view that holds the whole source says nothing.
let part = l.source.as_ref().is_some_and(|src| src.exact_count() != Some(l.dataset.features.len() as u64));
let part = l.source.as_ref().is_some_and(|src| !src.holds_only(l.dataset.features.len() as u64));
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
let from_view = from_view.filter(|_| part).map(|then| (l.dataset.kept_features(&l.visible_rows()).count(), then));
let mut dropped = None;
let out = with(s, p, |l| {
Expand Down
60 changes: 60 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51299,6 +51299,66 @@ fn hand_edits_and_the_notice_on_a_geoparquet_read_in_place_follow_the_view() {
let _ = std::fs::remove_dir_all(dir);
}

/// On a GeoPackage read in place, the notice of a view that isn't the whole layer follows the rows
/// the file holds, not the count GDAL keeps in `gpkg_ogr_contents`, which an `INSERT OR REPLACE`
/// leaves high (#2898): 40 points, whose kept count says 40, 50 or 10, with the whole layer in
/// view or a quarter of it.
#[test]
fn the_notice_of_a_part_of_a_geopackage_follows_its_rows_not_its_kept_count() {
use rustgis_data::{Coord, Dataset, Field, FieldType, Geometry, GeometryType, Value as V};
// Ten points on each of the parallels 0, 20, 40 and 60.
let mut ds = Dataset::new("pts", GeometryType::Point, vec![Field::new("n", FieldType::Integer)]);
for i in 0..40 {
ds.push(Some(Geometry::Point(Coord::new(f64::from(i % 10), f64::from(i / 10 * 20)))), vec![V::Int(i64::from(i))]);
}
let dir = std::env::temp_dir().join(format!("rustgis-kept-count-notice-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let whole = rustgis_geom::Extent::new(-1.0, -5.0, 10.0, 65.0);
let part = rustgis_geom::Extent::new(-1.0, -3.0, 10.0, 3.0);
// The kept count, the view, the features it reads and whether the notice is said.
let mut wrong = Vec::new();
for (kept, view, read, said) in [
(40, whole, 40, false),
(40, part, 10, true),
(50, whole, 40, false),
(50, part, 10, true),
(10, whole, 40, false),
(10, part, 10, true),
] {
let path = dir.join(format!("pts-{kept}-{read}.gpkg"));
rustgis_data::io::write_path(&ds, &path).unwrap();
// The count and the two triggers GDAL keeps it with.
rusqlite::Connection::open(&path)
.unwrap()
.execute_batch(&format!(
"CREATE TABLE IF NOT EXISTS gpkg_ogr_contents (table_name TEXT NOT NULL PRIMARY KEY, feature_count INTEGER DEFAULT NULL);
INSERT OR REPLACE INTO gpkg_ogr_contents VALUES ('pts', {kept});
CREATE TRIGGER IF NOT EXISTS \"trigger_insert_feature_count_pts\" AFTER INSERT ON \"pts\" BEGIN UPDATE gpkg_ogr_contents
SET feature_count = feature_count + 1 WHERE lower(table_name) = lower('pts'); END;
CREATE TRIGGER IF NOT EXISTS \"trigger_delete_feature_count_pts\" AFTER DELETE ON \"pts\" BEGIN UPDATE gpkg_ogr_contents
SET feature_count = feature_count - 1 WHERE lower(table_name) = lower('pts'); END;"
))
.unwrap();
let mut s = Session::new(Project::default());
let id =
s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap()["layer"].as_u64().unwrap() as u32;
let l = s.map().layer(id).unwrap();
assert!(l.is_streamed() && l.source.as_ref().and_then(|src| src.exact_count()) == Some(kept), "{kept}");
let v = view_of(&mut s, view);
crate::render::render_png(s.map_mut(), &v).unwrap();
assert_eq!(s.map().layer(id).unwrap().dataset.features.len(), read, "{kept}");
let mark = s.notice_mark();
s.run("layer.symbology", &json!({"layer": id, "type": "graduated", "field": "n", "classes": 3})).unwrap();
let notices: Vec<_> =
s.notices_since(mark).iter().filter(|n| n.text.contains("of the view read so far")).map(|n| n.text.clone()).collect();
if notices.len() != usize::from(said) {
wrong.push(format!("kept {kept}, {read} read: {notices:?}"));
}
}
let _ = std::fs::remove_dir_all(dir);
assert!(wrong.is_empty(), "{wrong:#?}");
}

/// Raster-side projection failures retain good multipart coverage and reach both command results.
#[test]
fn raster_projection_failures_are_reported_for_zonal_statistics_and_clipping() {
Expand Down
9 changes: 9 additions & 0 deletions crates/map/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,15 @@ impl SourceLayer {
self.info().filter(|_| !database)?.count
}

/// Whether the `n` features read from it are all its source holds: asked of the source when
/// its count may be wrong ([`FeatureSource::holds_more_than`], #2898), else by its exact count.
pub fn holds_only(&self, n: u64) -> bool {
match self.handle.as_ref().and_then(|h| h.holds_more_than(n)) {
Some(more) => !more,
None => self.exact_count() == Some(n),
}
}

/// Whether its source tests its features against a box in its own system
/// ([`FeatureSource::reads_own_box`]); no until it is open.
pub fn reads_own_box(&self) -> bool {
Expand Down
Loading