From 42473fc81d1cd53779978302cb3b1deb0ff8ec1e Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 12 Nov 2026 18:08:28 -0600 Subject: [PATCH 01/13] test(geom, data): geodesic identities and reprojection round trips as properties A distance is symmetric, zero only between names of one place and never longer than a detour; a ring measures the same from any vertex, either way round, closed or with a vertex twice (bands, caps, antimeridian seams included). A place taken into a coordinate system comes back within a centimetre inside its area of use, and is refused or brought back outside it. What a system does instead is listed with its reason; known bugs are marked. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/nightly.yml | 2 + Cargo.lock | 2 + crates/data/Cargo.toml | 1 + crates/data/tests/reprojection_round_trip.rs | 180 ++++++++++++++++++ crates/geom/Cargo.toml | 3 + crates/geom/tests/geodesic_identities.rs | 184 +++++++++++++++++++ 6 files changed, 372 insertions(+) create mode 100644 crates/data/tests/reprojection_round_trip.rs create mode 100644 crates/geom/tests/geodesic_identities.rs diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index f8e9a91459..3d09045016 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -120,6 +120,8 @@ jobs: PROPERTY_CASES: "2000" run: | cargo test --locked -p rustgis-engine --test never_silent_roundtrip + cargo test --locked -p rustgis-data --test reprojection_round_trip + cargo test --locked -p rustgis-geom --test geodesic_identities report: name: Report diff --git a/Cargo.lock b/Cargo.lock index 8a61abbe4a..1787c8eed9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4787,6 +4787,7 @@ dependencies = [ "parquet", "postgres", "proj4rs", + "proptest", "quick-xml 0.42.0", "roxmltree", "rusqlite", @@ -4863,6 +4864,7 @@ name = "rustgis-geom" version = "0.6.0" dependencies = [ "geographiclib-rs", + "proptest", "serde", ] diff --git a/crates/data/Cargo.toml b/crates/data/Cargo.toml index 6ff9ebd2b9..c393989b64 100644 --- a/crates/data/Cargo.toml +++ b/crates/data/Cargo.toml @@ -79,6 +79,7 @@ rustix = { version = "1.1", features = ["process", "fs"] } parquet = { workspace = true, features = ["arrow"] } arrow-array = { workspace = true } arrow-schema = { workspace = true } +proptest = { workspace = true } [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] # Writing Zstandard-compressed GeoTIFFs. diff --git a/crates/data/tests/reprojection_round_trip.rs b/crates/data/tests/reprojection_round_trip.rs new file mode 100644 index 0000000000..0313a57ed1 --- /dev/null +++ b/crates/data/tests/reprojection_round_trip.rs @@ -0,0 +1,180 @@ +//! Reprojection round trips, as a property: a longitude/latitude taken into a coordinate system +//! with `Transformer::from_lonlat` and back with `to_lonlat` is the place it was, within a +//! centimetre on the ground. Inside a system's area of use a place must go there and back; outside +//! it the system may refuse it (`None`), but a place it does take must come back, never somewhere +//! else. What a system does instead is listed in [`LIMITS`] and [`OUTLINE_REFUSALS`] with the +//! reason; a known bug is marked `bug:`. Places lean on the areas' edges and corners, the poles +//! and the antimeridian. +//! +//! Pull requests run a fixed seed; the nightly a new one with more cases. A failure gives its seed +//! and the place found that doesn't come back. To run a seed again, or deeper: +//! `PROPERTY_SEED=7 PROPERTY_CASES=200000 cargo test -p rustgis-data --test reprojection_round_trip`. +use proptest::prelude::*; +use proptest::test_runner::{Config, RngAlgorithm, TestCaseError, TestRng, TestRunner}; +use rustgis_data::raster::{Crs, Transformer}; +use rustgis_geom::{Coord, wgs84_distance_m}; +use std::sync::atomic::{AtomicUsize, Ordering}; + +/// How far (metres on the ground) a place may come back from where it was. +const TOLERANCE_M: f64 = 0.01; + +/// Coordinate systems, each with its area of use (west, east, south, north; degrees, east of +/// 180° for one across the antimeridian), roughly as EPSG gives it. +const SYSTEMS: &[(&str, (f64, f64, f64, f64))] = &[ + ("EPSG:3857", (-180.0, 180.0, -85.051_128, 85.051_128)), + ("EPSG:32617", (-84.0, -78.0, 0.0, 84.0)), + ("EPSG:32760", (174.0, 180.0, -80.0, 0.0)), + ("EPSG:32601", (-180.0, -174.0, 0.0, 84.0)), + ("EPSG:32661", (-180.0, 180.0, 60.0, 90.0)), + ("EPSG:32761", (-180.0, 180.0, -90.0, -60.0)), + ("EPSG:3413", (-180.0, 180.0, 30.0, 90.0)), + ("EPSG:3031", (-180.0, 180.0, -90.0, -60.0)), + ("EPSG:2154", (-9.86, 10.38, 41.15, 51.56)), + ("EPSG:3347", (-141.0, -47.0, 38.0, 86.0)), + ("EPSG:5070", (-124.79, -66.91, 24.41, 49.38)), + ("EPSG:3577", (112.0, 154.0, -44.0, -10.0)), + ("EPSG:3035", (-35.58, 44.83, 24.6, 84.73)), + ("EPSG:3338", (-172.54, -129.99, 51.3, 71.4)), + ("EPSG:27700", (-9.0, 2.0, 49.75, 61.0)), + ("EPSG:2056", (5.96, 10.49, 45.82, 47.81)), + ("EPSG:2193", (166.37, 178.63, -47.33, -34.1)), + ("EPSG:3832", (98.0, 300.0, -60.0, 66.67)), + ("EPSG:4087", (-180.0, 180.0, -90.0, 90.0)), + ("EPSG:6933", (-180.0, 180.0, -86.0, 86.0)), + ("+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs", (-180.0, 180.0, -90.0, 90.0)), + ("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs", (-180.0, 180.0, -90.0, 90.0)), + ("+proj=natearth +lon_0=0 +datum=WGS84 +units=m +no_defs", (-180.0, 180.0, -90.0, 90.0)), + ("+proj=wintri +lon_0=0 +datum=WGS84 +units=m +no_defs", (-180.0, 180.0, -90.0, 90.0)), + ("+proj=eck4 +lon_0=0 +datum=WGS84 +units=m +no_defs", (-180.0, 180.0, -90.0, 90.0)), +]; + +/// Where a system doesn't bring a place back to within [`TOLERANCE_M`], as (system, how far in +/// metres a place may come back from where it was, whether inside its area of use too or only +/// outside it, why). Each but Eckert IV's (RustGIS's own) is PROJ's behaviour too, checked with +/// PROJ 9.8.1. +const LIMITS: &[(&str, f64, bool, &str)] = &[ + ("EPSG:3857", f64::INFINITY, false, "a latitude past 85.05° is taken as 85.05°, where Web Mercator ends; an export says so"), + // bug: transverse Mercator takes places up to 150° from its central meridian (as PROJ does), + // and those near its singular points, 90° off on the equator, come back anywhere. + ("EPSG:32617", f64::INFINITY, false, "transverse Mercator takes places it can't bring back"), + ("EPSG:32760", f64::INFINITY, false, "transverse Mercator takes places it can't bring back"), + ("EPSG:32601", f64::INFINITY, false, "transverse Mercator takes places it can't bring back"), + ("EPSG:2193", f64::INFINITY, false, "transverse Mercator takes places it can't bring back"), + ("EPSG:27700", f64::INFINITY, false, "transverse Mercator takes places it can't bring back"), + // The authalic latitude's sine is nearly 1 near a pole, and its arcsine loses half its digits + // there: about R·√ε, a decimetre. + ("EPSG:3035", 0.2, false, "the authalic latitude rounds within a ten-thousandth of a degree of a pole"), + ("EPSG:6933", 0.2, false, "the authalic latitude rounds within a ten-thousandth of a degree of a pole"), + // Robinson's inverse finds the latitude to 2e-6° (`projection_methods.rs`). + ("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs", 0.25, true, "Robinson's inverse settles to 2e-6°"), + // bug: Eckert IV's inverse (`rustgis_geom::world`) stops where the residual is under 1e-9 of + // the radius, and y hardly changes with latitude near a pole: metres there. + ("+proj=eck4 +lon_0=0 +datum=WGS84 +units=m +no_defs", 10.0, true, "Eckert IV's inverse stops short near a pole"), + // bug: Albers' inverse (proj4rs's, as PROJ's) snaps a latitude within about 0.02° of a pole + // onto it. + ("EPSG:5070", 5_000.0, false, "Albers takes a place near a pole as the pole"), + ("EPSG:3577", 5_000.0, false, "Albers takes a place near a pole as the pole"), + ("EPSG:3338", 5_000.0, false, "Albers takes a place near a pole as the pole"), + // bug: oblique Mercator takes a place more than 90° round its sphere from Bern (as PROJ + // does), and brings it back mirrored, thousands of kilometres away. + ("EPSG:2056", f64::INFINITY, false, "Swiss oblique Mercator mirrors the far side of the globe"), +]; + +/// Systems that refuse a place in their area of use on a world map's outline (within a hundredth +/// of a degree of ±180° or a pole), as (system, why). +const OUTLINE_REFUSALS: &[(&str, &str)] = &[ + // bug: Robinson's inverse refuses a longitude its latitude's rounding puts past ±180°, so a + // world Robinson file loses every shape that touches the map's edge. + ("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs", "Robinson's inverse refuses the map's edge"), + // bug: Eckert IV's inverse (`rustgis_geom::world`) refuses the pole line away from the + // central meridian. + ("+proj=eck4 +lon_0=0 +datum=WGS84 +units=m +no_defs", "Eckert IV's inverse refuses the pole line"), +]; + +/// A number from the environment, else `default`. +fn env(name: &str, default: u64) -> u64 { + std::env::var(name).ok().and_then(|v| v.parse().ok()).unwrap_or(default) +} + +/// A longitude folded into −180..180. +fn fold(lon: f64) -> f64 { + if lon > 180.0 { lon - 360.0 } else { lon } +} + +/// A place in an area of use, often on its edges or corners: (place, whether it is inside). +fn inside((w, e, s, n): (f64, f64, f64, f64)) -> impl Strategy { + let lon = prop_oneof![3 => w..=e, 1 => prop::sample::select(vec![w, e, (w + e) / 2.0])]; + let lat = prop_oneof![3 => s..=n, 1 => prop::sample::select(vec![s, n, (s + n) / 2.0])]; + (lon, lat).prop_map(|(x, y)| (Coord::new(fold(x), y), true)) +} + +/// A place anywhere, often on an edge of the globe: ±180°, the poles, Web Mercator's limit. +fn anywhere() -> impl Strategy { + let lon = prop_oneof![3 => -180.0..=180.0f64, 1 => prop::sample::select(vec![-180.0, 180.0, 0.0, 179.999_999, -90.0, 90.0])]; + let lat = prop_oneof![3 => -90.0..=90.0f64, 1 => prop::sample::select(vec![-90.0, 90.0, 0.0, 85.06, -85.06, 89.999_999])]; + (lon, lat).prop_map(|(x, y)| (Coord::new(x, y), false)) +} + +/// A system (its index in [`SYSTEMS`]) and a place, inside its area of use or anywhere. +fn case() -> impl Strategy { + (0..SYSTEMS.len()).prop_flat_map(|i| { + let area = SYSTEMS.get(i).map_or((-180.0, 180.0, -90.0, 90.0), |s| s.1); + prop_oneof![3 => inside(area), 1 => anywhere()].prop_map(move |(c, ins)| (i, c, ins)) + }) +} + +/// Cases that went there and back, and places that were refused. +static TRIPS: AtomicUsize = AtomicUsize::new(0); +static REFUSED: AtomicUsize = AtomicUsize::new(0); + +/// Takes a place there and back: how far (metres) it came back from where it was, or `None` when +/// it was refused. +fn trip(t: &Transformer, p: Coord) -> Result, String> { + let Some(q) = t.from_lonlat(p) else { return Ok(None) }; + let Some(back) = t.to_lonlat(q) else { return Err(format!("{p:?} went to {q:?}, which didn't come back")) }; + let off = wgs84_distance_m(p, back); + if off.is_nan() { + return Err(format!("{p:?} went to {q:?} and came back at {back:?}")); + } + Ok(Some(off)) +} + +#[test] +fn a_place_taken_into_a_coordinate_system_comes_back() { + let transformers: Vec = SYSTEMS + .iter() + .map(|(text, _)| Crs::parse(text).and_then(|c| c.transformer()).map_err(|e| format!("{text}: {e}")).unwrap()) + .collect(); + let seed = env("PROPERTY_SEED", 1); + let mut bytes = [0u8; 32]; + bytes[..8].copy_from_slice(&seed.to_le_bytes()); + let cases = u32::try_from(env("PROPERTY_CASES", 20_000)).unwrap_or(20_000); + let config = Config { cases, failure_persistence: None, ..Config::default() }; + let mut runner = TestRunner::new_with_rng(config, TestRng::from_seed(RngAlgorithm::ChaCha, &bytes)); + let result = runner.run(&case(), |(i, p, inside)| { + let (Some((name, _)), Some(t)) = (SYSTEMS.get(i), transformers.get(i)) else { return Ok(()) }; + let limit = LIMITS.iter().find(|(s, _, anywhere, _)| s == name && (*anywhere || !inside)); + let allowed = limit.map_or(TOLERANCE_M, |l| l.1.max(TOLERANCE_M)); + let edge = (p.x.abs() > 179.99 || p.y.abs() > 89.99) && OUTLINE_REFUSALS.iter().any(|(s, _)| s == name); + let trip = match trip(t, p) { + Err(_) if edge => None, + other => other.map_err(|e| TestCaseError::fail(format!("{name}: {e}")))?, + }; + match trip { + None if inside && !edge => Err(TestCaseError::fail(format!("{name}: {p:?} is in the area of use but was refused"))), + None => { + REFUSED.fetch_add(1, Ordering::Relaxed); + Ok(()) + } + Some(off) if off > allowed => Err(TestCaseError::fail(format!("{name}: {p:?} came back {off:.3e} m away"))), + Some(_) => { + TRIPS.fetch_add(1, Ordering::Relaxed); + Ok(()) + } + } + }); + eprintln!("{} places there and back, {} refused", TRIPS.load(Ordering::Relaxed), REFUSED.load(Ordering::Relaxed)); + if let Err(e) = result { + panic!("PROPERTY_SEED={seed}: {e}"); + } +} diff --git a/crates/geom/Cargo.toml b/crates/geom/Cargo.toml index db9fb25c62..cdfc48d4e4 100644 --- a/crates/geom/Cargo.toml +++ b/crates/geom/Cargo.toml @@ -11,3 +11,6 @@ workspace = true [dependencies] geographiclib-rs = { workspace = true } serde = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/geom/tests/geodesic_identities.rs b/crates/geom/tests/geodesic_identities.rs new file mode 100644 index 0000000000..75c039be1a --- /dev/null +++ b/crates/geom/tests/geodesic_identities.rs @@ -0,0 +1,184 @@ +//! Geodesic measurement, as properties. A distance is the same both ways, zero only between two +//! names of one place (±180°, a pole at any longitude), and never longer than a detour through a +//! third point. A ring's perimeter and area don't depend on the vertex it starts from, its +//! direction, a closing vertex or a vertex written twice. Points lean on the poles and the +//! antimeridian; rings include ones across the antimeridian, around a pole, globe-wide bands, caps +//! closed along the pole and pieces of a polygon split at ±180°. +//! +//! Pull requests run a fixed seed; the nightly a new one with more cases. A failure gives its seed +//! and the smallest input found. To run a seed again, or deeper: +//! `PROPERTY_SEED=7 PROPERTY_CASES=50000 cargo test -p rustgis-geom --test geodesic_identities`. +use proptest::prelude::*; +use proptest::test_runner::{Config, RngAlgorithm, TestCaseError, TestRng, TestRunner}; +use rustgis_geom::{Coord, geodesic_ring_area_m2, geodesic_ring_perimeter_m, haversine_m, wgs84_distance_m, wgs84_ring_perimeter_area_m2}; + +/// A number from the environment, else `default`. +fn env(name: &str, default: u64) -> u64 { + std::env::var(name).ok().and_then(|v| v.parse().ok()).unwrap_or(default) +} + +/// A runner seeded from `PROPERTY_SEED`, running `PROPERTY_CASES` cases (else `default`). +fn runner(default: u32) -> (u64, TestRunner) { + let seed = env("PROPERTY_SEED", 1); + let mut bytes = [0u8; 32]; + bytes[..8].copy_from_slice(&seed.to_le_bytes()); + let cases = u32::try_from(env("PROPERTY_CASES", default.into())).unwrap_or(default); + let config = Config { cases, failure_persistence: None, ..Config::default() }; + (seed, TestRunner::new_with_rng(config, TestRng::from_seed(RngAlgorithm::ChaCha, &bytes))) +} + +/// A longitude/latitude, often on an edge: ±180°, the poles, the equator. +fn coord() -> impl Strategy { + let lon = prop_oneof![3 => -180.0..=180.0f64, 1 => prop::sample::select(vec![-180.0, 180.0, 0.0, 90.0, 179.999_999])]; + let lat = prop_oneof![3 => -90.0..=90.0f64, 1 => prop::sample::select(vec![-90.0, 90.0, 0.0, 89.999_999, -45.0])]; + (lon, lat).prop_map(|(x, y)| Coord::new(x, y)) +} + +/// How far (metres) a distance may be from each identity, as (measure, a part of the distance, +/// why): what its formula can't hold, not a mistake in it. +const ROUNDING: &[(&str, f64, &str)] = &[ + ("WGS 84", 1e-12, "GeographicLib solves to a few nanometres"), + // h = sin²(Δφ/2) + … is near 1 there, and asin(√h) has lost half of h's digits: about + // R·√ε, a decimetre in 20,000 km. + ("sphere", 2e-8, "the haversine formula loses half its digits between points nearly opposite"), +]; + +/// Whether two coordinates name one place: the same latitude, and the same longitude but for +/// whole turns, or any longitude at a pole. +fn same_place(a: Coord, b: Coord) -> bool { + a.y == b.y && (a.y.abs() == 90.0 || (a.x - b.x).rem_euclid(360.0) == 0.0) +} + +#[test] +fn a_distance_is_symmetric_zero_only_at_one_place_and_no_longer_than_a_detour() { + let (seed, mut runner) = runner(20_000); + let result = runner.run(&(coord(), coord(), coord()), |(a, b, c)| { + for ((name, part, _), d) in ROUNDING.iter().zip([wgs84_distance_m as fn(Coord, Coord) -> f64, haversine_m]) { + let (ab, ba, ac, bc) = (d(a, b), d(b, a), d(a, c), d(b, c)); + let tolerance = 1e-6 + part * ab.max(ac).max(bc); + if (ab - ba).abs() > tolerance { + return Err(TestCaseError::fail(format!("{name}: {ab} m from {a:?} to {b:?}, {ba} m back"))); + } + // cos 90° isn't quite 0 in floating point: two longitudes at a pole are a nanometre apart. + if d(a, a) != 0.0 || if same_place(a, b) { ab > 1e-6 } else { ab <= 0.0 } { + return Err(TestCaseError::fail(format!("{name}: {ab} m from {a:?} to {b:?}, the same place: {}", same_place(a, b)))); + } + if ac > ab + bc + tolerance { + return Err(TestCaseError::fail(format!("{name}: {ac} m from {a:?} to {c:?}, {} m through {b:?}", ab + bc))); + } + } + Ok(()) + }); + if let Err(e) = result { + panic!("PROPERTY_SEED={seed}: {e}"); + } +} + +/// A longitude folded into −180..180, as a file writes it. +fn fold(lon: f64) -> f64 { + (lon + 180.0).rem_euclid(360.0) - 180.0 +} + +/// Three to seven vertices around a centre, in order of angle, folded at the antimeridian: small +/// and large rings, rings across ±180° and, centred near a pole, rings around it. +fn star() -> impl Strategy> { + let centre = prop_oneof![3 => (-180.0..180.0f64, -80.0..80.0f64), 1 => (prop::sample::select(vec![179.9, -180.0, 0.0]), prop::sample::select(vec![89.0, -89.5, 0.0, 60.0]))]; + let spokes = prop::collection::vec((0.0..1.0f64, 0.2..1.0f64), 3..8); + (centre, prop_oneof![1e-4..0.01f64, 0.5..20.0f64, 20.0..60.0f64], spokes).prop_map(|((x, y), r, mut spokes)| { + spokes.sort_by(|a, b| a.0.total_cmp(&b.0)); + spokes + .iter() + .map(|(a, k)| { + let a = a * std::f64::consts::TAU; + Coord::new(fold(x + r * k * a.cos()), (y + r * k * a.sin()).clamp(-90.0, 90.0)) + }) + .collect() + }) +} + +/// A globe-wide band between two parallels: down the −180° meridian, round the parallel and up +/// the 180° meridian, with vertices sometimes part way along the meridians. +fn band() -> impl Strategy> { + (-89.0..0.0f64, 0.5..89.0f64, any::()).prop_map(|(s, n, mid)| { + let m = (s + n) / 2.0; + let west: Vec = if mid { vec![n, m, s] } else { vec![n, s] }; + let mut ring: Vec = west.iter().map(|&y| Coord::new(-180.0, y)).collect(); + ring.extend(west.iter().rev().map(|&y| Coord::new(180.0, y))); + ring + }) +} + +/// A cap around a pole closed along it: a coastline from −180° to 180°, then the pole. +fn cap() -> impl Strategy> { + (prop::collection::vec(-80.0..80.0f64, 2..6), any::()).prop_map(|(lats, north)| { + let n = lats.len() - 1; + let mut ring: Vec = lats.iter().enumerate().map(|(i, &y)| Coord::new(-180.0 + 360.0 * i as f64 / n as f64, y)).collect(); + let pole = if north { 90.0 } else { -90.0 }; + ring.extend([Coord::new(180.0, pole), Coord::new(-180.0, pole)]); + if !north { + ring.reverse(); + } + ring + }) +} + +/// A piece of a polygon split at the antimeridian, along ±180°, or a cell across it written with +/// vertices on both 180° and −180°. +fn seam() -> impl Strategy> { + (-80.0..0.0f64, 0.1..80.0f64, 0.01..20.0f64, any::(), any::()).prop_map(|(s, n, w, east, cell)| { + if cell { + let c = |x: f64, y| Coord::new(x, y); + vec![c(180.0 - w, s), c(180.0, s), c(-180.0, s), c(-180.0 + w, s), c(-180.0 + w, n), c(-180.0, n), c(180.0, n), c(180.0 - w, n)] + } else { + let (e, x) = if east { (180.0, 180.0 - w) } else { (-180.0, -180.0 + w) }; + vec![Coord::new(e, s), Coord::new(x, s), Coord::new(x, n), Coord::new(e, n)] + } + }) +} + +/// A ring, without its closing vertex. +fn ring() -> impl Strategy> { + prop_oneof![4 => star(), 1 => band(), 1 => cap(), 1 => seam()] +} + +/// The perimeter and area of a ring on the ellipsoid and on the sphere. +fn measures(ring: &[Coord]) -> [(&'static str, f64); 4] { + let (p, a) = wgs84_ring_perimeter_area_m2(ring); + [ + ("WGS 84 perimeter", p), + ("WGS 84 area", a), + ("sphere perimeter", geodesic_ring_perimeter_m(ring)), + ("sphere area", geodesic_ring_area_m2(ring)), + ] +} + +/// Whether two measures agree: both NaN, or equal to a part in 10⁹ and a square metre. +fn agree(a: f64, b: f64) -> bool { + (a.is_nan() && b.is_nan()) || (a - b).abs() <= 1e-9 * a.abs().max(b.abs()) + 1.0 +} + +#[test] +fn a_ring_measures_the_same_from_any_vertex_either_way_round() { + let (seed, mut runner) = runner(10_000); + let result = runner.run(&(ring(), any::()), |(ring, twice)| { + let want = measures(&ring); + let n = ring.len(); + let mut variants: Vec<(String, Vec)> = + (1..n).map(|k| (format!("starting at vertex {k}"), [&ring[k..], &ring[..k]].concat())).collect(); + variants.push(("reversed".into(), ring.iter().rev().copied().collect())); + variants.push(("closed".into(), [&ring[..], &ring[..1]].concat())); + let i = twice.index(n); + variants.push((format!("with vertex {i} twice"), [&ring[..=i], &ring[i..]].concat())); + for (how, other) in variants { + for ((name, a), (_, b)) in want.iter().zip(measures(&other)) { + if !agree(*a, b) { + return Err(TestCaseError::fail(format!("{name} of {ring:?}: {a}, {how} {b}"))); + } + } + } + Ok(()) + }); + if let Err(e) = result { + panic!("PROPERTY_SEED={seed}: {e}"); + } +} From f21b598649d5b014b1f1d769f2149a993c6e9187 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 01:45:03 -0600 Subject: [PATCH 02/13] test(data): Eckert IV's fixed inverse needs no allowance; Robinson's row overlap does, as PROJ's Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/reprojection_round_trip.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/crates/data/tests/reprojection_round_trip.rs b/crates/data/tests/reprojection_round_trip.rs index 0313a57ed1..e93a18c784 100644 --- a/crates/data/tests/reprojection_round_trip.rs +++ b/crates/data/tests/reprojection_round_trip.rs @@ -50,8 +50,7 @@ const SYSTEMS: &[(&str, (f64, f64, f64, f64))] = &[ /// Where a system doesn't bring a place back to within [`TOLERANCE_M`], as (system, how far in /// metres a place may come back from where it was, whether inside its area of use too or only -/// outside it, why). Each but Eckert IV's (RustGIS's own) is PROJ's behaviour too, checked with -/// PROJ 9.8.1. +/// outside it, why). Each is PROJ's behaviour too, checked with PROJ 9.8.1. const LIMITS: &[(&str, f64, bool, &str)] = &[ ("EPSG:3857", f64::INFINITY, false, "a latitude past 85.05° is taken as 85.05°, where Web Mercator ends; an export says so"), // bug: transverse Mercator takes places up to 150° from its central meridian (as PROJ does), @@ -65,11 +64,10 @@ const LIMITS: &[(&str, f64, bool, &str)] = &[ // there: about R·√ε, a decimetre. ("EPSG:3035", 0.2, false, "the authalic latitude rounds within a ten-thousandth of a degree of a pole"), ("EPSG:6933", 0.2, false, "the authalic latitude rounds within a ten-thousandth of a degree of a pole"), - // Robinson's inverse finds the latitude to 2e-6° (`projection_methods.rs`). - ("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs", 0.25, true, "Robinson's inverse settles to 2e-6°"), - // bug: Eckert IV's inverse (`rustgis_geom::world`) stops where the residual is under 1e-9 of - // the radius, and y hardly changes with latitude near a pole: metres there. - ("+proj=eck4 +lon_0=0 +datum=WGS84 +units=m +no_defs", 10.0, true, "Eckert IV's inverse stops short near a pole"), + // Robinson's curve between its table rows (every 5°) is in pieces whose single-precision + // coefficients don't quite meet: at 10°, 35°, 45°, 65° and 90° a band of y up to 2.13 m wide is + // reached from both sides of the row, and the inverse takes the side above. As PROJ's. + ("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs", 2.5, true, "Robinson's pieces overlap at some table rows"), // bug: Albers' inverse (proj4rs's, as PROJ's) snaps a latitude within about 0.02° of a pole // onto it. ("EPSG:5070", 5_000.0, false, "Albers takes a place near a pole as the pole"), @@ -86,9 +84,6 @@ const OUTLINE_REFUSALS: &[(&str, &str)] = &[ // bug: Robinson's inverse refuses a longitude its latitude's rounding puts past ±180°, so a // world Robinson file loses every shape that touches the map's edge. ("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs", "Robinson's inverse refuses the map's edge"), - // bug: Eckert IV's inverse (`rustgis_geom::world`) refuses the pole line away from the - // central meridian. - ("+proj=eck4 +lon_0=0 +datum=WGS84 +units=m +no_defs", "Eckert IV's inverse refuses the pole line"), ]; /// A number from the environment, else `default`. From 6fcd078207e72e0f6224ee85a0994d5dfaa05bca Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 12 Nov 2026 17:44:30 -0600 Subject: [PATCH 03/13] test(data): an extent read equals the whole read filtered, as a property Co-Authored-By: Claude Opus 5.5 --- .github/workflows/nightly.yml | 1 + crates/data/tests/extent_query_properties.rs | 202 +++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 crates/data/tests/extent_query_properties.rs diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 3d09045016..5f49377c7b 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -122,6 +122,7 @@ jobs: cargo test --locked -p rustgis-engine --test never_silent_roundtrip cargo test --locked -p rustgis-data --test reprojection_round_trip cargo test --locked -p rustgis-geom --test geodesic_identities + cargo test --locked -p rustgis-data --test extent_query_properties report: name: Report diff --git a/crates/data/tests/extent_query_properties.rs b/crates/data/tests/extent_query_properties.rs new file mode 100644 index 0000000000..d1e097b0b8 --- /dev/null +++ b/crates/data/tests/extent_query_properties.rs @@ -0,0 +1,202 @@ +//! A source asked for the features in an extent gives the features a whole read gives whose +//! geometry's box meets the extent (`Request`), as a property: through each provider's spatial +//! index (GeoPackage and Shapefile R-trees, FlatGeobuf's packed R-tree, GeoJSON's boxes) or its +//! scan, by `read` and by `visit_chunks`. What a provider may do differently is listed in +//! [`LIMITS`], with the reason. Layers and extents lean on edges: boxes that touch, degenerate +//! boxes, empty and inverted boxes, infinite boxes, coordinates that `f32` can't hold, empty layers. +//! +//! Pull requests run a fixed seed; the nightly a new one with more cases. A failure gives its seed +//! and the smallest layer and extent found. To run a seed again, or deeper: +//! `PROPERTY_SEED=7 PROPERTY_CASES=2000 cargo test -p rustgis-data --test extent_query_properties`. +#![cfg(not(target_arch = "wasm32"))] + +use proptest::prelude::*; +use proptest::test_runner::{Config, RngAlgorithm, TestCaseError, TestRng, TestRunner}; +use rustgis_data::source::{FeatureSource, MAX_LIMIT, Request, open_path}; +use rustgis_data::{Coord, Dataset, Extent, Geometry, GeometryType}; +use std::sync::atomic::{AtomicU64, Ordering}; + +const FORMATS: [&str; 5] = ["gpkg", "fgb", "shp", "parquet", "geojson"]; + +/// Differences a provider may have from the whole read filtered in memory, as (format, the start +/// of a difference from [`differences`], why). +const LIMITS: &[(&str, &str, &str)] = &[ + // bug: the Shapefile and GeoJSON indexes round the extent to the nearest `f32` and never test + // the feature's own box against it, so a box just outside the extent matches. + ("shp", "read gave a feature just outside", "the index compares boxes as `f32`"), + ("shp", "visit gave a feature just outside", "the index compares boxes as `f32`"), + ("geojson", "read gave a feature just outside", "the index compares boxes as `f32`"), +]; + +/// Whether `difference` is a limit of `ext` listed above. +fn declared(ext: &str, difference: &str) -> bool { + LIMITS.iter().any(|(f, start, _)| *f == ext && difference.starts_with(start)) +} + +/// A number from the environment, else `default`. +fn env(name: &str, default: u64) -> u64 { + std::env::var(name).ok().and_then(|v| v.parse().ok()).unwrap_or(default) +} + +/// Values that extents and geometries share, so boxes touch: whole degrees, the edges of the +/// world, and ones `f32` can't hold (0.1, a hair either side of 1). +const EDGES: [f64; 12] = [-180.0, -90.0, -1.0, 0.0, 0.1, 0.3, 1.0 - 1e-9, 1.0, 1.0 + 1e-9, 45.123_456_789, 90.0, 180.0]; + +/// A coordinate value, often one of [`EDGES`]. +fn number(range: f64) -> impl Strategy { + prop_oneof![2 => -range..range, 3 => prop::sample::select(EDGES.to_vec()).prop_map(move |v| v.clamp(-range, range))] +} + +fn coord() -> impl Strategy { + (number(180.0), number(90.0)).prop_map(|(x, y)| Coord::new(x, y)) +} + +/// A coordinate and another up to `size` from it, often on a whole degree, within the world. +fn pair(size: f64) -> impl Strategy { + let step = prop_oneof![2 => -size..size, 1 => (-3i32..4).prop_map(f64::from)]; + (coord(), step.clone(), step).prop_map(|(a, dx, dy)| (a, Coord::new((a.x + dx).clamp(-180.0, 180.0), (a.y + dy).clamp(-90.0, 90.0)))) +} + +/// A geometry of a layer of `kind`: points and multipoints, lines, rectangles. +fn geometry(kind: GeometryType) -> BoxedStrategy { + match kind { + GeometryType::Point => prop_oneof![ + 3 => coord().prop_map(Geometry::Point), + 1 => prop::collection::vec(coord(), 1..4).prop_map(Geometry::MultiPoint), + ] + .boxed(), + GeometryType::Polyline => prop::collection::vec(pair(10.0), 1..3) + .prop_map(|l| Geometry::Line(vec![l.into_iter().flat_map(|(a, b)| [a, b]).collect()])) + .boxed(), + _ => pair(20.0) + .prop_filter("a rectangle with an area", |(a, b)| a.x != b.x && a.y != b.y) + .prop_map(|(a, b)| { + let e = Extent::from_points(a, b); + let ring = [(e.min_x, e.min_y), (e.max_x, e.min_y), (e.max_x, e.max_y), (e.min_x, e.max_y), (e.min_x, e.min_y)]; + Geometry::Polygon(vec![vec![ring.iter().map(|&(x, y)| Coord::new(x, y)).collect()]]) + }) + .boxed(), + } +} + +/// A layer of up to 60 features, sometimes none, a few without geometry: enough for more than +/// one node of a packed R-tree. +fn layer() -> impl Strategy { + let kind = prop::sample::select(vec![GeometryType::Point, GeometryType::Polyline, GeometryType::Polygon]); + let size = prop_oneof![1 => Just(0usize), 6 => 1usize..61]; + (kind, size).prop_flat_map(|(kind, n)| { + prop::collection::vec(prop::option::weighted(0.95, geometry(kind)), n).prop_map(move |gs| { + let mut ds = Dataset::new("layer", kind, vec![]); + for g in gs { + ds.push(g, vec![]); + } + ds + }) + }) +} + +/// An extent: one from two coordinates (touching the layer's on [`EDGES`]), a line or a point, +/// an inverted (empty) one, the world, or an infinite one. +fn extent() -> impl Strategy { + prop_oneof![ + 4 => pair(40.0).prop_map(|(a, b)| Extent::from_points(a, b)), + 2 => (coord(), coord()).prop_map(|(a, b)| Extent::from_points(a, b)), + 1 => coord().prop_map(|c| Extent::new(c.x, c.y, c.x, c.y)), + 1 => (coord(), number(90.0)).prop_map(|(c, y)| Extent::new(c.x, c.y.min(y), c.x, c.y.max(y))), + 1 => (coord(), coord()).prop_map(|(a, b)| Extent::new(a.x.max(b.x) + 1.0, a.y, a.x.min(b.x), b.y)), + 1 => Just(Extent::new(-180.0, -90.0, 180.0, 90.0)), + 1 => Just(Extent::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::INFINITY, f64::INFINITY)), + ] +} + +/// The ids of a read's features, sorted. +fn ids(fs: impl IntoIterator) -> Vec { + let mut v: Vec = fs.into_iter().collect(); + v.sort_unstable(); + v +} + +/// `q` grown by what rounding each side to the nearest `f32` may move it. +fn widened(q: &Extent) -> Extent { + let pad = |v: f64| v.abs() * f64::from(f32::EPSILON) + f64::from(f32::MIN_POSITIVE); + Extent::new(q.min_x - pad(q.min_x), q.min_y - pad(q.min_y), q.max_x + pad(q.max_x), q.max_y + pad(q.max_y)) +} + +/// How the features a source gave for `q` (`got`) differ from what the whole read filtered in +/// memory gives: features given whose box doesn't meet `q`, and features missed whose box does. +fn differences(all: &[rustgis_data::Feature], got: &[u64], q: &Extent, how: &str) -> Vec { + let want = ids(all.iter().filter(|f| f.geometry.as_ref().is_some_and(|g| g.extent().intersects(q))).map(|f| f.id)); + let mut out = Vec::new(); + for id in got.iter().filter(|id| !want.contains(id)) { + let g = all.iter().find(|f| f.id == *id).and_then(|f| f.geometry.as_ref()); + let near = g.is_some_and(|g| g.extent().intersects(&widened(q))); + let outside = if near { "just outside the extent, as an `f32`" } else { "outside the extent" }; + out.push(format!("{how} gave a feature {outside} (id {id}, box {:?})", g.map(|g| g.extent()))); + } + for id in want.iter().filter(|id| !got.contains(id)) { + let g = all.iter().find(|f| f.id == *id).and_then(|f| f.geometry.as_ref()); + out.push(format!("{how} missed a feature in the extent (id {id}, box {:?})", g.map(|g| g.extent()))); + } + out +} + +/// What `src` does differently for `q`, besides the limits. +fn check(src: &dyn FeatureSource, q: &Extent) -> Result<(Vec, usize, usize), String> { + let all = src.read(&Request::all()).map_err(|e| format!("whole read: {e}"))?; + let read = src.read(&Request { extent: Some(*q), limit: MAX_LIMIT }).map_err(|e| format!("read: {e}"))?; + let mut found = differences(&all.features, &ids(read.features.iter().map(|f| f.id)), q, "read"); + if !read.complete { + found.push("read was incomplete".into()); + } + if src.visits_chunks() { + let mut visited = Vec::new(); + src.visit_chunks(Some(*q), 1 << 20, &mut |fs| { + visited.extend(fs.into_iter().map(|f| f.id)); + Ok(()) + }) + .map_err(|e| format!("visit: {e}"))?; + found.extend(differences(&all.features, &ids(visited), q, "visit")); + } + Ok((found, read.features.len(), all.features.len())) +} + +#[test] +fn an_extent_read_gives_the_features_a_whole_read_filters_to() { + rustgis_data::source::shp::use_temporary_index_dir(); + let dir = tempfile::tempdir().unwrap(); + let case = AtomicU64::new(0); + // Cases where some formats' answer was neither all of the layer nor none of it. + let partial = AtomicU64::new(0); + let seed = env("PROPERTY_SEED", 1); + let mut bytes = [0u8; 32]; + bytes[..8].copy_from_slice(&seed.to_le_bytes()); + // About 20 ms a case. + let cases = u32::try_from(env("PROPERTY_CASES", 128)).unwrap_or(128); + let config = Config { cases, failure_persistence: None, ..Config::default() }; + let mut runner = TestRunner::new_with_rng(config, TestRng::from_seed(RngAlgorithm::ChaCha, &bytes)); + let result = runner.run(&(layer(), extent()), |(ds, q)| { + // A folder of its own for each case: a Shapefile's index is reused by path and stamp. + let sub = dir.path().join(case.fetch_add(1, Ordering::Relaxed).to_string()); + std::fs::create_dir_all(&sub).unwrap(); + let mut some = false; + for ext in FORMATS { + let path = sub.join(format!("layer.{ext}")); + if rustgis_data::io::write_path(&ds, &path).is_err() { + continue; + } + let src = open_path(&path, None).map_err(|e| TestCaseError::fail(format!("{ext}: open: {e}")))?; + let (found, n, total) = check(src.as_ref(), &q).map_err(|e| TestCaseError::fail(format!("{ext}: {e}")))?; + some |= n > 0 && n < total; + let wrong: Vec = found.into_iter().filter(|d| !declared(ext, d)).collect(); + if !wrong.is_empty() { + return Err(TestCaseError::fail(format!("{ext}: {wrong:?}"))); + } + } + partial.fetch_add(u64::from(some), Ordering::Relaxed); + Ok(()) + }); + eprintln!("{} of {} cases matched part of a layer", partial.load(Ordering::Relaxed), case.load(Ordering::Relaxed)); + if let Err(e) = result { + panic!("PROPERTY_SEED={seed}: {e}"); + } +} From 133ea2fcb84188f1179d437b963a752145f524b0 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 01:44:50 -0600 Subject: [PATCH 04/13] test(data): the extent property's allow-list is empty now that #1897 rechecks Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/extent_query_properties.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/data/tests/extent_query_properties.rs b/crates/data/tests/extent_query_properties.rs index d1e097b0b8..7fe86a704f 100644 --- a/crates/data/tests/extent_query_properties.rs +++ b/crates/data/tests/extent_query_properties.rs @@ -20,13 +20,7 @@ const FORMATS: [&str; 5] = ["gpkg", "fgb", "shp", "parquet", "geojson"]; /// Differences a provider may have from the whole read filtered in memory, as (format, the start /// of a difference from [`differences`], why). -const LIMITS: &[(&str, &str, &str)] = &[ - // bug: the Shapefile and GeoJSON indexes round the extent to the nearest `f32` and never test - // the feature's own box against it, so a box just outside the extent matches. - ("shp", "read gave a feature just outside", "the index compares boxes as `f32`"), - ("shp", "visit gave a feature just outside", "the index compares boxes as `f32`"), - ("geojson", "read gave a feature just outside", "the index compares boxes as `f32`"), -]; +const LIMITS: &[(&str, &str, &str)] = &[]; /// Whether `difference` is a limit of `ext` listed above. fn declared(ext: &str, difference: &str) -> bool { From 630848e46dacd488b01846f57beabc28ae4b766e Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 01:44:50 -0600 Subject: [PATCH 05/13] test(data): Eckert IV holds a pole to about a centimetre, as a Double does Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/reprojection_round_trip.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/data/tests/reprojection_round_trip.rs b/crates/data/tests/reprojection_round_trip.rs index e93a18c784..a0c4d519dd 100644 --- a/crates/data/tests/reprojection_round_trip.rs +++ b/crates/data/tests/reprojection_round_trip.rs @@ -64,6 +64,9 @@ const LIMITS: &[(&str, f64, bool, &str)] = &[ // there: about R·√ε, a decimetre. ("EPSG:3035", 0.2, false, "the authalic latitude rounds within a ten-thousandth of a degree of a pole"), ("EPSG:6933", 0.2, false, "the authalic latitude rounds within a ten-thousandth of a degree of a pole"), + // Eckert IV: within a millionth of a degree of a pole, y is a rounding step or two from the + // pole line's, which is about a centimetre on the ground (`rustgis_geom::world`, #1911). + ("+proj=eck4 +lon_0=0 +datum=WGS84 +units=m +no_defs", 0.05, true, "a Double holds y to about a centimetre near a pole"), // Robinson's curve between its table rows (every 5°) is in pieces whose single-precision // coefficients don't quite meet: at 10°, 35°, 45°, 65° and 90° a band of y up to 2.13 m wide is // reached from both sides of the row, and the inverse takes the side above. As PROJ's. From 5a0fb732631727f837f6d618fb898abe92b53c7c Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 01:55:08 -0600 Subject: [PATCH 06/13] test(data): the reprojection property's known bugs point to #1925 Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/reprojection_round_trip.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/data/tests/reprojection_round_trip.rs b/crates/data/tests/reprojection_round_trip.rs index a0c4d519dd..ce6ef3d0c1 100644 --- a/crates/data/tests/reprojection_round_trip.rs +++ b/crates/data/tests/reprojection_round_trip.rs @@ -3,7 +3,7 @@ //! centimetre on the ground. Inside a system's area of use a place must go there and back; outside //! it the system may refuse it (`None`), but a place it does take must come back, never somewhere //! else. What a system does instead is listed in [`LIMITS`] and [`OUTLINE_REFUSALS`] with the -//! reason; a known bug is marked `bug:`. Places lean on the areas' edges and corners, the poles +//! reason; a known bug is marked `bug:` (#1925). Places lean on the areas' edges and corners, the poles //! and the antimeridian. //! //! Pull requests run a fixed seed; the nightly a new one with more cases. A failure gives its seed From c7258347fa644aa27397d81d7203c377d930baf1 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 02:24:06 -0600 Subject: [PATCH 07/13] ci: the nightly runs more reprojection and geodesic cases than a pull request, not fewer Co-Authored-By: Claude Opus 5.5 --- .github/workflows/nightly.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 5f49377c7b..173bbcb5ed 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -120,8 +120,8 @@ jobs: PROPERTY_CASES: "2000" run: | cargo test --locked -p rustgis-engine --test never_silent_roundtrip - cargo test --locked -p rustgis-data --test reprojection_round_trip - cargo test --locked -p rustgis-geom --test geodesic_identities + PROPERTY_CASES=200000 cargo test --locked -p rustgis-data --test reprojection_round_trip + PROPERTY_CASES=100000 cargo test --locked -p rustgis-geom --test geodesic_identities cargo test --locked -p rustgis-data --test extent_query_properties report: From c8c29158da3f34b0b06b48fda11161f23c615bdf Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 02:58:56 -0600 Subject: [PATCH 08/13] test(data, geom): the properties catch duplicates, unwritten formats, NaN and wrong areas An extent read that gives a feature twice fails, and so does a format that writes none of the layers. A place is inside a system's area of use by where it is; only a refusal on the way back is excused at a world map's outline. Distances and measures must be finite. Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/extent_query_properties.rs | 11 ++++++++++- crates/data/tests/reprojection_round_trip.rs | 14 +++++++++++--- crates/geom/tests/geodesic_identities.rs | 8 ++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/crates/data/tests/extent_query_properties.rs b/crates/data/tests/extent_query_properties.rs index 7fe86a704f..a7d563f383 100644 --- a/crates/data/tests/extent_query_properties.rs +++ b/crates/data/tests/extent_query_properties.rs @@ -121,6 +121,9 @@ fn widened(q: &Extent) -> Extent { fn differences(all: &[rustgis_data::Feature], got: &[u64], q: &Extent, how: &str) -> Vec { let want = ids(all.iter().filter(|f| f.geometry.as_ref().is_some_and(|g| g.extent().intersects(q))).map(|f| f.id)); let mut out = Vec::new(); + for w in got.windows(2).filter(|w| w[0] == w[1]) { + out.push(format!("{how} gave a feature twice (id {})", w[0])); + } for id in got.iter().filter(|id| !want.contains(id)) { let g = all.iter().find(|f| f.id == *id).and_then(|f| f.geometry.as_ref()); let near = g.is_some_and(|g| g.extent().intersects(&widened(q))); @@ -168,16 +171,19 @@ fn an_extent_read_gives_the_features_a_whole_read_filters_to() { let cases = u32::try_from(env("PROPERTY_CASES", 128)).unwrap_or(128); let config = Config { cases, failure_persistence: None, ..Config::default() }; let mut runner = TestRunner::new_with_rng(config, TestRng::from_seed(RngAlgorithm::ChaCha, &bytes)); + // Layers each format wrote: one that refuses every layer checks nothing. + let written = std::cell::RefCell::new([0u32; FORMATS.len()]); let result = runner.run(&(layer(), extent()), |(ds, q)| { // A folder of its own for each case: a Shapefile's index is reused by path and stamp. let sub = dir.path().join(case.fetch_add(1, Ordering::Relaxed).to_string()); std::fs::create_dir_all(&sub).unwrap(); let mut some = false; - for ext in FORMATS { + for (k, ext) in FORMATS.into_iter().enumerate() { let path = sub.join(format!("layer.{ext}")); if rustgis_data::io::write_path(&ds, &path).is_err() { continue; } + written.borrow_mut()[k] += 1; let src = open_path(&path, None).map_err(|e| TestCaseError::fail(format!("{ext}: open: {e}")))?; let (found, n, total) = check(src.as_ref(), &q).map_err(|e| TestCaseError::fail(format!("{ext}: {e}")))?; some |= n > 0 && n < total; @@ -193,4 +199,7 @@ fn an_extent_read_gives_the_features_a_whole_read_filters_to() { if let Err(e) = result { panic!("PROPERTY_SEED={seed}: {e}"); } + for (ext, n) in FORMATS.iter().zip(written.into_inner()) { + assert!(n > 0, "PROPERTY_SEED={seed}: {ext} wrote none of the layers"); + } } diff --git a/crates/data/tests/reprojection_round_trip.rs b/crates/data/tests/reprojection_round_trip.rs index ce6ef3d0c1..e576e2b646 100644 --- a/crates/data/tests/reprojection_round_trip.rs +++ b/crates/data/tests/reprojection_round_trip.rs @@ -121,6 +121,11 @@ fn case() -> impl Strategy { }) } +/// Whether `p` is in the area of use `(w, e, s, n)`, whose east edge may be past 180°. +fn in_area((w, e, s, n): (f64, f64, f64, f64), p: Coord) -> bool { + (s..=n).contains(&p.y) && ((w..=e).contains(&p.x) || (w..=e).contains(&(p.x + 360.0))) +} + /// Cases that went there and back, and places that were refused. static TRIPS: AtomicUsize = AtomicUsize::new(0); static REFUSED: AtomicUsize = AtomicUsize::new(0); @@ -149,13 +154,16 @@ fn a_place_taken_into_a_coordinate_system_comes_back() { let cases = u32::try_from(env("PROPERTY_CASES", 20_000)).unwrap_or(20_000); let config = Config { cases, failure_persistence: None, ..Config::default() }; let mut runner = TestRunner::new_with_rng(config, TestRng::from_seed(RngAlgorithm::ChaCha, &bytes)); - let result = runner.run(&case(), |(i, p, inside)| { - let (Some((name, _)), Some(t)) = (SYSTEMS.get(i), transformers.get(i)) else { return Ok(()) }; + let result = runner.run(&case(), |(i, p, _)| { + let (Some((name, area)), Some(t)) = (SYSTEMS.get(i), transformers.get(i)) else { return Ok(()) }; + // Inside the area of use by where the place is, whichever way it was made. + let inside = in_area(*area, p); let limit = LIMITS.iter().find(|(s, _, anywhere, _)| s == name && (*anywhere || !inside)); let allowed = limit.map_or(TOLERANCE_M, |l| l.1.max(TOLERANCE_M)); let edge = (p.x.abs() > 179.99 || p.y.abs() > 89.99) && OUTLINE_REFUSALS.iter().any(|(s, _)| s == name); let trip = match trip(t, p) { - Err(_) if edge => None, + // Only a refusal on the way back, not a place that comes back wrong. + Err(e) if edge && e.contains("didn't come back") => None, other => other.map_err(|e| TestCaseError::fail(format!("{name}: {e}")))?, }; match trip { diff --git a/crates/geom/tests/geodesic_identities.rs b/crates/geom/tests/geodesic_identities.rs index 75c039be1a..97c6161c5c 100644 --- a/crates/geom/tests/geodesic_identities.rs +++ b/crates/geom/tests/geodesic_identities.rs @@ -55,6 +55,10 @@ fn a_distance_is_symmetric_zero_only_at_one_place_and_no_longer_than_a_detour() let result = runner.run(&(coord(), coord(), coord()), |(a, b, c)| { for ((name, part, _), d) in ROUNDING.iter().zip([wgs84_distance_m as fn(Coord, Coord) -> f64, haversine_m]) { let (ab, ba, ac, bc) = (d(a, b), d(b, a), d(a, c), d(b, c)); + // NaN fails no comparison below, so a distance that isn't a number fails here. + if ![ab, ba, ac, bc].iter().all(|d| d.is_finite()) { + return Err(TestCaseError::fail(format!("{name}: {ab}, {ba}, {ac}, {bc} m among {a:?}, {b:?}, {c:?}"))); + } let tolerance = 1e-6 + part * ab.max(ac).max(bc); if (ab - ba).abs() > tolerance { return Err(TestCaseError::fail(format!("{name}: {ab} m from {a:?} to {b:?}, {ba} m back"))); @@ -152,9 +156,9 @@ fn measures(ring: &[Coord]) -> [(&'static str, f64); 4] { ] } -/// Whether two measures agree: both NaN, or equal to a part in 10⁹ and a square metre. +/// Whether two measures agree: both NaN, or finite and equal to a part in 10⁹ and a square metre. fn agree(a: f64, b: f64) -> bool { - (a.is_nan() && b.is_nan()) || (a - b).abs() <= 1e-9 * a.abs().max(b.abs()) + 1.0 + (a.is_nan() && b.is_nan()) || a.is_finite() && b.is_finite() && (a - b).abs() <= 1e-9 * a.abs().max(b.abs()) + 1.0 } #[test] From abdb749f14094134f43eedc041e2ee276c337000 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 03:32:52 -0600 Subject: [PATCH 09/13] test(data): the extent property's chunked reads come in several chunks Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/extent_query_properties.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/data/tests/extent_query_properties.rs b/crates/data/tests/extent_query_properties.rs index a7d563f383..2332b103d2 100644 --- a/crates/data/tests/extent_query_properties.rs +++ b/crates/data/tests/extent_query_properties.rs @@ -147,7 +147,8 @@ fn check(src: &dyn FeatureSource, q: &Extent) -> Result<(Vec, usize, usi } if src.visits_chunks() { let mut visited = Vec::new(); - src.visit_chunks(Some(*q), 1 << 20, &mut |fs| { + // A small budget, so a layer comes in several chunks. + src.visit_chunks(Some(*q), 512, &mut |fs| { visited.extend(fs.into_iter().map(|f| f.id)); Ok(()) }) From abbf085b7d48472979142c8dd2e0be4553662912 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 04:04:47 -0600 Subject: [PATCH 10/13] =?UTF-8?q?test(data,=20geom):=20ring=20measures=20a?= =?UTF-8?q?re=20finite,=20writes=20count=20by=20kind,=20and=20=C2=B1180?= =?UTF-8?q?=C2=B0=20is=20in=20either=20area?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/extent_query_properties.rs | 14 ++++++++++---- crates/data/tests/reprojection_round_trip.rs | 10 +++++++++- crates/geom/tests/geodesic_identities.rs | 8 ++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/crates/data/tests/extent_query_properties.rs b/crates/data/tests/extent_query_properties.rs index 2332b103d2..af2d76401f 100644 --- a/crates/data/tests/extent_query_properties.rs +++ b/crates/data/tests/extent_query_properties.rs @@ -172,8 +172,9 @@ fn an_extent_read_gives_the_features_a_whole_read_filters_to() { let cases = u32::try_from(env("PROPERTY_CASES", 128)).unwrap_or(128); let config = Config { cases, failure_persistence: None, ..Config::default() }; let mut runner = TestRunner::new_with_rng(config, TestRng::from_seed(RngAlgorithm::ChaCha, &bytes)); - // Layers each format wrote: one that refuses every layer checks nothing. - let written = std::cell::RefCell::new([0u32; FORMATS.len()]); + // Layers each format wrote, by kind (points, lines, polygons): one that refuses every layer + // of a kind checks nothing for it. + let written = std::cell::RefCell::new([[0u32; 3]; FORMATS.len()]); let result = runner.run(&(layer(), extent()), |(ds, q)| { // A folder of its own for each case: a Shapefile's index is reused by path and stamp. let sub = dir.path().join(case.fetch_add(1, Ordering::Relaxed).to_string()); @@ -184,7 +185,12 @@ fn an_extent_read_gives_the_features_a_whole_read_filters_to() { if rustgis_data::io::write_path(&ds, &path).is_err() { continue; } - written.borrow_mut()[k] += 1; + let kind = match ds.geometry_type { + GeometryType::Point => 0, + GeometryType::Polyline => 1, + _ => 2, + }; + written.borrow_mut()[k][kind] += 1; let src = open_path(&path, None).map_err(|e| TestCaseError::fail(format!("{ext}: open: {e}")))?; let (found, n, total) = check(src.as_ref(), &q).map_err(|e| TestCaseError::fail(format!("{ext}: {e}")))?; some |= n > 0 && n < total; @@ -201,6 +207,6 @@ fn an_extent_read_gives_the_features_a_whole_read_filters_to() { panic!("PROPERTY_SEED={seed}: {e}"); } for (ext, n) in FORMATS.iter().zip(written.into_inner()) { - assert!(n > 0, "PROPERTY_SEED={seed}: {ext} wrote none of the layers"); + assert!(n.iter().all(|n| *n > 0), "PROPERTY_SEED={seed}: {ext} wrote no layer of some kind (points, lines, polygons): {n:?}"); } } diff --git a/crates/data/tests/reprojection_round_trip.rs b/crates/data/tests/reprojection_round_trip.rs index e576e2b646..c8d52aaded 100644 --- a/crates/data/tests/reprojection_round_trip.rs +++ b/crates/data/tests/reprojection_round_trip.rs @@ -123,7 +123,15 @@ fn case() -> impl Strategy { /// Whether `p` is in the area of use `(w, e, s, n)`, whose east edge may be past 180°. fn in_area((w, e, s, n): (f64, f64, f64, f64), p: Coord) -> bool { - (s..=n).contains(&p.y) && ((w..=e).contains(&p.x) || (w..=e).contains(&(p.x + 360.0))) + (s..=n).contains(&p.y) && [p.x - 360.0, p.x, p.x + 360.0].iter().any(|x| (w..=e).contains(x)) +} + +#[test] +fn a_place_on_the_antimeridian_is_in_an_area_on_either_side() { + let zone_1 = (-180.0, -174.0, 0.0, 84.0); + assert!(in_area(zone_1, Coord::new(180.0, 45.0)) && in_area(zone_1, Coord::new(-180.0, 45.0))); + assert!(in_area((174.0, 180.0, -80.0, 0.0), Coord::new(-180.0, -45.0))); + assert!(!in_area(zone_1, Coord::new(179.0, 45.0))); } /// Cases that went there and back, and places that were refused. diff --git a/crates/geom/tests/geodesic_identities.rs b/crates/geom/tests/geodesic_identities.rs index 97c6161c5c..58b85d1d99 100644 --- a/crates/geom/tests/geodesic_identities.rs +++ b/crates/geom/tests/geodesic_identities.rs @@ -156,9 +156,9 @@ fn measures(ring: &[Coord]) -> [(&'static str, f64); 4] { ] } -/// Whether two measures agree: both NaN, or finite and equal to a part in 10⁹ and a square metre. +/// Whether two measures agree: finite and equal to a part in 10⁹ and a square metre. fn agree(a: f64, b: f64) -> bool { - (a.is_nan() && b.is_nan()) || a.is_finite() && b.is_finite() && (a - b).abs() <= 1e-9 * a.abs().max(b.abs()) + 1.0 + a.is_finite() && b.is_finite() && (a - b).abs() <= 1e-9 * a.abs().max(b.abs()) + 1.0 } #[test] @@ -166,6 +166,10 @@ fn a_ring_measures_the_same_from_any_vertex_either_way_round() { let (seed, mut runner) = runner(10_000); let result = runner.run(&(ring(), any::()), |(ring, twice)| { let want = measures(&ring); + // The rings made here are never the ambiguous ones (through both poles) measured as NaN. + if let Some((name, m)) = want.iter().find(|(_, m)| !m.is_finite()) { + return Err(TestCaseError::fail(format!("{name} of {ring:?} is {m}"))); + } let n = ring.len(); let mut variants: Vec<(String, Vec)> = (1..n).map(|k| (format!("starting at vertex {k}"), [&ring[k..], &ring[..k]].concat())).collect(); From 7afa9e5e70ad4c3a21a798c5c987f089cbc3e79c Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 04:33:42 -0600 Subject: [PATCH 11/13] test(data): the extent property fails if no chunked read comes in more than one chunk Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/extent_query_properties.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/data/tests/extent_query_properties.rs b/crates/data/tests/extent_query_properties.rs index af2d76401f..84ce730055 100644 --- a/crates/data/tests/extent_query_properties.rs +++ b/crates/data/tests/extent_query_properties.rs @@ -137,6 +137,9 @@ fn differences(all: &[rustgis_data::Feature], got: &[u64], q: &Extent, how: &str out } +/// Chunked reads that came in more than one chunk: none would leave chunk boundaries unchecked. +static SPLIT: AtomicU64 = AtomicU64::new(0); + /// What `src` does differently for `q`, besides the limits. fn check(src: &dyn FeatureSource, q: &Extent) -> Result<(Vec, usize, usize), String> { let all = src.read(&Request::all()).map_err(|e| format!("whole read: {e}"))?; @@ -146,13 +149,17 @@ fn check(src: &dyn FeatureSource, q: &Extent) -> Result<(Vec, usize, usi found.push("read was incomplete".into()); } if src.visits_chunks() { - let mut visited = Vec::new(); + let (mut visited, mut chunks) = (Vec::new(), 0); // A small budget, so a layer comes in several chunks. src.visit_chunks(Some(*q), 512, &mut |fs| { + chunks += 1; visited.extend(fs.into_iter().map(|f| f.id)); Ok(()) }) .map_err(|e| format!("visit: {e}"))?; + if chunks > 1 { + SPLIT.fetch_add(1, Ordering::Relaxed); + } found.extend(differences(&all.features, &ids(visited), q, "visit")); } Ok((found, read.features.len(), all.features.len())) @@ -206,6 +213,7 @@ fn an_extent_read_gives_the_features_a_whole_read_filters_to() { if let Err(e) = result { panic!("PROPERTY_SEED={seed}: {e}"); } + assert!(SPLIT.load(Ordering::Relaxed) > 0, "PROPERTY_SEED={seed}: no chunked read came in more than one chunk"); for (ext, n) in FORMATS.iter().zip(written.into_inner()) { assert!(n.iter().all(|n| *n > 0), "PROPERTY_SEED={seed}: {ext} wrote no layer of some kind (points, lines, polygons): {n:?}"); } From c190eccd266a79963fb0f7b937dffb521a37be82 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 04:58:58 -0600 Subject: [PATCH 12/13] test(data): each chunked read is checked for chunks, and only kinds made must be written A one-byte budget puts each feature in a chunk of its own, so a read of two or more that comes in one chunk fails its case. A small run need not make every kind of layer: only the kinds made, with something to draw, must be written by every format. Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/extent_query_properties.rs | 32 +++++++++++--------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/crates/data/tests/extent_query_properties.rs b/crates/data/tests/extent_query_properties.rs index 84ce730055..a822ee9bce 100644 --- a/crates/data/tests/extent_query_properties.rs +++ b/crates/data/tests/extent_query_properties.rs @@ -137,9 +137,6 @@ fn differences(all: &[rustgis_data::Feature], got: &[u64], q: &Extent, how: &str out } -/// Chunked reads that came in more than one chunk: none would leave chunk boundaries unchecked. -static SPLIT: AtomicU64 = AtomicU64::new(0); - /// What `src` does differently for `q`, besides the limits. fn check(src: &dyn FeatureSource, q: &Extent) -> Result<(Vec, usize, usize), String> { let all = src.read(&Request::all()).map_err(|e| format!("whole read: {e}"))?; @@ -150,15 +147,15 @@ fn check(src: &dyn FeatureSource, q: &Extent) -> Result<(Vec, usize, usi } if src.visits_chunks() { let (mut visited, mut chunks) = (Vec::new(), 0); - // A small budget, so a layer comes in several chunks. - src.visit_chunks(Some(*q), 512, &mut |fs| { + // A budget no feature fits in, so each comes in a chunk of its own. + src.visit_chunks(Some(*q), 1, &mut |fs| { chunks += 1; visited.extend(fs.into_iter().map(|f| f.id)); Ok(()) }) .map_err(|e| format!("visit: {e}"))?; - if chunks > 1 { - SPLIT.fetch_add(1, Ordering::Relaxed); + if visited.len() >= 2 && chunks < 2 { + found.push(format!("visit gave {} features in one chunk of a byte", visited.len())); } found.extend(differences(&all.features, &ids(visited), q, "visit")); } @@ -182,21 +179,27 @@ fn an_extent_read_gives_the_features_a_whole_read_filters_to() { // Layers each format wrote, by kind (points, lines, polygons): one that refuses every layer // of a kind checks nothing for it. let written = std::cell::RefCell::new([[0u32; 3]; FORMATS.len()]); + // Kinds of layer made: a small run may not make all three. + let made = std::cell::RefCell::new([false; 3]); let result = runner.run(&(layer(), extent()), |(ds, q)| { // A folder of its own for each case: a Shapefile's index is reused by path and stamp. let sub = dir.path().join(case.fetch_add(1, Ordering::Relaxed).to_string()); std::fs::create_dir_all(&sub).unwrap(); let mut some = false; + let kind = match ds.geometry_type { + GeometryType::Point => 0, + GeometryType::Polyline => 1, + _ => 2, + }; + // A format may refuse a layer with nothing to draw (a Shapefile does). + if ds.features.iter().any(|f| f.geometry.is_some()) { + made.borrow_mut()[kind] = true; + } for (k, ext) in FORMATS.into_iter().enumerate() { let path = sub.join(format!("layer.{ext}")); if rustgis_data::io::write_path(&ds, &path).is_err() { continue; } - let kind = match ds.geometry_type { - GeometryType::Point => 0, - GeometryType::Polyline => 1, - _ => 2, - }; written.borrow_mut()[k][kind] += 1; let src = open_path(&path, None).map_err(|e| TestCaseError::fail(format!("{ext}: open: {e}")))?; let (found, n, total) = check(src.as_ref(), &q).map_err(|e| TestCaseError::fail(format!("{ext}: {e}")))?; @@ -213,8 +216,9 @@ fn an_extent_read_gives_the_features_a_whole_read_filters_to() { if let Err(e) = result { panic!("PROPERTY_SEED={seed}: {e}"); } - assert!(SPLIT.load(Ordering::Relaxed) > 0, "PROPERTY_SEED={seed}: no chunked read came in more than one chunk"); + let made = made.into_inner(); for (ext, n) in FORMATS.iter().zip(written.into_inner()) { - assert!(n.iter().all(|n| *n > 0), "PROPERTY_SEED={seed}: {ext} wrote no layer of some kind (points, lines, polygons): {n:?}"); + let missing = n.iter().zip(made).any(|(n, made)| made && *n == 0); + assert!(!missing, "PROPERTY_SEED={seed}: {ext} wrote no layer of a kind made (points, lines, polygons): {n:?}"); } } From a8a108c577829d1e8c719bc7b27b26e32f994550 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 13 Nov 2026 05:30:05 -0600 Subject: [PATCH 13/13] test(data): the extent property says GeoJSON has no chunked read Co-Authored-By: Claude Opus 5.5 --- crates/data/tests/extent_query_properties.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/data/tests/extent_query_properties.rs b/crates/data/tests/extent_query_properties.rs index a822ee9bce..8d9dcec587 100644 --- a/crates/data/tests/extent_query_properties.rs +++ b/crates/data/tests/extent_query_properties.rs @@ -1,9 +1,10 @@ //! A source asked for the features in an extent gives the features a whole read gives whose //! geometry's box meets the extent (`Request`), as a property: through each provider's spatial //! index (GeoPackage and Shapefile R-trees, FlatGeobuf's packed R-tree, GeoJSON's boxes) or its -//! scan, by `read` and by `visit_chunks`. What a provider may do differently is listed in -//! [`LIMITS`], with the reason. Layers and extents lean on edges: boxes that touch, degenerate -//! boxes, empty and inverted boxes, infinite boxes, coordinates that `f32` can't hold, empty layers. +//! scan, by `read` and, where the source has it (not GeoJSON), by `visit_chunks`. What a provider +//! may do differently is listed in [`LIMITS`], with the reason. Layers and extents lean on edges: +//! boxes that touch, degenerate boxes, empty and inverted boxes, infinite boxes, coordinates that +//! `f32` can't hold, empty layers. //! //! Pull requests run a fixed seed; the nightly a new one with more cases. A failure gives its seed //! and the smallest layer and extent found. To run a seed again, or deeper: