diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 10604da458..2719013779 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -122,6 +122,9 @@ jobs: cargo test --locked -p rustgis-engine --test never_silent_roundtrip PROPERTY_CASES=200 cargo test --locked -p rustgis-analysis --test parallel_serial cargo test --locked -p rustgis-engine --test project_save_roundtrip + 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: name: Report diff --git a/Cargo.lock b/Cargo.lock index edfb1e25a6..c8c2449813 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4788,6 +4788,7 @@ dependencies = [ "parquet", "postgres", "proj4rs", + "proptest", "quick-xml 0.42.0", "roxmltree", "rusqlite", @@ -4864,6 +4865,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/extent_query_properties.rs b/crates/data/tests/extent_query_properties.rs new file mode 100644 index 0000000000..8d9dcec587 --- /dev/null +++ b/crates/data/tests/extent_query_properties.rs @@ -0,0 +1,225 @@ +//! 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, 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: +//! `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)] = &[]; + +/// 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 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))); + 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, mut chunks) = (Vec::new(), 0); + // 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 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")); + } + 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)); + // 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; + } + 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; + 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}"); + } + let made = made.into_inner(); + for (ext, n) in FORMATS.iter().zip(written.into_inner()) { + 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:?}"); + } +} diff --git a/crates/data/tests/reprojection_round_trip.rs b/crates/data/tests/reprojection_round_trip.rs new file mode 100644 index 0000000000..c8d52aaded --- /dev/null +++ b/crates/data/tests/reprojection_round_trip.rs @@ -0,0 +1,194 @@ +//! 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:` (#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 +//! 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 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"), + // 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. + ("+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"), + ("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"), +]; + +/// 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)) + }) +} + +/// 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) && [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. +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, _)| { + 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) { + // 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 { + 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..58b85d1d99 --- /dev/null +++ b/crates/geom/tests/geodesic_identities.rs @@ -0,0 +1,192 @@ +//! 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)); + // 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"))); + } + // 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: finite and equal to a part in 10⁹ and a square metre. +fn agree(a: f64, b: f64) -> bool { + a.is_finite() && b.is_finite() && (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); + // 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(); + 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}"); + } +}