Skip to content

Commit 7551717

Browse files
Mavrick-Fclaude
andcommitted
fix(analysis): settle Spatial Join's open questions; index the distance matches
- "Have their center in", as the tools' other choices (Center, Kilometers) and ArcGIS spell it. - A count merge rule gives 0 for a target without matches, as QGIS's Join Attributes by Location (Summary) does; other merged values stay null. - param_applies moves beside the Spatial Join spec it describes. - Closest and Within a distance look join features up in the grid index: Within a distance through a box around the target that holds every point within the distance, Closest through searches widening from a grid cell until one finds a feature within its distance. A box reaching a pole or across the antimeridian looks at every feature. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d64dcec commit 7551717

4 files changed

Lines changed: 152 additions & 66 deletions

File tree

‎crates/analysis/src/join.rs‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,49 @@ fn closest_on(p: Coord, a: Coord, b: Coord) -> Coord {
8484
Coord::new(a.x + t * dx, a.y + t * dy)
8585
}
8686

87+
/// How far apart, in degrees of latitude and of longitude, two points within `metres` of each
88+
/// other can be when one is no further than `lat` degrees from the equator. The longitude is
89+
/// `None` when they can reach a pole, so any longitudes can be that close. Two points whose
90+
/// longitudes differ by Δλ, both no further from the equator than φ, are at least
91+
/// 2R·asin(cos φ·sin(Δλ/2)) apart.
92+
fn spread(lat: f64, metres: f64) -> (f64, Option<f64>) {
93+
// A little slack, so a feature exactly at the distance isn't ruled out by rounding.
94+
let r = (metres.max(0.0) * (1.0 + 1e-9) + 1e-3) / EARTH_RADIUS_M;
95+
let dlat = r.to_degrees();
96+
let phi = (lat.abs() + dlat).to_radians();
97+
let reach = (r / 2.0).min(std::f64::consts::FRAC_PI_2).sin() / phi.cos();
98+
if phi >= std::f64::consts::FRAC_PI_2 || reach.is_nan() || reach >= 1.0 {
99+
return (dlat, None);
100+
}
101+
(dlat, Some((2.0 * reach.asin()).to_degrees()))
102+
}
103+
87104
/// Whether a feature within the lon/lat extent `b` may lie within `metres` of one within `a`:
88105
/// `false` only when every pair of points of the two extents is farther apart than that, at the
89106
/// poles and across the antimeridian too.
90107
pub fn may_be_within(a: &Extent, b: &Extent, metres: f64) -> bool {
91108
if a.is_empty() || b.is_empty() {
92109
return false;
93110
}
94-
// A little slack, so a feature exactly at the distance isn't ruled out by rounding.
95-
let r = (metres.max(0.0) * (1.0 + 1e-9) + 1e-3) / EARTH_RADIUS_M;
96-
let dlat = r.to_degrees();
111+
let (dlat, dlon) = spread(a.min_y.abs().max(a.max_y.abs()), metres);
97112
let lat_gap = (b.min_y - a.max_y).max(a.min_y - b.max_y).max(0.0);
98113
if lat_gap > dlat {
99114
return false;
100115
}
101-
// Two points whose longitudes differ by Δλ, both no further from the equator than φ, are at
102-
// least 2R·asin(cos φ·sin(Δλ/2)) apart.
103-
let phi = (a.min_y.abs().max(a.max_y.abs()) + dlat).to_radians();
104-
let reach = (r / 2.0).min(std::f64::consts::FRAC_PI_2).sin() / phi.cos();
105-
if phi >= std::f64::consts::FRAC_PI_2 || reach.is_nan() || reach >= 1.0 {
106-
return true;
116+
dlon.is_none_or(|dlon| lon_gap(a, b) <= dlon)
117+
}
118+
119+
/// The lon/lat extent that holds every point within `metres` of `ext`, for looking features up in
120+
/// an index. `None` when that reaches a pole or crosses the antimeridian, where only looking at
121+
/// every feature is sure to find them.
122+
pub fn search_box(ext: &Extent, metres: f64) -> Option<Extent> {
123+
if ext.is_empty() || !metres.is_finite() {
124+
return None;
107125
}
108-
let dlon = (2.0 * reach.asin()).to_degrees();
109-
lon_gap(a, b) <= dlon
126+
let (dlat, dlon) = spread(ext.min_y.abs().max(ext.max_y.abs()), metres);
127+
let dlon = dlon?;
128+
let (x0, x1) = (ext.min_x - dlon, ext.max_x + dlon);
129+
(x0 >= -180.0 && x1 <= 180.0).then(|| Extent::new(x0, ext.min_y - dlat, x1, ext.max_y + dlat))
110130
}
111131

112132
/// The gap in longitude between two extents (degrees), the short way round.

‎crates/analysis/src/lib.rs‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub mod topology;
2626
pub mod trend;
2727
pub mod warp;
2828

29-
pub use tools::{TOOLS, find_field_case_insensitive, holds_coordinates, simplify_geometry, zonal_bands};
29+
pub use tools::{TOOLS, find_field_case_insensitive, holds_coordinates, param_applies, simplify_geometry, zonal_bands};
3030

3131
use rustgis_data::Dataset;
3232
use serde_json::Value;
@@ -288,21 +288,6 @@ pub fn external_placeholder(_: &dyn Inputs, _: &Value) -> Result<ToolResult, Str
288288
Err("this tool runs through the external suite".into())
289289
}
290290

291-
/// Whether a tool's parameter applies, given the values of the others (`value` gives one's
292-
/// value as text, empty when unset, which stands for its default). The Tools pane hides and leaves
293-
/// out one that doesn't. Spatial Join's search distance, units and distance field apply only to the
294-
/// match options that measure distances, its merge rules only to one-to-one joins.
295-
pub fn param_applies(tool: &str, param: &str, value: &dyn Fn(&str) -> String) -> bool {
296-
match (tool, param) {
297-
("spatialJoin", "distance" | "units" | "distanceField") => {
298-
let m = value("match");
299-
tools::DISTANCE_MATCHES.iter().any(|d| d.eq_ignore_ascii_case(m.trim()))
300-
}
301-
("spatialJoin", "merge") => !value("operation").trim().eq_ignore_ascii_case("Join one to many"),
302-
_ => true,
303-
}
304-
}
305-
306291
/// A tool's parameters with defaults filled in, or an error naming a missing required one.
307292
/// [`run`] does this first; a host that runs an external tool another way (the browser's
308293
/// asynchronous runner) does it too, so both check parameters the same way.

0 commit comments

Comments
 (0)