Skip to content

Commit 9bd2a5d

Browse files
Mavrick-Fclaude
andcommitted
fix(analysis): no edge across a left-out coordinate; Closest skips features too far
- A coordinate a distance leaves out (not a place on Earth) ends a run of edges instead of joining its neighbours with an edge the data doesn't have, and a ring missing one isn't closed across the gap. A point beside such an invented edge measured 0 m instead of 556 km. - Within one search, Closest doesn't measure a join feature whose extent can't come within the nearest distance found so far. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8e8c2ec commit 9bd2a5d

2 files changed

Lines changed: 42 additions & 11 deletions

File tree

‎crates/analysis/src/join.rs‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,21 @@ fn nearest_pair(a: &Geometry, b: &Geometry, plane: &Aeqd, seed: Option<(Coord, C
8888
fn segments(g: &Geometry, plane: &Aeqd) -> Vec<(Coord, Coord)> {
8989
let mut out = Vec::new();
9090
let mut path = |pts: &[Coord], closed: bool| {
91-
let v: Vec<Coord> = pts.iter().filter(|c| on_earth(c)).map(|c| plane.forward(*c)).collect();
92-
match v.as_slice() {
93-
[] => {}
94-
[only] => out.push((*only, *only)),
95-
_ => {
96-
out.extend(v.windows(2).filter_map(|w| Some((*w.first()?, *w.get(1)?))));
97-
if let (true, Some(first), Some(last)) = (closed, v.first(), v.last())
98-
&& first != last
99-
{
100-
out.push((*last, *first));
91+
// A coordinate left out ends a run: no edge joins the runs on either side of it, and a
92+
// ring missing one isn't closed across the gap.
93+
let whole = pts.iter().all(on_earth);
94+
for run in pts.split(|c| !on_earth(c)).filter(|r| !r.is_empty()) {
95+
let v: Vec<Coord> = run.iter().map(|c| plane.forward(*c)).collect();
96+
match v.as_slice() {
97+
[] => {}
98+
[only] => out.push((*only, *only)),
99+
_ => {
100+
out.extend(v.windows(2).filter_map(|w| Some((*w.first()?, *w.get(1)?))));
101+
if let (true, Some(first), Some(last)) = (closed && whole, v.first(), v.last())
102+
&& first != last
103+
{
104+
out.push((*last, *first));
105+
}
101106
}
102107
}
103108
}
@@ -357,6 +362,26 @@ mod tests {
357362
assert_eq!(sum.apply(FieldType::Integer, &[&Value::Float(2.5)]), Value::Null);
358363
}
359364

365+
#[test]
366+
fn a_left_out_coordinate_does_not_join_its_neighbours() {
367+
// The line goes north to a coordinate that is no place, and back: no edge runs straight
368+
// between the two real vertices, past the point between them.
369+
let line = Geometry::Line(vec![vec![Coord::new(0.0, 0.0), Coord::new(600.0, 80.0), Coord::new(10.0, 0.0)]]);
370+
let d = distance_m(&Geometry::Point(Coord::new(5.0, 0.0)), &line).unwrap();
371+
let truth = haversine_m(Coord::new(5.0, 0.0), Coord::new(0.0, 0.0));
372+
assert!((d - truth).abs() < 1e-6 * truth, "{d} vs {truth}");
373+
// Nor does a ring close across the gap.
374+
let ring = Geometry::Polygon(vec![vec![vec![
375+
Coord::new(0.0, 0.0),
376+
Coord::new(10.0, 0.0),
377+
Coord::new(f64::NAN, 5.0),
378+
Coord::new(0.0, 10.0),
379+
Coord::new(0.0, 0.0),
380+
]]]);
381+
let d = distance_m(&Geometry::Point(Coord::new(5.0, 5.2)), &ring).unwrap();
382+
assert!(d > 400_000.0, "{d}");
383+
}
384+
360385
#[test]
361386
fn an_absurd_coordinate_does_not_hide_a_real_one() {
362387
// A finite longitude far out of range projects to nothing finite; the real point beside it

‎crates/analysis/src/tools.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,14 +1633,20 @@ fn spatial_join(inp: &dyn Inputs, p: &Json) -> Result<ToolResult, String> {
16331633
None => (0..join_geoms.len()).collect(),
16341634
};
16351635
let mut near = Vec::new();
1636+
// Closest wants only the nearest: a feature that can't be as near as the
1637+
// nearest so far isn't measured.
1638+
let mut bound = reach;
16361639
for idx in looked {
16371640
let Some(j) = at(idx) else { continue };
1638-
if !crate::join::may_be_within(&t_ext, &j.ext, reach) {
1641+
if !crate::join::may_be_within(&t_ext, &j.ext, bound) {
16391642
continue;
16401643
}
16411644
let d = *measured.entry(idx).or_insert_with(|| ground_distance(tg, &t_geo, j.geom, &j.geo));
16421645
if let Some(d) = d.filter(|d| *d <= reach * (1.0 + 1e-9) + 1e-6) {
16431646
near.push((j.row, d));
1647+
if mode == JoinMatch::Closest {
1648+
bound = bound.min(d);
1649+
}
16441650
}
16451651
}
16461652
near

0 commit comments

Comments
 (0)