Repository navigation
fix(analysis): Spatial Join and routes measure the Earth on the ellipsoid; lint the sphere #2730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fbf8081
02115c7
3c995c1
dbe11f8
c45eb23
f8a8c90
2665b44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,14 @@ | ||
| allow-unwrap-in-tests = true | ||
| allow-expect-in-tests = true | ||
| allow-panic-in-tests = true | ||
|
|
||
| # The Earth measures on the WGS 84 ellipsoid (`wgs84_distance_m`, `wgs84_length_m`, | ||
| # `wgs84_ring_perimeter_area_m2`), as the Measure tool does (#939, #2568). These measure on a sphere: | ||
| # a call says why with `#[expect(clippy::disallowed_methods, reason = "…")]`, for another body | ||
| # (scaled by `Body`) or a comparison the sphere can't change. | ||
| disallowed-methods = [ | ||
| { path = "rustgis_geom::haversine_m", reason = "the sphere; the Earth measures with `wgs84_distance_m`" }, | ||
| { path = "rustgis_geom::geodesic_length_m", reason = "the sphere; the Earth measures with `wgs84_length_m`" }, | ||
| { path = "rustgis_geom::geodesic_ring_perimeter_m", reason = "the sphere; the Earth measures with `wgs84_ring_perimeter_area_m2`" }, | ||
| { path = "rustgis_geom::geodesic_ring_area_m2", reason = "the sphere; the Earth measures with `wgs84_ring_perimeter_area_m2`" }, | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,9 @@ | |
| //! they are found there and measured straight, in the system's unit times its length. | ||
|
|
||
| use rustgis_data::{Coord, Dataset, FieldType, Geometry, Value}; | ||
| use rustgis_geom::{Aeqd, Extent, WGS84_MIN_RADIUS_M, geodesic_interpolate, geodesic_length_m, geodesic_ring_area_m2, wgs84_distance_m}; | ||
| use rustgis_geom::{ | ||
| Aeqd, Extent, WGS84_MIN_RADIUS_M, geodesic_interpolate, wgs84_distance_m, wgs84_length_m, wgs84_ring_perimeter_area_m2, | ||
| }; | ||
| use serde_json::Value as Json; | ||
|
|
||
| /// Whether a coordinate is a place on the Earth in lon/lat: finite, with a latitude of ±90° at most | ||
|
|
@@ -229,20 +231,24 @@ fn lon_gap(a: &Extent, b: &Extent) -> f64 { | |
| [-360.0, 0.0, 360.0].iter().map(|k| (b.min_x + k - a.max_x).max(a.min_x - (b.max_x + k)).max(0.0)).fold(f64::INFINITY, f64::min) | ||
| } | ||
|
|
||
| /// The ground area (square metres) of a lon/lat polygon geometry, holes taken out. | ||
| /// The ground area (square metres) of a lon/lat polygon geometry on the WGS 84 ellipsoid, holes | ||
| /// taken out, as the Measure tool gives it. NaN for a ring whose winding is ambiguous. | ||
| pub fn area_m2(g: &Geometry) -> f64 { | ||
| let Geometry::Polygon(polys) = g else { return 0.0 }; | ||
| polys | ||
| .iter() | ||
| .flat_map(|rings| rings.iter().enumerate()) | ||
| .map(|(i, r)| if i == 0 { geodesic_ring_area_m2(r) } else { -geodesic_ring_area_m2(r) }) | ||
| .map(|(i, r)| { | ||
| let (_, area) = wgs84_ring_perimeter_area_m2(r); | ||
| if i == 0 { area } else { -area } | ||
| }) | ||
| .sum() | ||
| } | ||
|
|
||
| /// The ground length (metres) of a lon/lat line geometry. | ||
| /// The ground length (metres) of a lon/lat line geometry on the WGS 84 ellipsoid. | ||
| pub fn length_m(g: &Geometry) -> f64 { | ||
| let Geometry::Line(parts) = g else { return 0.0 }; | ||
| parts.iter().map(|p| geodesic_length_m(p)).sum() | ||
| parts.iter().map(|p| wgs84_length_m(p)).sum() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance, medium-low confidence. The PR justifies the ellipsoid overlap cost as "small next to the overlay that cuts it out of the pair". That holds for areas, where the ellipsoid polygon pass sits beside a boolean overlay. For lines it looks weaker: Options: measure a dense line in the dev profile and update the PR's cost claim, or use Category: performance
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The line case was not measured. The largest-overlap line length moves to |
||
| } | ||
|
|
||
| /// How the values of a join field's matches become one value in a one-to-one join. | ||
|
|
@@ -495,6 +501,7 @@ mod tests { | |
| /// The ground distance from `p` to a line, by sampling each edge finely along its great circle. | ||
| /// Only the samples within 1.2% of the nearest on the sphere are measured on the ellipsoid, | ||
| /// whose distances are within 0.6% of the sphere's. | ||
| #[expect(clippy::disallowed_methods, reason = "the test ranks samples on the sphere and measures the nearest on the ellipsoid")] | ||
| fn brute(p: Coord, line: &[Coord]) -> f64 { | ||
| let samples: Vec<(Coord, f64)> = line | ||
| .windows(2) | ||
|
|
@@ -506,6 +513,7 @@ mod tests { | |
| } | ||
|
|
||
| #[test] | ||
| #[expect(clippy::disallowed_methods, reason = "the test tells the ellipsoid's distance from the sphere's")] | ||
| fn distances_are_on_the_wgs84_ellipsoid() { | ||
| // 0.0904369° north of the equator is 10,000 m on WGS 84 (GeographicLib) but 10,056 m on the | ||
| // mean sphere: within 10 km in the measure, and not ruled out before it (#888). | ||
|
|
@@ -518,6 +526,59 @@ mod tests { | |
| assert!(search_box(&ea, 10_000.0).is_some_and(|e| e.max_y >= b.y)); | ||
| } | ||
|
|
||
| /// Spatial Join's Largest overlap ranks overlaps by their area or length on the WGS 84 | ||
| /// ellipsoid, as the Measure tool measures them. A 1° square on the equator, listed first, and a | ||
| /// band at 60°N that the ellipsoid makes 0.5% larger but the sphere 0.5% smaller: the band | ||
| /// wins. Likewise for a line along a meridian through a 1° zone and a shorter one at 60°N. | ||
| #[test] | ||
| #[expect(clippy::disallowed_methods, reason = "the test sets the sphere's ranking against the ellipsoid's")] | ||
| fn largest_overlap_ranks_overlaps_on_the_ellipsoid() { | ||
| use crate::{Inputs, Output}; | ||
| use rustgis_data::{Field, GeometryType}; | ||
| use rustgis_geom::{geodesic_length_m, geodesic_ring_area_m2}; | ||
| use std::collections::HashMap; | ||
| struct Layers(HashMap<String, Dataset>); | ||
| impl Inputs for Layers { | ||
| fn dataset(&self, layer: &str) -> Option<&Dataset> { | ||
| self.0.get(layer) | ||
| } | ||
| fn selection(&self, _: &str) -> &[usize] { | ||
| &[] | ||
| } | ||
| } | ||
| let ring = |x0: f64, x1: f64, y0: f64, y1: f64| { | ||
| vec![Coord::new(x0, y0), Coord::new(x1, y0), Coord::new(x1, y1), Coord::new(x0, y1), Coord::new(x0, y0)] | ||
| }; | ||
| let zones = |band_top: f64| { | ||
| let mut ds = Dataset::new("zones", GeometryType::Polygon, vec![Field::new("zone", FieldType::Text)]); | ||
| for (name, r) in [("equator", ring(0.0, 1.0, 0.0, 1.0)), ("north", ring(0.0, 1.0, 60.0, band_top))] { | ||
| ds.push(Some(Geometry::Polygon(vec![vec![r]])), vec![Value::Text(name.into())]); | ||
| } | ||
| ds | ||
| }; | ||
| let joined = |target: Dataset, zones: Dataset| { | ||
| let layers = Layers(HashMap::from([("t".to_string(), target), ("zones".to_string(), zones)])); | ||
| let r = crate::run("spatialJoin", &layers, &serde_json::json!({"target": "t", "join": "zones", "match": "Largest overlap"})); | ||
| let Ok(crate::ToolResult { output: Output::Layer(ds), .. }) = r else { panic!("a layer") }; | ||
| ds.field_index("join_zone").map(|i| ds.value(0, i).clone()) | ||
| }; | ||
| let north = Some(Value::Text("north".into())); | ||
|
|
||
| let (square, band) = (ring(0.0, 1.0, 0.0, 1.0), ring(0.0, 1.0, 60.0, 62.054)); | ||
| let area = |r: &Vec<Coord>| wgs84_ring_perimeter_area_m2(r).1; | ||
| assert!(area(&band) > area(&square) * 1.004 && geodesic_ring_area_m2(&band) < geodesic_ring_area_m2(&square) * 0.996); | ||
| let mut parcel = Dataset::new("t", GeometryType::Polygon, vec![]); | ||
| parcel.push(Some(Geometry::Polygon(vec![vec![ring(0.0, 1.0, -1.0, 63.0)]])), vec![]); | ||
| assert_eq!(joined(parcel, zones(62.054)), north); | ||
|
|
||
| let (near_equator, near_pole) = ([Coord::new(0.5, 0.0), Coord::new(0.5, 1.0)], [Coord::new(0.5, 60.0), Coord::new(0.5, 60.995)]); | ||
| assert!(wgs84_length_m(&near_pole) > wgs84_length_m(&near_equator) * 1.002); | ||
| assert!(geodesic_length_m(&near_pole) < geodesic_length_m(&near_equator) * 0.996); | ||
| let mut road = Dataset::new("t", GeometryType::Polyline, vec![]); | ||
| road.push(Some(Geometry::Line(vec![vec![Coord::new(0.5, -1.0), Coord::new(0.5, 63.0)]])), vec![]); | ||
| assert_eq!(joined(road, zones(60.995)), north); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_integer_sum_takes_decimals_over_the_whole_i64_range() { | ||
| let sum = Merge { field: 0, rule: Rule::Sum, delimiter: String::new() }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR adds a check (the
disallowed-methodslint over the four spherical measures), but its rule is only listed under "Suggested AGENTS.md additions". AGENTS.md's "This file" section says a PR that adds a check updates its line in the Quality gates section. A line for this check belongs in this PR, and the suggested addition covers the agent-facing rule. A maintainer may still decide otherwise.Category: agents-md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in c45eb23: AGENTS.md › Quality gates now has the lint's line, and the description no longer carries it as a suggestion.