From 2679ffb2c076c542201ef6a58d8794cd52e22d0d Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 18 Nov 2026 13:42:52 -0600 Subject: [PATCH] test(engine): cover Shapefile Z/M reader losses --- ATTRIBUTION.md | 1 + ROADMAP.md | 5 +- .../engine/testdata/reading-losses/README.md | 4 +- .../testdata/reading-losses/SHAPEFILE.md | 38 +++++++ .../testdata/reading-losses/shp-line-xy.shp | Bin 0 -> 188 bytes .../testdata/reading-losses/shp-line-xy.shx | Bin 0 -> 108 bytes .../testdata/reading-losses/shp-line-z.shp | Bin 0 -> 220 bytes .../testdata/reading-losses/shp-line-z.shx | Bin 0 -> 108 bytes .../testdata/reading-losses/shp-line-zm.shp | Bin 0 -> 252 bytes .../testdata/reading-losses/shp-line-zm.shx | Bin 0 -> 108 bytes .../testdata/reading-losses/shp-point-m.shp | Bin 0 -> 136 bytes .../testdata/reading-losses/shp-point-m.shx | Bin 0 -> 108 bytes .../testdata/reading-losses/shp-point-xy.shp | Bin 0 -> 128 bytes .../testdata/reading-losses/shp-point-xy.shx | Bin 0 -> 108 bytes .../testdata/reading-losses/shp-point-z.shp | Bin 0 -> 144 bytes .../testdata/reading-losses/shp-point-z.shx | Bin 0 -> 108 bytes .../testdata/reading-losses/shp-row.dbf | Bin 0 -> 68 bytes crates/engine/tests/never_silent_reading.rs | 103 ++++++++++++++++-- 18 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 crates/engine/testdata/reading-losses/SHAPEFILE.md create mode 100644 crates/engine/testdata/reading-losses/shp-line-xy.shp create mode 100644 crates/engine/testdata/reading-losses/shp-line-xy.shx create mode 100644 crates/engine/testdata/reading-losses/shp-line-z.shp create mode 100644 crates/engine/testdata/reading-losses/shp-line-z.shx create mode 100644 crates/engine/testdata/reading-losses/shp-line-zm.shp create mode 100644 crates/engine/testdata/reading-losses/shp-line-zm.shx create mode 100644 crates/engine/testdata/reading-losses/shp-point-m.shp create mode 100644 crates/engine/testdata/reading-losses/shp-point-m.shx create mode 100644 crates/engine/testdata/reading-losses/shp-point-xy.shp create mode 100644 crates/engine/testdata/reading-losses/shp-point-xy.shx create mode 100644 crates/engine/testdata/reading-losses/shp-point-z.shp create mode 100644 crates/engine/testdata/reading-losses/shp-point-z.shx create mode 100644 crates/engine/testdata/reading-losses/shp-row.dbf diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 3ef6f4e5cf..53373ced68 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -17,6 +17,7 @@ of use. |---|---|---| | Paired binary reader-loss fixtures (`crates/engine/testdata/reading-losses/xy.*`, `xyz.*`) | The RustGIS project: one made-up point, written offline with [GDAL](https://gdal.org) 3.13.3 (FlatGeobuf and FileGDB) and [PyArrow](https://arrow.apache.org/docs/python/) 26.0.1 (GeoParquet); [fixture notes](crates/engine/testdata/reading-losses/README.md) describe the minimal data and retained files | Apache-2.0 | | Historical release projects and preferences (`crates/engine/testdata/releases/v*.rgis`, `v*.prefs.json`) | The RustGIS project: unchanged output of the published binaries, with made-up layer data. The [fixture README](crates/engine/testdata/releases/README.md) lists writers and archive checksums. | Apache-2.0 | +| Shapefile Z/M reader-loss fixtures (`crates/engine/testdata/reading-losses/shp-*`) | The RustGIS project: independently encoded, made-up point and line records following the published Shapefile specification; byte layout in [SHAPEFILE.md](crates/engine/testdata/reading-losses/SHAPEFILE.md) | Apache-2.0 | | OSM PBF ring-loss fixtures (`crates/engine/testdata/reading-losses/hole-*.osm.pbf`) | The RustGIS project: independently encoded, made-up nodes, ways and relation from the published OSM PBF schema; contents and checksums in [OSM.md](crates/engine/testdata/reading-losses/OSM.md) | Apache-2.0 | | Interface themes (`crates/ui-egui/themes/light.json`, `dark.json`) | The RustGIS project | Apache-2.0 | | Sample data (`countries`, `cities`, `rivers`, `lakes` GeoJSON), embedded at build time from [rustgis-assets](https://github.com/opengeos/rustgis-assets/tree/main/data) | Natural Earth 1:110m, attributes trimmed | Public domain | diff --git a/ROADMAP.md b/ROADMAP.md index 823539a01c..d880a7d1ac 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -75,9 +75,10 @@ A result is right, or RustGIS says what it lost or assumed each vertex's Z in DXF and unreadable coordinates, holes and parts in KML, GeoRSS, MapInfo and DXF. A fix makes its case `Said` _(open)_. - Lossy sample files in `never_silent_reading.rs` for the readers it doesn't cover yet: - Shapefile and spreadsheets. It covers GeoJSON, TopoJSON, CSV, KML, KMZ, GPX, GML, GeoRSS, + spreadsheets. It covers GeoJSON, TopoJSON, CSV, KML, KMZ, GPX, GML, GeoRSS, DXF, LandXML and MapInfo, with height-loss cases for CityJSON, CityGML, GeoPackage, - FlatGeobuf, GeoParquet and FileGDB, and ring-loss cases for OSM PBF; other loss kinds remain. + FlatGeobuf, GeoParquet and FileGDB, Z/M-loss cases for Shapefile, and ring-loss cases for + OSM PBF; other loss kinds remain. Exports are checked by `never_silent_roundtrip.rs` _(open)_. - A `--strict` option for `rustgis-cli run` that exits non-zero when a loss is reported, for scheduled jobs. Notices carry a severity but not a kind today (`Notice` in diff --git a/crates/engine/testdata/reading-losses/README.md b/crates/engine/testdata/reading-losses/README.md index cdf327de41..d9947dc65b 100644 --- a/crates/engine/testdata/reading-losses/README.md +++ b/crates/engine/testdata/reading-losses/README.md @@ -1,6 +1,6 @@ # Paired reader-loss fixtures -Each format holds one point and no user attributes: `xy` has `(1, 2)` and `xyz` has +The `xy.*` and `xyz.*` pairs hold one point and no user attributes: `xy` has `(1, 2)` and `xyz` has `(1, 2, 3)`, in WGS 84 longitude/latitude. The reading corpus opens each pair from the same path and checks that only the XYZ file reports dropping Z or M. These test the shared engine command, not just a reader's internal report. @@ -37,3 +37,5 @@ statistics. The value is little-endian ISO WKB: byte order `1`, type `1` (Point) metadata declares version `1.0.0`, primary column `geometry`, encoding `WKB` and geometry type `Point` or `Point Z`. CRS is omitted, meaning the format's default OGC:CRS84. This is not a fixture for an unknown CRS or for CRS fallback behavior. + +The independently encoded [Shapefile pairs](SHAPEFILE.md) cover point and line Z/M loss. diff --git a/crates/engine/testdata/reading-losses/SHAPEFILE.md b/crates/engine/testdata/reading-losses/SHAPEFILE.md new file mode 100644 index 0000000000..d7cac01aad --- /dev/null +++ b/crates/engine/testdata/reading-losses/SHAPEFILE.md @@ -0,0 +1,38 @@ +# Shapefile Z/M pairs + +Made-up data, independently encoded as bytes for RustGIS (Apache-2.0), following +[Shapefile Technical Description](https://content.esri.com/support/whitepapers/ao_/shapefile.pdf), +file headers and Tables 4, 6, 8, 12 and 14. No production writer or third-party +code generated these fixtures; no generator is needed to run the tests. + +Every `.shp` contains one record. Point XY is `(1, 2)`; the single-part line's XY +vertices are `(1, 2), (3, 4)`. Both clean and lossy inputs must import this geometry +and the one `ID=1` attribute row unchanged. All four lossy inputs currently import +and report dropping Z or M; clean inputs do not report that loss. + +| Stem (`shp-`) | Shape type | Extra ordinates | `.shp` bytes | +| --- | --- | --- | --- | +| `point-xy` | 1, Point | None; clean partner for both points | 128 | +| `point-z` | 11, PointZ | Z=3; M=-1e39 (no-data sentinel) | 144 | +| `point-m` | 21, PointM | M=7 | 136 | +| `line-xy` | 3, PolyLine | None; clean partner for both lines | 188 | +| `line-z` | 13, PolyLineZ | Z=[5,9]; optional M section absent | 220 | +| `line-zm` | 13, PolyLineZ | Z=[5,9], M=[7,11] | 252 | + +The first 100 bytes are the format's header: file code 9994 and file length in +big-endian 16-bit words, version 1000 and shape type in little endian, then XY, +Z and M bounds as little-endian float64. Record number 1 and content length are +big endian; record contents are little endian. Each matching `.shx` is 108 bytes: +the same header with file length 54 words, then offset 50 words and the record's +content length. Line contents have one part starting at vertex 0, the XY array, +and (for type 13) the Z bounds and array, followed by M bounds and array only in +`line-zm`. The PointZ M sentinel is below the specification's -1e38 no-data limit, +so this case exercises a height without a real measure. + +The shared `shp-row.dbf` is 68 bytes: dBASE III, date 2026-11-18, one row, a +65-byte header and two-byte record; one numeric field `ID`, width 1, no decimals, +header terminator 0x0d, undeleted row ` 1`, EOF 0x1a. The harness supplies identical +WGS 84 `.prj` and UTF-8 `.cpg` text beside both files. This isolates ordinate loss +from coordinate-system assumptions and DBF encoding behavior. The corpus checks +the native reader first and then `layer.addData`, so external fallbacks or empty +imports cannot stand in for valid XY geometry. diff --git a/crates/engine/testdata/reading-losses/shp-line-xy.shp b/crates/engine/testdata/reading-losses/shp-line-xy.shp new file mode 100644 index 0000000000000000000000000000000000000000..6b8a638ccf882f73ee9e798a0de7b925f15cc88f GIT binary patch literal 188 zcmZQzQ0HR64&q)gGcW)V6nwCUG8`Z@2b30o(hyM`BqLB~}0V9xOL5x`-g-pmg8A!Dk G;SK;!EeaR_ literal 0 HcmV?d00001 diff --git a/crates/engine/testdata/reading-losses/shp-line-z.shx b/crates/engine/testdata/reading-losses/shp-line-z.shx new file mode 100644 index 0000000000000000000000000000000000000000..71b54bddf0cd477d8c4d0bc8740943cdde286e43 GIT binary patch literal 108 tcmZQzQ0HR64$NLKGcfSN~}ff0~n0RaD~1cv|s literal 0 HcmV?d00001 diff --git a/crates/engine/testdata/reading-losses/shp-line-zm.shp b/crates/engine/testdata/reading-losses/shp-line-zm.shp new file mode 100644 index 0000000000000000000000000000000000000000..d48d342e0562636c3108537e39ad8530c5ebf8db GIT binary patch literal 252 zcmZQzQ0HR64(eVoGcfSNJ$J1Y!?j%mOK70%8Op N)n2^r02+&M7XUM741NFr literal 0 HcmV?d00001 diff --git a/crates/engine/testdata/reading-losses/shp-line-zm.shx b/crates/engine/testdata/reading-losses/shp-line-zm.shx new file mode 100644 index 0000000000000000000000000000000000000000..8ec8200546190b46207c72fda97893b0b08fdb2e GIT binary patch literal 108 vcmZQzQ0HR64$NLKGcfSNJ#u0%8vU3A+VZ literal 0 HcmV?d00001 diff --git a/crates/engine/testdata/reading-losses/shp-point-m.shp b/crates/engine/testdata/reading-losses/shp-point-m.shp new file mode 100644 index 0000000000000000000000000000000000000000..a1e6e9e990dfd2a55ce71ae426b07be5b2fb82b0 GIT binary patch literal 136 ucmZQzQ0HR64qRR^Gcbt4huz|#dT<_BVd)FK031w$zSRkQ*( literal 0 HcmV?d00001 diff --git a/crates/engine/tests/never_silent_reading.rs b/crates/engine/tests/never_silent_reading.rs index 71ebf015df..3a6e5fd4a9 100644 --- a/crates/engine/tests/never_silent_reading.rs +++ b/crates/engine/tests/never_silent_reading.rs @@ -11,8 +11,8 @@ //! fails here until the case is moved to [`Expect::Said`], so the list only shrinks. Exports have //! their own test, `never_silent_roundtrip.rs`. //! -//! The table includes text, CityJSON, CityGML, KMZ, GeoPackage, FlatGeobuf, GeoParquet, FileGDB -//! and OSM PBF. More formats and loss kinds remain to add (ROADMAP.md); one loss does not +//! The table includes text, CityJSON, CityGML, KMZ, GeoPackage, FlatGeobuf, GeoParquet, FileGDB, +//! Shapefile and OSM PBF. More formats and loss kinds remain to add (ROADMAP.md); one loss does not //! certify a reader. use rustgis_engine::{Project, Session}; use serde_json::json; @@ -37,13 +37,13 @@ struct Case { file: &'static str, clean: Vec, lossy: Vec, - /// Files beside it, the same in both (a MapInfo `.mid`). - beside: Vec<(&'static str, String)>, + /// Files beside the clean and lossy inputs (binary indexes can differ). + beside: [Vec<(&'static str, Vec)>; 2], expect: Expect, } fn case(name: &'static str, file: &'static str, clean: impl AsRef<[u8]>, lossy: impl AsRef<[u8]>, expect: Expect) -> Case { - Case { name, file, clean: clean.as_ref().to_vec(), lossy: lossy.as_ref().to_vec(), beside: Vec::new(), expect } + Case { name, file, clean: clean.as_ref().to_vec(), lossy: lossy.as_ref().to_vec(), beside: [Vec::new(), Vec::new()], expect } } /// A GeoJSON feature collection of these geometries. @@ -97,6 +97,7 @@ fn landxml(content: &str) -> String { format!(r#"{content}"#) } +/// Builds paired inputs, keeping sidecars specific to each input when their binary layout differs. fn cases(fixture_dir: &std::path::Path) -> Vec { let point = r#"{"type":"Point","coordinates":[1,2]}"#; let topo_line = |arcs: &str| format!(r#"{{"type":"Topology","objects":{{"o":{{"type":"LineString","arcs":[0]}}}},"arcs":[{arcs}]}}"#); @@ -357,7 +358,9 @@ fn cases(fixture_dir: &std::path::Path) -> Vec { // MapInfo: the .mid beside each .mif holds one row per object. let mut mapinfo = |name, clean: &str, lossy: &str, mid: &str, expect| { let mut c = case(name, "a.mif", mif(clean), mif(lossy), expect); - c.beside.push(("a.mid", mid.to_string())); + for beside in &mut c.beside { + beside.push(("a.mid", mid.as_bytes().to_vec())); + } out.push(c); }; mapinfo("MapInfo: an unreadable object", "Point 0 0\nPoint 1 1\n", "Point 0 0\nPoint 1 x\n", "1\n2\n", Said("couldn't be read")); @@ -386,7 +389,7 @@ fn cases(fixture_dir: &std::path::Path) -> Vec { file: "a.kmz", clean: rustgis_engine::data::zip::write(&[("doc.kml".into(), c.clean.clone())]).unwrap(), lossy: rustgis_engine::data::zip::write(&[("doc.kml".into(), c.lossy.clone())]).unwrap(), - beside: Vec::new(), + beside: [Vec::new(), Vec::new()], expect: c.expect, }) .collect::>(); @@ -417,6 +420,48 @@ fn cases(fixture_dir: &std::path::Path) -> Vec { Imported("Z or M"), ), ]); + // Independently encoded shapes: scalar Z, scalar M, and arrays with/without optional M. + for (name, clean, lossy, clean_index, lossy_index) in [ + ( + "Shapefile: a point height", + include_bytes!("../testdata/reading-losses/shp-point-xy.shp").as_slice(), + include_bytes!("../testdata/reading-losses/shp-point-z.shp").as_slice(), + include_bytes!("../testdata/reading-losses/shp-point-xy.shx"), + include_bytes!("../testdata/reading-losses/shp-point-z.shx"), + ), + ( + "Shapefile: a point measure", + include_bytes!("../testdata/reading-losses/shp-point-xy.shp").as_slice(), + include_bytes!("../testdata/reading-losses/shp-point-m.shp").as_slice(), + include_bytes!("../testdata/reading-losses/shp-point-xy.shx"), + include_bytes!("../testdata/reading-losses/shp-point-m.shx"), + ), + ( + "Shapefile: line heights without measures", + include_bytes!("../testdata/reading-losses/shp-line-xy.shp").as_slice(), + include_bytes!("../testdata/reading-losses/shp-line-z.shp").as_slice(), + include_bytes!("../testdata/reading-losses/shp-line-xy.shx"), + include_bytes!("../testdata/reading-losses/shp-line-z.shx"), + ), + ( + "Shapefile: line heights and measures", + include_bytes!("../testdata/reading-losses/shp-line-xy.shp").as_slice(), + include_bytes!("../testdata/reading-losses/shp-line-zm.shp").as_slice(), + include_bytes!("../testdata/reading-losses/shp-line-xy.shx"), + include_bytes!("../testdata/reading-losses/shp-line-zm.shx"), + ), + ] { + let mut c = case(name, "a.shp", clean, lossy, Imported("Z or M")); + for (beside, index) in c.beside.iter_mut().zip([clean_index, lossy_index]) { + beside.extend([ + ("a.shx", index.to_vec()), + ("a.dbf", include_bytes!("../testdata/reading-losses/shp-row.dbf").to_vec()), + ("a.cpg", b"UTF-8\n".to_vec()), + ("a.prj", br#"GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]"#.to_vec()), + ]); + } + out.push(c); + } out } @@ -476,6 +521,22 @@ fn gpkg(dir: &std::path::Path, z: bool) -> Vec { std::fs::read(path).unwrap() } +/// Both Shapefile inputs must retain the one attribute row and every XY vertex, through +/// the native reader and the command. A notice over an empty or partial import isn't enough. +fn check_shapefile_xy(ds: &rustgis_engine::data::Dataset, shape_type: Option) -> std::io::Result<()> { + use rustgis_engine::data::{Geometry, Value}; + use rustgis_engine::geom::Coord; + let expected = match shape_type { + Some(1 | 11 | 21) => Geometry::Point(Coord::new(1.0, 2.0)), + Some(3 | 13) => Geometry::Line(vec![vec![Coord::new(1.0, 2.0), Coord::new(3.0, 4.0)]]), + _ => return Err(std::io::Error::other("Shapefile fixture has the wrong geometry type")), + }; + if ds.features.len() != 1 || ds.features[0].geometry.as_ref() != Some(&expected) || ds.features[0].attrs != vec![Value::Int(1)] { + return Err(std::io::Error::other("Shapefile fixture didn't retain its XY geometry and ID")); + } + Ok(()) +} + /// What `layer.addData` did with a file: the notices it gave and whether it added a layer or table, /// or why it refused the file. type Reading = Result<(Vec, bool), String>; @@ -485,11 +546,11 @@ type Reading = Result<(Vec, bool), String>; /// case is read from the same `dir`, so notices naming its path are the same for both. /// A `.gdb` fixture is stored as a ZIP of its table files, extracted and checked with the native /// reader before the command opens it, so an optional GDAL fallback cannot mask a regression. -fn read(dir: &std::path::Path, file: &str, content: &[u8], beside: &[(&str, String)]) -> std::io::Result { +fn read(dir: &std::path::Path, file: &str, content: &[u8], beside: &[(&str, Vec)]) -> std::io::Result { let _ = std::fs::remove_dir_all(dir); std::fs::create_dir_all(dir)?; - for (name, text) in beside { - std::fs::write(dir.join(name), text)?; + for (name, bytes) in beside { + std::fs::write(dir.join(name), bytes)?; } let path = dir.join(file); if file.ends_with(".gdb") { @@ -514,6 +575,18 @@ fn read(dir: &std::path::Path, file: &str, content: &[u8], beside: &[(&str, Stri } else { std::fs::write(&path, content)?; } + if file.ends_with(".shp") { + let (native, report) = + rustgis_engine::data::io::read_path(&path).map_err(|e| std::io::Error::other(format!("native Shapefile fixture: {e}")))?; + check_shapefile_xy(&native, content.get(32).copied())?; + let zm = matches!(content.get(32), Some(11 | 13 | 21)); + if native.zm_dropped != zm || report.messages.iter().any(|m| m.contains("Z or M")) != zm { + return Err(std::io::Error::other(format!("native Shapefile fixture has the wrong loss report: {report:?}"))); + } + if report.skipped != 0 { + return Err(std::io::Error::other("native Shapefile fixture skipped a record")); + } + } let mut s = Session::new(Project::default()); // Layers and standalone tables (what a file without geometry adds). let held = |s: &Session| s.map().layers.len() + s.project.tables.len(); @@ -522,6 +595,12 @@ fn read(dir: &std::path::Path, file: &str, content: &[u8], beside: &[(&str, Stri if let Err(e) = s.run("layer.addData", &json!({"path": path})) { return Ok(Err(e)); } + if file.ends_with(".shp") { + if s.map().layers.len() != 1 { + return Err(std::io::Error::other("Shapefile fixture didn't add exactly one layer")); + } + check_shapefile_xy(&s.map().layers[0].dataset, content.get(32).copied())?; + } let said = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); Ok(Ok((said, held(&s) > before))) } @@ -533,7 +612,7 @@ fn a_reading_that_loses_something_says_so() { let mut failures = Vec::new(); for c in cases(&root.join("fixtures")) { let name = format!("{} [{}]", c.name, c.file); - let clean = match read(&dir, c.file, &c.clean, &c.beside) { + let clean = match read(&dir, c.file, &c.clean, &c.beside[0]) { Err(e) => { failures.push(format!("{name}: the clean file couldn't be written: {e}")); continue; @@ -549,7 +628,7 @@ fn a_reading_that_loses_something_says_so() { Ok(Ok((said, true))) => said, }; // What the lossy file says that the clean one doesn't, and whether it added a layer. - let (new, added): (Vec, bool) = match read(&dir, c.file, &c.lossy, &c.beside) { + let (new, added): (Vec, bool) = match read(&dir, c.file, &c.lossy, &c.beside[1]) { Err(e) => { failures.push(format!("{name}: the lossy file couldn't be written: {e}")); continue;