Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2320175
feat(analysis): Repair Geometry takes a rule for what is inside: Shel…
flight1973 Nov 17, 2026
6bd2d07
fix(analysis): Even-Odd finds duplicate rings in linear time and says…
flight1973 Nov 17, 2026
7b7855c
fix(analysis): Even-Odd tells overlapping parts by their exterior rin…
flight1973 Nov 17, 2026
2234f12
fix(analysis): Even-Odd combines each part first, then the parts (#2003)
flight1973 Nov 17, 2026
80c1aab
fix(analysis): Even-Odd's notices say what it did, and rule names in …
flight1973 Nov 17, 2026
a560a36
docs(analysis): Even-Odd's parts notice says what the rule does, and …
flight1973 Nov 18, 2026
8177ea4
fix(analysis): Even-Odd parts all narrower than a grid step leave no …
flight1973 Nov 18, 2026
bb8dc13
Merge remote-tracking branch 'origin/main' into feat/2003-repair-rule
flight1973 Nov 18, 2026
cd27538
fix(analysis): under Even-Odd, a part repeated with a vertex in the m…
flight1973 Nov 18, 2026
32b6389
test(analysis): a near-copy of a part under Even-Odd keeps its area, …
flight1973 Nov 18, 2026
b2a0ff4
fix(analysis): a vertex is taken for one mid-edge only where the orie…
flight1973 Nov 18, 2026
f86dbec
fix(analysis): a vertex is taken for one mid-edge only where the orie…
flight1973 Nov 18, 2026
0e61634
Merge remote-tracking branch 'fork/feat/2003-repair-rule' into fix/23…
flight1973 Nov 18, 2026
5e02676
fix(analysis): the mid-edge test refuses a tiny nonzero coordinate to…
flight1973 Nov 18, 2026
4e681b8
fix(analysis): duplicates left out before Even-Odd leaves no area are…
flight1973 Nov 20, 2026
f076ddd
test(analysis): a no-area feature with no duplicate says none; the Ev…
flight1973 Nov 20, 2026
50fd059
Merge remote-tracking branch 'origin/main' into fix/2372-review
flight1973 Nov 20, 2026
74d486d
docs(analysis): each PostGIS-example doc with its own function, and t…
flight1973 Nov 20, 2026
482ce76
fix(analysis): a mid-edge vertex on a level edge is found at any size…
flight1973 Nov 21, 2026
c714b7b
fix(analysis): the Even-Odd hole notice says a hole's outside part is…
flight1973 Nov 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
678 changes: 632 additions & 46 deletions crates/analysis/src/repair.rs

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion crates/analysis/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,17 @@ pub static TOOLS: &[ToolSpec] = &[
id: "repairGeometry",
name: "Repair Geometry",
toolbox: "Data Management › Features",
summary: "Writes every feature with its geometry made valid where it can be: rings closed, repeated vertices and degenerate parts dropped, self-intersections, overlapping parts and holes outside their shell resolved. Features with a missing or non-finite coordinate anywhere, even in a hole, or left with no usable points or, for polygons, no area once repaired, are written without geometry. Other features too complex to check, or whose repair fails, are written unchanged. Says what was repaired in each.",
summary: "Writes every feature with its geometry made valid where it can be: rings closed, repeated vertices and degenerate parts dropped, self-intersections, overlapping parts and holes outside their shell resolved by the chosen rule (Shells Minus Holes merges overlapping parts; Even-Odd keeps what lies inside an odd number of rings). Features with a missing or non-finite coordinate anywhere, even in a hole, or left with no usable points or, for polygons, no area once repaired, are written without geometry. Other features too complex to check, or whose repair fails, are written unchanged. Says what was repaired in each.",
params: &[
P { name: "input", label: "Input Features", kind: K::VectorLayer, required: true, default: "", help: "" },
P {
name: "rule",
label: "Rule",
kind: K::Choice(&crate::repair::RULES),
required: false,
default: "\"Shells Minus Holes\"",
help: "What is inside a polygon whose rings cross, overlap or sit in the wrong place. Shells Minus Holes: each part is its outer ring less its holes, and overlapping parts merge. Even-Odd: what lies inside an odd number of rings, so where two parts overlap is left out and a ring listed as a part inside another is a hole. Valid polygons are written the same either way.",
},
P { name: "output", label: "Output Name", kind: K::Text, required: false, default: "", help: "" },
],
run: repair_geometry::run,
Expand Down
101 changes: 83 additions & 18 deletions crates/analysis/src/tools/repair_geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! repaired.
use super::check_geometry::{features, joined, row_bytes};
use super::*;
use crate::repair::{self, Gone, Kept, Outcome};
use crate::repair::{self, Gone, Kept, Outcome, Rule};
use crate::validity::Code;

const TOOL: &str = "Repair Geometry";
Expand All @@ -12,9 +12,24 @@ const TOOL: &str = "Repair Geometry";
/// valid ones as stored, repaired ones with their new geometry, those that can't be repaired
/// without geometry, and those left as they were (too complex, or the repair failed) as stored.
pub(super) fn run(inp: &dyn Inputs, p: &Json) -> Result<ToolResult, String> {
run_with_room(inp, p, &web_has_room, |g| repair::repair(g, &web_has_room))
run_with_room(inp, p, &web_has_room, |g, rule| repair::repair_by(g, rule, &web_has_room))
}

/// The rule `p` names (#2003), Shells Minus Holes when it names none.
fn rule_of(p: &Json) -> Result<Rule, String> {
match p.get("rule") {
None | Some(Json::Null) => Ok(Rule::default()),
Some(v) => v
.as_str()
.and_then(Rule::named)
.ok_or_else(|| format!("{TOOL}: rule is one of {}", repair::RULES.map(|r| format!("\"{r}\"")).join(", "))),
}
}

/// What a feature rebuilt under the Even-Odd rule has in its repairs field, after the codes: data,
/// never translated.
const EVEN_ODD: &str = "even_odd";

/// A feature's geometry in the output.
enum Written {
/// As stored.
Expand Down Expand Up @@ -58,6 +73,11 @@ struct Tally {
moved: f64,
/// Features with more attribute values than the layer has fields.
extra: usize,
/// Under the Even-Odd rule: features it rebuilt, and duplicate rings left out first, from how
/// many features.
even_odd: usize,
duplicates: usize,
duplicate_features: usize,
}

impl Tally {
Expand All @@ -72,8 +92,9 @@ fn run_with_room(
inp: &dyn Inputs,
p: &Json,
has_room: &dyn Fn(u64) -> bool,
repair: impl Fn(Option<&Geometry>) -> Outcome,
repair: impl Fn(Option<&Geometry>, Rule) -> Outcome,
) -> Result<ToolResult, String> {
let rule = rule_of(p)?;
let ds = input(inp, p, "input")?;
if ds.geometry_type == GeometryType::None {
return Err(format!("{TOOL} needs a layer with geometry: '{}' is a table", ds.name));
Expand Down Expand Up @@ -105,7 +126,7 @@ fn run_with_room(
crate::control::reached(i);
let Some(f) = ds.features.get(*row) else { continue };
tally.extra += usize::from(f.attrs.len() > width);
let (repairs, geometry) = match repair(f.geometry.as_ref()) {
let (repairs, geometry) = match repair(f.geometry.as_ref(), rule) {
Outcome::Valid => (String::new(), Written::Same),
Outcome::Repaired(r) => {
tally.repaired += 1;
Expand All @@ -122,9 +143,15 @@ fn run_with_room(
tally.overlaid += 1;
tally.moved = tally.moved.max(moved);
}
// A hole or part with no area on the overlay's grid is said after the codes.
// The Even-Odd rule decides only what the overlay rebuilds: said for those features.
let even_odd = rule == Rule::EvenOdd && r.moved.is_some();
tally.even_odd += usize::from(even_odd);
tally.duplicates += r.duplicates;
tally.duplicate_features += usize::from(r.duplicates > 0);
// A hole or part with no area on the overlay's grid is said after the codes, then the
// rule when it isn't the default.
let narrow = (r.narrow > 0).then_some(Gone::NoArea.as_str());
let names: Vec<&str> = r.codes.iter().map(|c| c.as_str()).chain(narrow).collect();
let names: Vec<&str> = r.codes.iter().map(|c| c.as_str()).chain(narrow).chain(even_odd.then_some(EVEN_ODD)).collect();
(joined(&names, ", ").map_err(no_memory)?, Written::Repaired(r.geometry))
}
Outcome::Gone(why) => {
Expand All @@ -133,6 +160,16 @@ fn run_with_room(
}
(joined(&[why.as_str()], "").map_err(no_memory)?, Written::Without)
}
// No area left once duplicates were left out: counted as both.
Outcome::GoneAfterDuplicates { duplicates } => {
let why = Gone::NoArea;
if let Some(n) = Gone::ALL.iter().position(|g| *g == why).and_then(|k| tally.gone.get_mut(k)) {
*n += 1;
}
tally.duplicates += duplicates;
tally.duplicate_features += 1;
(joined(&[why.as_str()], "").map_err(no_memory)?, Written::Without)
}
Outcome::Kept(why) => {
match why {
Kept::Work => tally.work += 1,
Expand Down Expand Up @@ -195,7 +232,7 @@ fn run_with_room(
}
out.features = list.into();

let mut messages = notices(&tally, &field);
let mut messages = notices(&tally, &field, rule);
messages.extend(notes);
let losses = (tally.extra > 0)
.then(|| rustgis_data::loss::LossMessage {
Expand All @@ -214,7 +251,7 @@ fn run_with_room(
/// and per kind of change beyond the codes: pieces dropped (with too few points or no area, or
/// narrower than a step of the overlay's grid in any direction), holes taken out, parts merged or
/// split, coordinates moved. `field` is the output's repairs field.
fn notices(t: &Tally, field: &str) -> Vec<String> {
fn notices(t: &Tally, field: &str, rule: Rule) -> Vec<String> {
let mut messages = vec![summary(t)];
let is = |n: usize| if n == 1 { "is" } else { "are" };
let was = |n: usize| if n == 1 { "was" } else { "were" };
Expand Down Expand Up @@ -272,17 +309,30 @@ fn notices(t: &Tally, field: &str) -> Vec<String> {
Gone::NoArea.as_str()
));
}
// What each rule does with a hole outside its ring and with overlapping parts.
let holes = t.count(Code::HoleOutsideShell);
if holes > 0 {
if holes > 0 && rule == Rule::ShellsMinusHoles {
Comment thread
flight1973 marked this conversation as resolved.
Resolved
messages.push(format!(
"{TOOL}: holes not inside their exterior ring were taken out of {}; what of a hole is inside its exterior ring stays a hole",
features(holes)
));
}
if holes > 0 && rule == Rule::EvenOdd {
messages.push(format!(
"{TOOL}: holes not inside their exterior ring in {} were combined by the Even-Odd rule: what of a hole lies outside its exterior ring is area, as a part, where no other ring covers it too; what lies inside stays a hole",
features(holes)
));
}
let merged = t.count(Code::PartsOverlap);
if merged > 0 {
if merged > 0 && rule == Rule::ShellsMinusHoles {
messages.push(format!("{TOOL}: the parts that overlapped or touched along a line were merged in {}", features(merged)));
}
if merged > 0 && rule == Rule::EvenOdd {
messages.push(format!(
"{TOOL}: the parts that overlapped or touched along a line in {} were combined by the Even-Odd rule, which keeps what an odd number of parts cover and leaves out what an even number cover, keeps a part repeated whole once, and joins parts that meet along a line",
features(merged)
));
}
let split = t.count(Code::DisconnectedInterior);
if split > 0 {
messages.push(format!(
Expand All @@ -299,6 +349,21 @@ fn notices(t: &Tally, field: &str) -> Vec<String> {
rounded_up(t.moved)
));
}
if t.even_odd > 0 {
messages.push(format!(
"{TOOL}: {} {} rebuilt by the Even-Odd rule, with {EVEN_ODD} in {field}: each part keeps what lies inside an odd number of its rings, whatever their roles, so where two holes overlap is area again, a ring inside another is a hole, and what of a hole lies outside its exterior ring is a part; then the parts are combined the same way",
features(t.even_odd),
was(t.even_odd)
));
}
if t.duplicates > 0 {
messages.push(format!(
"{TOOL}: {} duplicate {} left out of {} before the Even-Odd rule, which would have canceled each pair out",
t.duplicates,
if t.duplicates == 1 { "ring was" } else { "rings were" },
features(t.duplicate_features)
));
}
messages
}

Expand Down Expand Up @@ -368,7 +433,7 @@ mod tests {
asked.set(need);
true
},
|g| repair::repair(g, &|_| true),
|g, rule| repair::repair_by(g, rule, &|_| true),
)
.unwrap();
let Output::Layer(out) = &r.output else { panic!("a layer") };
Expand All @@ -381,7 +446,7 @@ mod tests {
let first = point_bytes(&inp.0.features[0].attrs, 2) + "ring_not_closed".len() as u64 + ALLOCATION_OVERHEAD + 2 * geometry;
let second = point_bytes(&inp.0.features[1].attrs, 2) + "null_geometry".len() as u64 + ALLOCATION_OVERHEAD;
assert_eq!(asked.get(), first + second);
let e = run_with_room(&inp, &p, &|_| false, |g| repair::repair(g, &|_| true)).err().unwrap();
let e = run_with_room(&inp, &p, &|_| false, |g, rule| repair::repair_by(g, rule, &|_| true)).err().unwrap();
assert_eq!(e, "Repair Geometry: the 2 features would need about 3 MiB, more than the browser can give now: select fewer features");
}

Expand All @@ -391,22 +456,22 @@ mod tests {
fn features_left_as_they_were_are_counted_by_reason() {
let t = Tally { processed: 9, failed: 1, ..Tally::default() };
assert_eq!(
notices(&t, "repairs"),
notices(&t, "repairs", Rule::default()),
[
"Repair Geometry: repaired 0 of 9 features",
"Repair Geometry: 1 feature couldn't be repaired, because geo's geometry operations failed on it, and is written unchanged, with repair_failed in repairs",
]
);
let t = Tally { processed: 9, work: 2, memory: 3, failed: 2, ..Tally::default() };
assert_eq!(
notices(&t, "repairs_2")[1..],
notices(&t, "repairs_2", Rule::default())[1..],
[
"Repair Geometry: 2 features were too complex to repair within the work limit and are written unchanged, with too_complex in repairs_2",
"Repair Geometry: 3 features couldn't be repaired, because there wasn't enough memory to repair them, and are written unchanged, with too_complex in repairs_2",
"Repair Geometry: 2 features couldn't be repaired, because geo's geometry operations failed on them, and are written unchanged, with repair_failed in repairs_2",
]
);
assert_eq!(notices(&Tally::default(), "repairs"), ["Repair Geometry: no features to repair"]);
assert_eq!(notices(&Tally::default(), "repairs", Rule::default()), ["Repair Geometry: no features to repair"]);
}

/// A cancel stops the repairs at the next feature, and the copies at the next row.
Expand All @@ -421,10 +486,10 @@ mod tests {
let control = crate::control::Control::new();
let _installed = crate::control::install(Some(control.clone()));
let repaired = std::cell::Cell::new(0);
let r = run_with_room(&inp, &p, &|_| true, |g| {
let r = run_with_room(&inp, &p, &|_| true, |g, rule| {
repaired.set(repaired.get() + 1);
control.cancel();
repair::repair(g, &|_| true)
repair::repair_by(g, rule, &|_| true)
});
assert_eq!(r.err().as_deref(), Some(crate::control::CANCELLED));
assert_eq!(repaired.get(), 1, "stopped before the second feature");
Expand All @@ -438,7 +503,7 @@ mod tests {
control.cancel();
true
},
|g| repair::repair(g, &|_| true),
|g, rule| repair::repair_by(g, rule, &|_| true),
);
assert_eq!(r.err().as_deref(), Some(crate::control::CANCELLED), "stopped in the copies");
}
Expand Down
Loading
Loading