Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
631 changes: 586 additions & 45 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
86 changes: 68 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 Down Expand Up @@ -195,7 +222,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);
Ok(ToolResult { output: Output::Layer(out), messages })
}
Expand All @@ -204,7 +231,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 @@ -262,17 +289,25 @@ fn notices(t: &Tally, field: &str) -> Vec<String> {
Gone::NoArea.as_str()
));
}
// What Shells Minus Holes does with a hole outside its ring and with overlapping parts; Even-Odd
// does otherwise, and its own notice says what.
let holes = t.count(Code::HoleOutsideShell);
if holes > 0 {
if holes > 0 && rule == Rule::ShellsMinusHoles {
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)
));
}
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 leaves out where parts overlap, 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 @@ -289,6 +324,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)
));
}
if t.extra > 0 {
messages.push(format!(
"{TOOL}: {} had more attribute values than the layer has fields; the extra values aren't in the output",
Expand Down Expand Up @@ -364,7 +414,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 @@ -377,7 +427,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 @@ -387,22 +437,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 @@ -417,10 +467,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 @@ -434,7 +484,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