diff --git a/crates/analysis/src/repair.rs b/crates/analysis/src/repair.rs index a921ac3db3..1b983c36a8 100644 --- a/crates/analysis/src/repair.rs +++ b/crates/analysis/src/repair.rs @@ -31,6 +31,7 @@ use crate::validity::{self, At, Checked, Code, Limit, Problem, Room}; use geo::algorithm::bool_ops::{BooleanOps, FillRule, OpType}; use geo::orient::{Direction, Orient}; +use geo::{Area, Euclidean, Length}; use rustgis_data::{Coord, Geometry}; /// Work per vertex the overlay takes in or makes, against the same bound as a check @@ -109,6 +110,9 @@ pub(crate) struct Repaired { /// When the overlay rebuilt it, how far its coordinates may have moved, in their units (degrees, /// as coordinates are stored). pub moved: Option, + /// Under [`Rule::EvenOdd`], duplicate rings (the same corners, [`normal_form`]) of one role dropped before the rings were + /// combined, since a pair would cancel out. + pub duplicates: usize, } /// What [`repair`] made of one geometry. @@ -121,17 +125,76 @@ pub(crate) enum Outcome { Kept(Kept), } -/// Repairs one geometry, spending at most [`validity::MAX_WORK`] on each check and on the -/// overlay, and asking `has_room` whether the browser can give the overlay's memory before it runs -/// (see `web_has_room` in the tools). -pub(crate) fn repair(g: Option<&Geometry>, has_room: &dyn Fn(u64) -> bool) -> Outcome { - repair_with(g, validity::MAX_WORK, &|_| true, has_room) +/// How the rebuild by area decides what is inside a polygon whose rings cross, overlap or sit in +/// the wrong place (#2003). Both give a valid geometry; they differ in what the rings are taken to +/// mean, and only for such polygons: one valid as it is, or fixed without moving a coordinate, is +/// written the same either way. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(crate) enum Rule { + /// Each part is its exterior ring's area less the union of its holes', and the feature is the + /// union of its parts: the rings' roles as OGC gives them (GEOS's "structure"). Parts that + /// overlap merge, a hole outside its exterior ring disappears, and a ring listed as a part of + /// its own is filled even when it lies inside another part. + #[default] + ShellsMinusHoles, + /// Each ring's own area as [`Rule::ShellsMinusHoles`] reads it (its loops by winding, so a + /// ring traced twice or a bowtie keeps its area), then the rings combined by parity whatever + /// their roles, first within each part and then the parts together: what lies inside an odd + /// number of rings is in, close to GEOS's "linework" method. Where two parts overlap is left + /// out, where two holes of a part overlap is area again, a ring listed as a part inside another + /// is a hole, and what of a hole lies outside its exterior ring is a part. A hole repeated in + /// its part and a part repeated whole are dropped first, since a pair would cancel out + /// ([`Repaired::duplicates`]); linework would cancel them. + EvenOdd, } -/// [`repair`] with a work `budget`, asking `allow` before each reservation of a buffer whether to +/// Each rule by the name Repair Geometry's `rule` parameter gives it, in the order it offers them. +const NAMED: &[(&str, Rule)] = &[("Shells Minus Holes", Rule::ShellsMinusHoles), ("Even-Odd", Rule::EvenOdd)]; +/// The names of [`NAMED`], in its order, as the parameter's choices. +pub(crate) const RULES: [&str; NAMED.len()] = { + let mut names = [""; NAMED.len()]; + let mut i = 0; + while i < NAMED.len() { + names[i] = NAMED[i].0; + i += 1; + } + names +}; + +impl Rule { + /// The rule a tool parameter names, as [`NAMED`] spells it, in any case. + pub(crate) fn named(name: &str) -> Option { + NAMED.iter().find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(_, rule)| *rule) + } +} + +/// The memory, per vertex, that [`Rule::EvenOdd`] holds besides the overlay's +/// ([`OVERLAY_BYTES`]), at most, while [`distinct_rings`] finds duplicates: each ring's normal form +/// kept (16), and while one is made its two readings (32), Booth's failure table (16), a rotation +/// (16) and the least so far (16; the table and the rotation are never held together); and, held +/// with them, a ring's place in `keep` (16), a part's holes listed (24 a ring), with their corner +/// counts (32), the ids kept (8) and their keys (32, up to twice that as a growing list reserves), +/// a ring at least 4 vertices. At most about 116 a vertex for large rings, about 48 for many small +/// ones; 128 leaves room. +const EVEN_ODD_BYTES: u64 = 128; + +/// Repairs one geometry under `rule`, spending at most [`validity::MAX_WORK`] on each check and on +/// the overlay, and asking `has_room` whether the browser can give the overlay's memory before it +/// runs (see `web_has_room` in the tools). +pub(crate) fn repair_by(g: Option<&Geometry>, rule: Rule, has_room: &dyn Fn(u64) -> bool) -> Outcome { + repair_by_with(g, rule, validity::MAX_WORK, &|_| true, has_room) +} + +/// [`repair_by`] with a work `budget`, asking `allow` before each reservation of a buffer whether to /// try it, as [`validity::check_with`] does: tests refuse some to stand in for running out of /// memory. -pub(crate) fn repair_with(g: Option<&Geometry>, budget: u64, allow: &dyn Fn(usize) -> bool, has_room: &dyn Fn(u64) -> bool) -> Outcome { +pub(crate) fn repair_by_with( + g: Option<&Geometry>, + rule: Rule, + budget: u64, + allow: &dyn Fn(usize) -> bool, + has_room: &dyn Fn(u64) -> bool, +) -> Outcome { let first = validity::check_with(g, budget, allow); if first.problems.is_empty() { return Outcome::Valid; @@ -148,11 +211,13 @@ pub(crate) fn repair_with(g: Option<&Geometry>, budget: u64, allow: &dyn Fn(usiz Geometry::Line(parts) => match lines(parts, room) { Err(limit) => Outcome::Kept(kept_for(limit)), Ok((parts, _)) if parts.is_empty() => Outcome::Gone(Gone::TooFewPoints), - Ok((parts, dropped)) => { - verified(Repaired { geometry: Geometry::Line(parts), codes: first.codes(), dropped, narrow: 0, moved: None }, budget, allow) - } + Ok((parts, dropped)) => verified( + Repaired { geometry: Geometry::Line(parts), codes: first.codes(), dropped, narrow: 0, moved: None, duplicates: 0 }, + budget, + allow, + ), }, - Geometry::Polygon(polys) => polygon(polys, &first, budget, allow, has_room), + Geometry::Polygon(polys) => polygon(polys, &first, rule, budget, allow, has_room), // A point's only problems, a missing or non-finite coordinate, are handled above. Geometry::Point(_) | Geometry::MultiPoint(_) => Outcome::Kept(Kept::Failed), } @@ -264,6 +329,7 @@ fn rings(polys: &[Vec>], room: Room) -> Result<(Vec>>, fn polygon( polys: &[Vec>], first: &Checked, + rule: Rule, budget: u64, allow: &dyn Fn(usize) -> bool, has_room: &dyn Fn(u64) -> bool, @@ -297,7 +363,7 @@ fn polygon( } codes.sort_unstable(); if second.problems.is_empty() { - return Outcome::Repaired(Repaired { geometry: fixed, codes, dropped, narrow: 0, moved: None }); + return Outcome::Repaired(Repaired { geometry: fixed, codes, dropped, narrow: 0, moved: None, duplicates: 0 }); } let Geometry::Polygon(parts) = &fixed else { return Outcome::Kept(Kept::Failed) }; if gone(second).is_some() { @@ -309,7 +375,8 @@ fn polygon( if size.saturating_mul(OVERLAY_COST) > budget { return Outcome::Kept(Kept::Work); } - if !has_room(size.saturating_mul(OVERLAY_BYTES)) { + let per_vertex = if rule == Rule::EvenOdd { OVERLAY_BYTES + EVEN_ODD_BYTES } else { OVERLAY_BYTES }; + if !has_room(size.saturating_mul(per_vertex)) { return Outcome::Kept(Kept::Memory); } let Some(step) = grid_step(parts.iter().flatten().flatten().map(|c| (c.x, c.y))) else { return Outcome::Kept(Kept::Failed) }; @@ -322,8 +389,11 @@ fn polygon( Ok(c) => c, Err(limit) => return Outcome::Kept(kept_for(limit)), }; - let rebuilt = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| by_area(parts, &crossing, room))); - let Rebuilt { polys, no_area, narrow, found } = match rebuilt { + let rebuilt = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| match rule { + Rule::ShellsMinusHoles => by_area(parts, &crossing, room), + Rule::EvenOdd => by_parity(parts, &crossing, room), + })); + let Rebuilt { polys, no_area, narrow, found, duplicates } = match rebuilt { Ok(Ok(rebuilt)) => rebuilt, Ok(Err(kept)) => return Outcome::Kept(kept), Err(_) => return Outcome::Kept(Kept::Failed), @@ -338,7 +408,11 @@ fn polygon( } codes.sort_unstable(); let moved = Some(step * GRID_STEPS_MOVED); - verified(Repaired { geometry: Geometry::Polygon(polys), codes, dropped: dropped.saturating_add(no_area), narrow, moved }, budget, allow) + verified( + Repaired { geometry: Geometry::Polygon(polys), codes, dropped: dropped.saturating_add(no_area), narrow, moved, duplicates }, + budget, + allow, + ) } /// The step of the integer grid i_overlay rounds the points `pts` to: it takes half the larger @@ -503,7 +577,7 @@ fn wider(mut polys: Vec>, step: f64, narrow: &mut usize, room: /// itself isn't related to its other rings or to other parts (see `Checked::unrelated`). type Found = Vec; -/// What [`by_area`] made of a feature's parts. +/// What [`by_area`] or [`by_parity`] made of a feature's parts. struct Rebuilt { /// The rings of the result. polys: Vec>>, @@ -512,6 +586,385 @@ struct Rebuilt { /// Holes and parts dropped as narrower than a step of the overlay's grid in any direction. narrow: usize, found: Found, + /// Exact duplicate rings of one role dropped first ([`Rule::EvenOdd`] only). + duplicates: usize, +} + +/// The area `ring` stands for, as both rules read one ring: as it is when simple (it isn't in +/// `crossing`, the rings that cross, touch or run back over themselves, by part and ring, sorted), +/// otherwise its loops by non-zero winding, so both lobes of a bowtie count and a ring traced twice +/// counts once. +fn ring_area(ring: &[Coord], at: (usize, usize), crossing: &[(usize, usize)], room: Room) -> Result, Kept> { + let ring = geo::LineString(room.collect(ring.len(), ring.iter().map(|c| geo::Coord { x: c.x, y: c.y })).map_err(kept_for)?); + let poly = geo::Polygon::new(ring, vec![]); + if crossing.binary_search(&at).is_err() { + // A simple ring: its area as it is, wound as the overlay expects. + return Ok(geo::MultiPolygon::new(vec![poly.orient(Direction::Default)])); + } + on_grid(std::iter::once(poly.exterior()))?; + Ok(poly.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::NonZero)) +} + +/// The rebuild under [`Rule::EvenOdd`] of `parts` (closed rings, each with 3 distinct points or +/// more; `crossing` as in [`ring_area`]), built as [`by_area`] builds its own, part by part and then +/// the parts together, with parity in place of shells minus holes: +/// - duplicates (the same corners, [`normal_form`]) left out first ([`distinct_rings`]): a hole repeated in its part, and a part +/// repeated whole, which parity would otherwise cancel out; +/// - each ring's own area ([`ring_area`]); a part whose exterior ring has none is left out with its +/// holes, as [`by_area`] leaves it; +/// - a part's holes narrower than a step of the overlay's grid dropped, then the part's rings +/// combined by parity, so its holes' overlaps come back and a hole partly outside its ring adds +/// that part as area; +/// - the parts, the narrow ones dropped, combined by parity, so where parts overlap is left out and +/// parts meeting along a line are joined. +/// +/// Besides the checks' codes it finds those the checks can't tell of a part with a ring crossing +/// itself ([`unrelated_hole_codes`]), and [`Code::PartsOverlap`] when the parts' combination makes fewer polygons than it was given (parts joined) +/// or less area than they had, by more than rounding to the grid could take: it moves each edge by +/// at most half a step on each axis, so changes the area by less than the rings' length times a +/// step. +/// +/// It has no Cancel check of its own: its passes over the rings are bounded by the work budget +/// `polygon` checks before calling it, and the tool looks at Cancel between features. +fn by_parity(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) -> Result { + let empty = geo::MultiPolygon::::new(vec![]); + let Distinct { keep, duplicates } = distinct_rings(parts, room).map_err(kept_for)?; + let mut all: Vec> = Vec::new(); + let mut no_area = 0usize; + let mut narrow = 0usize; + let mut found = Found::new(); + // `keep` is by part, then ring, and keeps every exterior ring of a part it keeps. + for kept in keep.chunk_by(|a, b| a.0 == b.0) { + let Some(&(p, 0)) = kept.first() else { continue }; + let Some(rings) = parts.get(p) else { continue }; + let Some(shell) = rings.first() else { continue }; + let shell = ring_area(shell, (p, 0), crossing, room)?; + if shell.0.is_empty() { + // Its holes go with it. + no_area += 1; + continue; + } + let mut cut: Vec> = Vec::new(); + for &(_, r) in kept.get(1..).unwrap_or_default() { + let Some(hole) = rings.get(r) else { continue }; + let hole = ring_area(hole, (p, r), crossing, room)?; + if hole.0.is_empty() { + no_area += 1; + } + for piece in hole.0 { + room.grow(&mut cut).map_err(kept_for)?; + cut.push(piece); + } + } + if !cut.is_empty() { + let step = on_grid(rings_of(&shell.0).chain(rings_of(&cut)))?; + cut = wider(cut, step, &mut narrow, room)?; + // What is left is on that grid too; checked again all the same, since a panic in the + // overlay can't be caught in the browser. + on_grid(rings_of(&shell.0).chain(rings_of(&cut)))?; + } + let part = if cut.is_empty() { + shell + } else { + let cut = geo::MultiPolygon::new(cut); + let related = (0..rings.len()).all(|r| crossing.binary_search(&(p, r)).is_err()); + if !related { + unrelated_hole_codes(&shell, &cut, &mut found); + } + let mut pieces: Vec> = room.vec(shell.0.len().saturating_add(cut.0.len())).map_err(kept_for)?; + pieces.extend(shell.0); + pieces.extend(cut.0); + geo::MultiPolygon::new(pieces).boolean_op_with_fill_rule(&empty, OpType::Union, FillRule::EvenOdd) + }; + if part.0.is_empty() { + no_area += 1; + } + for piece in part.0 { + room.grow(&mut all).map_err(kept_for)?; + all.push(piece); + } + } + let combined = if parts.len() > 1 && all.len() > 1 { + // Parts narrower than a step of the grid, in any direction, are dropped first; finding the + // step checks the grid can take every vertex, which holds for those left. + let step = on_grid(rings_of(&all))?; + let all = wider(all, step, &mut narrow, room)?; + // Every part narrower than a step (two thin rectangles overlapping): none left, so no area, + // as a part with none is, not a grid with no coordinates to check. + if all.is_empty() { + return Ok(Rebuilt { polys: Vec::new(), no_area, narrow, found, duplicates }); + } + on_grid(rings_of(&all))?; + let given = all.len(); + let each: f64 = all.iter().map(Area::unsigned_area).sum(); + let length: f64 = rings_of(&all).map(|r| Euclidean.length(r)).sum(); + let combined = geo::MultiPolygon::new(all).boolean_op_with_fill_rule(&empty, OpType::Union, FillRule::EvenOdd); + if combined.0.len() < given || each - combined.unsigned_area() > length * step { + found.push(Code::PartsOverlap); + } + combined + } else { + geo::MultiPolygon::new(all) + }; + Ok(Rebuilt { polys: polygons(&combined, room).map_err(kept_for)?, no_area, narrow, found, duplicates }) +} + +/// What the checks don't tell of the holes of a part with a ring crossing itself, which they +/// can't relate to the rest: [`Code::HoleOutsideShell`] when some of `cut`, its holes' areas, lies +/// outside `shell`, its exterior ring's, and [`Code::RingsIntersect`] when holes overlap or touch +/// along a line, which their union tells by making fewer polygons than it was given. +fn unrelated_hole_codes(shell: &geo::MultiPolygon, cut: &geo::MultiPolygon, found: &mut Found) { + if !found.contains(&Code::HoleOutsideShell) && !cut.boolean_op_with_fill_rule(shell, OpType::Difference, FillRule::NonZero).0.is_empty() + { + found.push(Code::HoleOutsideShell); + } + if cut.0.len() > 1 + && !found.contains(&Code::RingsIntersect) + && on_grid(rings_of(&cut.0)).is_ok() + && cut.boolean_op_with_fill_rule(&geo::MultiPolygon::new(vec![]), OpType::Union, FillRule::NonZero).0.len() < cut.0.len() + { + found.push(Code::RingsIntersect); + } +} + +/// What [`distinct_rings`] keeps of a polygon's rings. +struct Distinct { + /// The rings to combine, by part and then ring. + keep: Vec<(usize, usize)>, + /// How many rings were left out as duplicates. + duplicates: usize, +} + +/// The rings of `parts` to combine under [`Rule::EvenOdd`], without duplicates whose +/// canceling out no one means: a hole repeated in its own part, and a part repeated whole (its +/// exterior ring and its holes, after the first). A hole repeated in another part is kept: each of +/// the parts has it, and leaving one out would fill it. The same ring is the same corners in the +/// same cyclic order, from any vertex and in either direction, a vertex in the middle of a straight +/// edge aside ([`normal_form`]): a copy with one more such vertex would cancel its twin out as +/// surely. Only rings, and parts, of as many corners are compared, so a polygon that can have no +/// duplicate costs no normal form. In buffers reserved fallibly. +fn distinct_rings(parts: &[Vec>], room: Room) -> Result { + let n = parts.iter().map(Vec::len).fold(0usize, usize::saturating_add); + let mut keep: Vec<(usize, usize)> = room.vec(n)?; + // One part with one hole or none can have no duplicate. + if let [rings] = parts + && rings.len() < 3 + { + keep.extend((0..rings.len()).map(|r| (0, r))); + return Ok(Distinct { keep, duplicates: 0 }); + } + let mut duplicates = 0usize; + for (p, rings) in parts.iter().enumerate() { + let Some((_, rest)) = rings.split_first() else { continue }; + keep.push((p, 0)); + let mut holes = room.collect(rest.len(), rest.iter().enumerate().map(|(h, ring)| (h + 1, ring.as_slice())))?; + let (kept, left_out) = first_of_each(&mut holes, room)?; + duplicates += left_out; + keep.extend(kept.into_iter().map(|r| (p, r))); + } + // Parts that may be the same: the same number of rings kept, of the same number of points. + let mut sizes: Vec<(usize, usize, usize)> = room.vec(parts.len())?; + for kept in keep.chunk_by(|a, b| a.0 == b.0) { + let Some(&(p, _)) = kept.first() else { continue }; + let points = kept.iter().filter_map(|&(p, r)| parts.get(p)?.get(r)).map(|r| corner_count(r)).fold(0usize, usize::saturating_add); + sizes.push((kept.len(), points, p)); + } + sizes.sort_unstable(); + let mut repeated: Vec = Vec::new(); + let mut keyed: Vec<(Vec>, usize)> = Vec::new(); + for group in sizes.chunk_by(|a, b| (a.0, a.1) == (b.0, b.1)) { + if group.len() < 2 { + continue; + } + keyed.clear(); + for &(count, _, p) in group { + let from = keep.partition_point(|k| k.0 < p); + let mut forms = room.vec(count)?; + for &(p, r) in keep.get(from..from.saturating_add(count)).unwrap_or_default() { + let Some(ring) = parts.get(p).and_then(|rings| rings.get(r)) else { continue }; + forms.push(normal_form(ring, room)?); + } + // The exterior ring first, then the holes in any order. + if let Some((_, holes)) = forms.split_first_mut() { + holes.sort_unstable(); + } + room.grow(&mut keyed)?; + keyed.push((forms, p)); + } + keyed.sort_unstable(); + for pair in keyed.windows(2) { + if let [first, then] = pair + && first.0 == then.0 + { + room.grow(&mut repeated)?; + repeated.push(then.1); + } + } + } + repeated.sort_unstable(); + let before = keep.len(); + keep.retain(|(p, _)| repeated.binary_search(p).is_err()); + Ok(Distinct { duplicates: duplicates + (before - keep.len()), keep }) +} + +/// Of `rings` (an id and the ring), the ids of those that aren't a duplicate (the same corners, [`normal_form`]) of another with +/// a lower id, in order, and how many are. Only rings with as many corners ([`corner_count`]) get +/// a normal form. +fn first_of_each(rings: &mut [(usize, &[Coord])], room: Room) -> Result<(Vec, usize), Limit> { + // One ring or none: no duplicate, and no corners to count. + if let [] | [_] = rings { + return Ok((room.collect(rings.len(), rings.iter().map(|&(id, _)| id))?, 0)); + } + let mut sized = room.collect(rings.len(), rings.iter().map(|&(id, ring)| (corner_count(ring), id, ring)))?; + sized.sort_unstable_by_key(|&(n, id, _)| (n, id)); + let mut kept: Vec = room.vec(rings.len())?; + let mut keyed: Vec<(Vec<(u64, u64)>, usize)> = Vec::new(); + for group in sized.chunk_by(|a, b| a.0 == b.0) { + if let [(_, id, _)] = group { + kept.push(*id); + continue; + } + for &(_, id, ring) in group { + room.grow(&mut keyed)?; + keyed.push((normal_form(ring, room)?, id)); + } + } + keyed.sort_unstable(); + let mut duplicates = 0usize; + for (i, (form, id)) in keyed.iter().enumerate() { + if i > 0 && keyed.get(i - 1).is_some_and(|prev| prev.0 == *form) { + duplicates += 1; + } else { + kept.push(*id); + } + } + kept.sort_unstable(); + Ok((kept, duplicates)) +} + +/// A ring without its closing repeat. +fn open(ring: &[Coord]) -> &[Coord] { + match ring.split_last() { + Some((last, rest)) if Some(last) == rest.first() => rest, + _ => ring, + } +} + +/// How far from 0, how near it, and how close together coordinates may be for +/// [`straight_through`]'s test to be exact: each 0 or between `1 / EXACT_SPAN` and `EXACT_SPAN` in +/// size, and any two on one axis equal or at least `1 / EXACT_SPAN` apart. The differences' +/// products then run from 1e-200 to 4e200, and the rounding tails the test's later stages +/// multiply together (each at least about 1e-116, from a coordinate or difference of 1e-100) stay +/// within an f64's normal range too. +const EXACT_SPAN: f64 = 1e100; + +/// Whether `b`, between `a` and `c` in a ring, lies on the straight line from `a` to `c`, exactly +/// (by Shewchuk's adaptive orientation test, as geo's robust kernel gives it: J. R. Shewchuk, +/// "Adaptive precision floating-point arithmetic and fast robust geometric predicates", Discrete & +/// Computational Geometry 18(3), 1997, 305–363), and strictly between them: a vertex in the middle +/// of a straight edge, which doesn't change the ring. A spike, going back the way it came, isn't. +/// +/// The test is exact only where its products neither overflow nor underflow (Shewchuk's own +/// condition; past it, an overflow's NaN reads as collinear and an underflow's 0 as on the line): +/// so only within [`EXACT_SPAN`] of 0, and with no two coordinates closer than its inverse but +/// equal, is a vertex taken for one mid-edge. Elsewhere it stays a corner. Real coordinates, in +/// degrees, meters or feet, are well inside. +fn straight_through(a: Coord, b: Coord, c: Coord) -> bool { + use geo::kernels::{Kernel, Orientation, RobustKernel}; + let g = |p: Coord| geo::Coord { x: p.x, y: p.y }; + let within = |lo: f64, v: f64, hi: f64| lo.min(hi) <= v && v <= lo.max(hi); + let sized = |v: f64| v == 0.0 || (1.0 / EXACT_SPAN..=EXACT_SPAN).contains(&v.abs()); + let small = |p: Coord| sized(p.x) && sized(p.y); + let apart = |u: f64, v: f64| u == v || (u - v).abs() >= 1.0 / EXACT_SPAN; + let exact = small(a) + && small(b) + && small(c) + && [(a.x, b.x), (a.x, c.x), (b.x, c.x), (a.y, b.y), (a.y, c.y), (b.y, c.y)].iter().all(|&(u, v)| apart(u, v)); + exact + && b != a + && b != c + && within(a.x, b.x, c.x) + && within(a.y, b.y, c.y) + && RobustKernel::orient2d(g(a), g(b), g(c)) == Orientation::Collinear +} + +/// The corners of an open ring: its vertices but those in the middle of a straight edge +/// ([`straight_through`]), each tested against its neighbors as written, so a straight run of +/// several loses them all. A ring of fewer than three vertices keeps them. +fn corners(pts: &[Coord]) -> impl Iterator { + let n = pts.len(); + pts.iter() + .enumerate() + .filter(move |&(i, &b)| { + n < 3 || { + let (a, c) = (pts.get((i + n - 1) % n), pts.get((i + 1) % n)); + !matches!((a, c), (Some(&a), Some(&c)) if straight_through(a, b, c)) + } + }) + .map(|(_, c)| c) +} + +/// How many corners a closed ring has ([`corners`]): the size its [`normal_form`] has. +fn corner_count(ring: &[Coord]) -> usize { + corners(open(ring)).count() +} + +/// A closed ring's corners ([`corners`]) as bits, without the closing repeat: its least rotation +/// (as a sequence of points) in whichever direction reads less, the same for the same ring whatever +/// vertex it starts at, whichever way it is wound, and whatever vertices it has in the middle of a +/// straight edge. In time and memory linear in the ring, however often it passes one vertex +/// ([`least_rotation`]). +fn normal_form(ring: &[Coord], room: Room) -> Result, Limit> { + let pts = open(ring); + let n = corners(pts).count(); + // Plus 0.0 makes -0.0 the same as 0.0: the same point, which writers and reprojection give either way. + let forward = room.collect(n, corners(pts).map(|c| ((c.x + 0.0).to_bits(), (c.y + 0.0).to_bits())))?; + let backward = room.collect(n, forward.iter().rev().copied())?; + let mut best: Option> = None; + for seq in [forward, backward] { + let start = least_rotation(&seq, room)?; + let form = room.collect(n, seq.iter().skip(start).chain(seq.iter().take(start)).copied())?; + if best.as_ref().is_none_or(|b| form < *b) { + best = Some(form); + } + } + Ok(best.unwrap_or_default()) +} + +/// Where the lexicographically least rotation of `s` starts, by Booth's algorithm in linear time +/// (K. S. Booth, "Lexicographically least circular substrings", Information Processing Letters +/// 10(4–5), 1980, 240–242): a failure function over `s` read twice round, as Knuth–Morris–Pratt +/// matching uses one. 0 for an empty `s`. +fn least_rotation(s: &[(u64, u64)], room: Room) -> Result { + let n = s.len(); + let len = n.saturating_mul(2); + // f[j]: the failure function's value at j, one more than the index it points to (0 for none), + // so it stays unsigned. + let mut f: Vec = room.collect(len, std::iter::repeat_n(0, len))?; + let at = |i: usize| s.get(i % n.max(1)).copied().unwrap_or_default(); + let mut k = 0usize; + for j in 1..len { + let sj = at(j); + let mut i = f.get(j.saturating_sub(k + 1)).copied().unwrap_or(0); + while i != 0 && sj != at(k + i) { + if sj < at(k + i) { + k = j.saturating_sub(i); + } + i = f.get(i.saturating_sub(1)).copied().unwrap_or(0); + } + if sj != at(k + i) { + // i == 0 here: no proper prefix matches. + if sj < at(k) { + k = j; + } + if let Some(v) = f.get_mut(j.saturating_sub(k)) { + *v = 0; + } + } else if let Some(v) = f.get_mut(j.saturating_sub(k)) { + *v = i + 1; + } + } + Ok(k % n.max(1)) } /// The rebuild by area (see the module doc) of `parts`, closed rings each with 3 distinct points @@ -525,16 +978,7 @@ struct Rebuilt { /// ones are dropped first, so their vanishing isn't taken for a merge). fn by_area(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) -> Result { let empty = geo::MultiPolygon::::new(vec![]); - let area = |ring: &Vec, at: (usize, usize)| -> Result, Kept> { - let ring = geo::LineString(room.collect(ring.len(), ring.iter().map(|c| geo::Coord { x: c.x, y: c.y })).map_err(kept_for)?); - let poly = geo::Polygon::new(ring, vec![]); - if crossing.binary_search(&at).is_err() { - // A simple ring: its area as it is, wound as the overlay expects. - return Ok(geo::MultiPolygon::new(vec![poly.orient(Direction::Default)])); - } - on_grid(std::iter::once(poly.exterior()))?; - Ok(poly.boolean_op_with_fill_rule(&empty, OpType::Union, FillRule::NonZero)) - }; + let area = |ring: &Vec, at: (usize, usize)| ring_area(ring, at, crossing, room); let mut all: Vec> = Vec::new(); let mut no_area = 0usize; let mut narrow = 0usize; @@ -570,21 +1014,8 @@ fn by_area(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) - // The holes' areas, each wound the same way, make their union under non-zero winding. let cut = geo::MultiPolygon::new(cut); let related = (0..rings.len()).all(|r| crossing.binary_search(&(p, r)).is_err()); - if !related - && !found.contains(&Code::HoleOutsideShell) - && !cut.boolean_op_with_fill_rule(&shell, OpType::Difference, FillRule::NonZero).0.is_empty() - { - found.push(Code::HoleOutsideShell); - } - // Holes that overlap or touch along a line merge, which their union tells by making - // fewer polygons than it was given, as the parts' union does below. - if !related - && cut.0.len() > 1 - && !found.contains(&Code::RingsIntersect) - && on_grid(rings_of(&cut.0)).is_ok() - && cut.boolean_op_with_fill_rule(&empty, OpType::Union, FillRule::NonZero).0.len() < cut.0.len() - { - found.push(Code::RingsIntersect); + if !related { + unrelated_hole_codes(&shell, &cut, &mut found); } shell.boolean_op_with_fill_rule(&cut, OpType::Difference, FillRule::NonZero) }; @@ -611,7 +1042,7 @@ fn by_area(parts: &[Vec>], crossing: &[(usize, usize)], room: Room) - } else { all }; - Ok(Rebuilt { polys: polygons(&merged, room).map_err(kept_for)?, no_area, narrow, found }) + Ok(Rebuilt { polys: polygons(&merged, room).map_err(kept_for)?, no_area, narrow, found, duplicates: 0 }) } /// The overlay's polygons as rings, copied fallibly. @@ -635,6 +1066,87 @@ fn polygons(m: &geo::MultiPolygon, room: Room) -> Result mod tests { use super::*; + /// [`repair_by`] under the default rule. + fn repair(g: Option<&Geometry>, has_room: &dyn Fn(u64) -> bool) -> Outcome { + repair_by(g, Rule::default(), has_room) + } + + /// [`repair_by_with`] under the default rule. + fn repair_with(g: Option<&Geometry>, budget: u64, allow: &dyn Fn(usize) -> bool, has_room: &dyn Fn(u64) -> bool) -> Outcome { + repair_by_with(g, Rule::default(), budget, allow, has_room) + } + + /// Booth's least rotation agrees with trying every rotation, on every sequence of up to 8 + /// symbols from 3, repeats and all. + #[test] + fn least_rotation_is_the_least_of_all_rotations() { + let room = Room(&|_| true); + for n in 1..=8usize { + for code in 0..3usize.pow(n as u32) { + let s: Vec<(u64, u64)> = (0..n).map(|k| ((code / 3usize.pow(k as u32) % 3) as u64, 0)).collect(); + let rotation = |start: usize| -> Vec<(u64, u64)> { s[start..].iter().chain(&s[..start]).copied().collect() }; + let least = (0..n).map(rotation).min().unwrap(); + assert_eq!(rotation(least_rotation(&s, room).unwrap()), least, "{s:?}"); + } + } + assert_eq!(least_rotation(&[], room).unwrap(), 0); + } + + /// The same ring has one normal form from any vertex, in either direction, and a different ring + /// another; a ring passing one vertex 100,000 times takes linear time, where trying each pass as + /// a start would take about 10^10 steps. + #[test] + fn a_ring_has_one_normal_form() { + let room = Room(&|_| true); + let square: Vec = ring(SQUARE); + let want = normal_form(&square, room).unwrap(); + let open = &square[..square.len() - 1]; + for start in 0..open.len() { + for reversed in [false, true] { + let mut r: Vec = open[start..].iter().chain(&open[..start]).copied().collect(); + if reversed { + r.reverse(); + } + r.push(r[0]); + assert_eq!(normal_form(&r, room).unwrap(), want, "from {start}, reversed {reversed}"); + } + } + assert_ne!(normal_form(&ring(&[(0., 0.), (10., 0.), (10., 11.), (0., 10.), (0., 0.)]), room).unwrap(), want); + // A vertex in the middle of a straight edge, or a straight run of them, doesn't change the + // ring; one past the edge's end (a spike out and back along it) does. + let mid = ring(&[(0., 0.), (2.5, 0.), (5., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]); + assert_eq!(normal_form(&mid, room).unwrap(), want); + assert_eq!(corner_count(&mid), 4); + let past = ring(&[(0., 0.), (12., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]); + assert_ne!(normal_form(&past, room).unwrap(), want); + // Where the orientation test isn't exact (its products overflowing or underflowing), a + // vertex is a corner, never taken for one on the line, on it or not: these two triangles + // aren't one ring. Within its range, one on the line is mid-edge. + let c = Coord::new; + assert!(!straight_through(c(-1e200, -1e200), c(0., 0.5e200), c(1e200, 1e200))); + assert!(!straight_through(c(0., 0.), c(0., 1.), c(1e200, 1e200)), "off the line, overflowing"); + assert!(!straight_through(c(0., 0.), c(1e-200, 1.5e-200), c(2e-200, 2e-200)), "off the line, underflowing"); + // A tiny coordinate beside ordinary ones, where the test's later stages would underflow: off + // the line by 1e-400, which the test would read as 0. + assert!(!straight_through(c(1e-200, -1.), c(0.5, -1e-200), c(1., 1.)), "a tiny coordinate"); + // Ordinary data, floating-point noise near 0 included, is within the test's range. + assert!(straight_through(c(0., 0.), c(6e-17, 6e-17), c(1., 1.))); + assert!(straight_through(c(-1e100, -1e100), c(0., 0.), c(1e100, 1e100))); + assert!(straight_through(c(0., 0.), c(1e-90, 1e-90), c(2e-90, 2e-90))); + let t1 = ring(&[(-1e200, -1e200), (1e200, 1e200), (1e200, -1e200), (-1e200, -1e200)]); + let t2 = ring(&[(-1e200, -1e200), (0., 0.5e200), (1e200, 1e200), (1e200, -1e200), (-1e200, -1e200)]); + assert_ne!(normal_form(&t1, room).unwrap(), normal_form(&t2, room).unwrap()); + // At the closing vertex too: a square starting mid-edge. + let from_mid = ring(&[(5., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.), (5., 0.)]); + assert_eq!(normal_form(&from_mid, room).unwrap(), want); + let mut star = vec![Coord::new(0., 0.)]; + for k in 0..100_000 { + let a = f64::from(k) * 1e-5; + star.extend([Coord::new(a.cos(), a.sin()), Coord::new(0., 0.)]); + } + assert_eq!(normal_form(&star, room).unwrap().len(), star.len() - 1); + } + type Ring<'a> = &'a [(f64, f64)]; /// Every reservation tried. @@ -793,6 +1305,34 @@ mod tests { assert_eq!(hull_of(&[]), []); } + /// Under Even-Odd, a part repeated with a vertex in the middle of a straight edge is the same + /// part, left out as a duplicate, not a copy that cancels its twin out. + #[test] + fn a_part_repeated_with_a_vertex_mid_edge_is_a_duplicate_under_even_odd() { + // The same square twice, once with a vertex in the middle of its bottom edge: without + // seeing them as one, parity cancels them out and the feature has no area. + let mid: Ring = &[(0., 0.), (5., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]; + let g = multi(&[&[SQUARE], &[mid]]); + match repair_by(Some(&g), Rule::EvenOdd, &|_| true) { + Outcome::Repaired(r) => { + assert_eq!(r.duplicates, 1); + assert!((area(&r.geometry) - 100.0).abs() < 1e-9, "{:?}", r.geometry); + } + other => panic!("{other:?}"), + } + } + + /// Under Even-Odd, parts all narrower than a step of the grid (two thin overlapping rectangles, + /// a billionth wide, where a step is 2⁻²⁷) are dropped, leaving no area: the feature has none, + /// not a repair that failed. + #[test] + fn parts_all_too_narrow_leave_no_area_under_even_odd() { + let a: Ring = &[(0., 0.), (1e-9, 0.), (1e-9, 10.), (0., 10.), (0., 0.)]; + let b: Ring = &[(0.5e-9, 0.), (1.5e-9, 0.), (1.5e-9, 10.), (0.5e-9, 10.), (0.5e-9, 0.)]; + let g = multi(&[&[a], &[b]]); + assert_eq!(repair_by(Some(&g), Rule::EvenOdd, &|_| true), Outcome::Gone(Gone::NoArea)); + } + #[test] fn what_can_not_be_repaired_has_its_reason() { assert_eq!(repair(None, &|_| true), Outcome::Gone(Gone::Null)); @@ -910,9 +1450,10 @@ mod tests { #[test] fn a_repair_that_is_not_valid_is_not_kept() { - let bad = Repaired { geometry: poly(&[BOWTIE]), codes: vec![Code::SelfIntersection], dropped: 0, narrow: 0, moved: None }; + let bad = + Repaired { geometry: poly(&[BOWTIE]), codes: vec![Code::SelfIntersection], dropped: 0, narrow: 0, moved: None, duplicates: 0 }; assert_eq!(verified(bad, validity::MAX_WORK, &|_| true), Outcome::Kept(Kept::Failed)); - let busy = Repaired { geometry: poly(&[BOWTIE]), codes: vec![], dropped: 0, narrow: 0, moved: None }; + let busy = Repaired { geometry: poly(&[BOWTIE]), codes: vec![], dropped: 0, narrow: 0, moved: None, duplicates: 0 }; assert_eq!(verified(busy, 0, &|_| true), Outcome::Kept(Kept::Work)); } } diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index 0bb511315f..df23c07ad7 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -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, diff --git a/crates/analysis/src/tools/repair_geometry.rs b/crates/analysis/src/tools/repair_geometry.rs index e6b5a2f363..8b1892c6e5 100644 --- a/crates/analysis/src/tools/repair_geometry.rs +++ b/crates/analysis/src/tools/repair_geometry.rs @@ -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"; @@ -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 { - 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 { + 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. @@ -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 { @@ -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 { + 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)); @@ -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; @@ -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) => { @@ -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 }) } @@ -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 { +fn notices(t: &Tally, field: &str, rule: Rule) -> Vec { 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" }; @@ -262,17 +289,25 @@ fn notices(t: &Tally, field: &str) -> Vec { 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!( @@ -289,6 +324,21 @@ fn notices(t: &Tally, field: &str) -> Vec { 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", @@ -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") }; @@ -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"); } @@ -387,7 +437,7 @@ 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", @@ -395,14 +445,14 @@ mod tests { ); 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. @@ -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"); @@ -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"); } diff --git a/crates/analysis/tests/repair_geometry.rs b/crates/analysis/tests/repair_geometry.rs index 75045de0e4..ceb97e3ed4 100644 --- a/crates/analysis/tests/repair_geometry.rs +++ b/crates/analysis/tests/repair_geometry.rs @@ -1039,8 +1039,10 @@ fn windings(g: &Geometry) -> Vec<(String, Geometry)> { /// each, as merging the parts does here. The manual gives no areas: the expected ones are this /// tool's own (geo's overlay), pinned so that a change shows, and checked against what any union /// of the parts must cover: at least the largest part, less than all of them added up. -#[test] -fn the_postgis_manual_examples_merge_into_one_polygon() { +/// The PostGIS manual's two ST_MakeValid examples of overlapping parts +/// (https://postgis.net/docs/ST_MakeValid.html), each ring a part of its own, with the area this +/// tool gives them merged (see [`the_postgis_manual_examples_merge_into_one_polygon`]). +fn postgis_examples() -> Vec<(Vec>, f64)> { let ring = |s: &str| -> Vec<(f64, f64)> { s.split(',') .map(|p| match p.split_whitespace().map(str::parse::).collect::>()[..] { @@ -1073,6 +1075,12 @@ fn the_postgis_manual_examples_merge_into_one_polygon() { 19872.857, ), ]; + examples.into() +} + +#[test] +fn the_postgis_manual_examples_merge_into_one_polygon() { + let examples = postgis_examples(); for (rings, want) in examples { // Each ring a part of its own, as the manual's MULTIPOLYGONs have them. let owned: Vec> = rings.iter().map(|r| vec![r.as_slice()]).collect(); @@ -1089,3 +1097,239 @@ fn the_postgis_manual_examples_merge_into_one_polygon() { assert!((got - want).abs() <= want * 1e-6, "area {got} not {want}"); } } + +/// The output and repairs of one polygon feature holding `g`, repaired under `rule` (#2003), and +/// the run's messages. +fn one_by(rule: &str, g: Geometry) -> (Option, String, Vec) { + let mut l = layer(GeometryType::Polygon); + add(&mut l, Some(g), "x"); + let r = rustgis_analysis::run("repairGeometry", &l, &json!({"input": "input", "rule": rule})).unwrap(); + valid(dataset(&r)); + let (g, repairs) = rows(&r).remove(0); + (g, repairs, r.messages.clone()) +} + +/// The hole count of each part of a polygon geometry, sorted, and its area. +fn shape(g: &Geometry) -> (Vec, f64) { + let Geometry::Polygon(parts) = g else { panic!("a polygon: {g:?}") }; + let mut holes: Vec = parts.iter().map(|rings| rings.len() - 1).collect(); + holes.sort_unstable(); + (holes, area(g)) +} + +/// The shapes where Even-Odd gives another polygon than the default, with what it gives: each +/// part's hole count, the area, and the repairs. +fn rule_cases() -> Vec<(&'static str, Geometry, Vec, f64, &'static str)> { + let twice: Ring = &[(0., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]; + let small: Ring = &[(20., 0.), (22., 0.), (22., 2.), (20., 2.), (20., 0.)]; + let arms = multi(&[&[&[(0., 4.), (10., 4.), (10., 6.), (0., 6.), (0., 4.)]], &[&[(4., 0.), (6., 0.), (6., 10.), (4., 10.), (4., 0.)]]]); + vec![ + // Where two parts overlap is left out: two L shapes touching at two corners; four arms. + ("two overlapping parts", multi(&[&[SQUARE], &[OVERLAPPING]]), vec![0, 0], 150.0, "parts_overlap, even_odd"), + ("plus sign", arms, vec![0, 0, 0, 0], 32.0, "parts_overlap, even_odd"), + // What of a hole lies outside its exterior ring is a part. + ("hole outside its shell", poly(&[SQUARE, OUTSIDE]), vec![0, 0], 100.5, "hole_outside_shell, even_odd"), + ( + "hole crossing its shell", + poly(&[SQUARE, &[(8., 4.), (8., 6.), (12., 6.), (12., 4.), (8., 4.)]]), + vec![0, 0], + 100.0, + "hole_outside_shell, even_odd", + ), + // A ring listed as a part inside another is a hole, whichever way it is wound. + ( + "hole listed as a part", + multi(&[&[SQUARE], &[&[(2., 2.), (2., 4.), (4., 4.), (4., 2.), (2., 2.)]]]), + vec![1], + 96.0, + "parts_overlap, even_odd", + ), + ( + "part inside another, wound the same way", + multi(&[&[SQUARE], &[&[(2., 2.), (8., 2.), (8., 8.), (2., 8.), (2., 2.)]]]), + vec![1], + 64.0, + "parts_overlap, even_odd", + ), + // Duplicates are left out first rather than cancel each other out. + ("the same part twice", multi(&[&[SQUARE], &[SQUARE]]), vec![0], 100.0, "parts_overlap, even_odd"), + ( + "the same part twice, from another vertex", + multi(&[&[SQUARE], &[&[(10., 0.), (10., 10.), (0., 10.), (0., 0.), (10., 0.)]]]), + vec![0], + 100.0, + "parts_overlap, even_odd", + ), + ( + "the same part twice, once with a vertex in the middle of an edge", + multi(&[&[SQUARE], &[&[(0., 0.), (5., 0.), (10., 0.), (10., 10.), (0., 10.), (0., 0.)]]]), + vec![0], + 100.0, + "parts_overlap, even_odd", + ), + // A ring's own area is read as the default reads it: traced twice, it counts once. + ("a ring traced twice", poly(&[twice]), vec![0], 100.0, "self_intersection, even_odd"), + ("a ring traced twice beside a part", multi(&[&[twice], &[small]]), vec![0, 0], 104.0, "self_intersection, even_odd"), + ] +} + +/// Shells Minus Holes, named, is the default exactly: the same output, coordinate for coordinate, +/// with the same repairs and notices, on every shape the default rebuilds and every shape the two +/// rules differ on. +#[test] +fn shells_minus_holes_named_is_the_default() { + let shapes = rebuilt().into_iter().map(|(_, g, _)| g).chain(rule_cases().into_iter().map(|(_, g, ..)| g)); + for g in shapes { + let mut l = layer(GeometryType::Polygon); + add(&mut l, Some(g.clone()), "x"); + let default = run(&l); + let named = rustgis_analysis::run("repairGeometry", &l, &json!({"input": "input", "rule": "Shells Minus Holes"})).unwrap(); + assert_eq!(dataset(&named).features[0].geometry, dataset(&default).features[0].geometry, "{g:?}"); + assert_eq!(rows(&named), rows(&default), "{g:?}"); + assert_eq!(named.messages, default.messages, "{g:?}"); + } +} + +/// Even-Odd (#2003): what lies inside an odd number of rings, each ring's own area first, in every +/// winding. A hole listed as a part stays a hole; overlaps are left out; duplicates are left out +/// first and said; a ring that runs back over itself keeps its area. The feature's repairs say the +/// rule, and one notice says how many features it rebuilt. +#[test] +fn even_odd_keeps_what_lies_inside_an_odd_number_of_rings() { + for (name, g, holes, want, code) in rule_cases() { + for (winding, g) in windings(&g) { + let (got, repairs, messages) = one_by("Even-Odd", g); + let got = got.unwrap_or_else(|| panic!("{name}, {winding}: no geometry ({repairs})")); + let (got_holes, got_area) = shape(&got); + assert_eq!(got_holes, holes, "{name}, {winding}: {got:?}"); + assert!((got_area - want).abs() < 1e-6, "{name}, {winding}: area {got_area}"); + assert_eq!(repairs, code, "{name}, {winding}"); + assert!(messages.iter().any(|m| m.contains("1 feature was rebuilt by the Even-Odd rule")), "{name}: {messages:?}"); + let dup = messages.iter().any(|m| m.contains("1 duplicate ring was left out")); + assert_eq!(dup, name.starts_with("the same part twice"), "{name}, {winding}: {messages:?}"); + } + } + // The hole listed as a part, exactly: the square with a hole where the ring was. + let (got, ..) = one_by("Even-Odd", multi(&[&[SQUARE], &[&[(2., 2.), (2., 4.), (4., 4.), (4., 2.), (2., 2.)]]])); + assert_eq!(got.as_ref().map(norm), Some(norm(&poly(&[SQUARE, &[(2., 2.), (2., 4.), (4., 4.), (4., 2.), (2., 2.)]])))); + // As under the default: a hole equal to its exterior ring leaves no area (they aren't of one + // role, so they aren't duplicates), and so does a ring with all its points on one line. + for g in [poly(&[SQUARE, SQUARE]), poly(&[&[(0., 0.), (5., 5.), (10., 10.), (0., 0.)]])] { + let (got, repairs, _) = one_by("Even-Odd", g.clone()); + assert_eq!((got, repairs.as_str()), (None, "no_area"), "{g:?}"); + } + // A feature valid as it is, or fixed without moving a coordinate, comes out the same under + // either rule; one the overlay rebuilds (the bowtie) says the rule. + for g in [poly(&[SQUARE, HOLE]), poly(&[&[(0., 0.), (10., 0.), (10., 10.), (0., 10.)]]), poly(&[BOWTIE])] { + let (got, repairs, messages) = one_by("Even-Odd", g.clone()); + let (want, default_repairs) = one(GeometryType::Polygon, Some(g.clone())); + assert_eq!(got.as_ref().map(norm), want.as_ref().map(norm), "{g:?}"); + let rebuilt = default_repairs.contains("self_intersection"); + assert_eq!(repairs.ends_with("even_odd"), rebuilt, "{g:?}: {repairs}"); + assert_eq!(messages.iter().any(|m| m.contains("Even-Odd")), rebuilt, "{g:?}"); + } +} + +/// The PostGIS manual's two examples under Even-Odd: 4 and 14 polygons, the counts the manual +/// shows for its "linework" method. +#[test] +fn even_odd_gives_the_postgis_manual_linework_counts() { + for ((rings, _), want) in postgis_examples().into_iter().zip([4, 14]) { + let owned: Vec> = rings.iter().map(|r| vec![r.as_slice()]).collect(); + let parts: Vec<&[Ring]> = owned.iter().map(Vec::as_slice).collect(); + let (got, repairs, _) = one_by("Even-Odd", multi(&parts)); + let Some(Geometry::Polygon(polys)) = got else { panic!("{repairs}") }; + assert_eq!(polys.len(), want, "{repairs}"); + } +} + +/// A rule the tool doesn't know is refused with its choices, not read as the default; any letter +/// case of a choice is the choice. +#[test] +fn an_unknown_rule_is_refused() { + let mut l = layer(GeometryType::Polygon); + add(&mut l, Some(poly(&[BOWTIE])), "x"); + let e = rustgis_analysis::run("repairGeometry", &l, &json!({"input": "input", "rule": "Odd-Even"})).unwrap_err(); + assert!(e.contains("Shells Minus Holes") && e.contains("Even-Odd"), "{e}"); + assert!(rustgis_analysis::run("repairGeometry", &l, &json!({"input": "input", "rule": "even-odd"})).is_ok()); +} + +/// Under Even-Odd the notices say what Even-Odd did: not the default's "merged" or "taken out"; +/// parts that overlap are said even when a part's ring crosses itself (which keeps the checks from +/// relating it); and a duplicate written with -0.0 where the other has 0.0 is the same ring. +#[test] +fn even_odd_says_what_it_did() { + let (_, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[OVERLAPPING]])); + assert_eq!(repairs, "parts_overlap, even_odd"); + assert!(!messages.iter().any(|m| m.contains("were merged") || m.contains("were taken out")), "{messages:?}"); + // A bowtie part with a square overlapping one lobe: the overlap is left out and said. + let lobes: Ring = &[(0., 0.), (4., 4.), (4., 0.), (0., 4.), (0., 0.)]; + let (got, repairs, _) = one_by("Even-Odd", multi(&[&[lobes], &[&[(3., 1.), (6., 1.), (6., 3.), (3., 3.), (3., 1.)]]])); + assert!(repairs.contains("parts_overlap") && repairs.contains("self_intersection"), "{repairs}"); + assert!(got.is_some(), "{repairs}"); + // A part's own hole is no overlap: a square with a hole, and a bowtie part well apart, which + // sends the feature to the overlay. + let far: Ring = &[(20., 0.), (22., 2.), (22., 0.), (20., 2.), (20., 0.)]; + let (_, repairs, _) = one_by("Even-Odd", multi(&[&[SQUARE, HOLE], &[far]])); + assert_eq!(repairs, "self_intersection, even_odd"); + // Parts sharing an edge, rounded to the grid, aren't taken for overlapping either (they touch + // along a line, which the checks say as such). + let side: Ring = &[(10., 0.), (20., 0.), (20., 10.), (10., 10.), (10., 0.)]; + let (_, repairs, _) = one_by("Even-Odd", multi(&[&[SQUARE], &[side], &[far]])); + assert!(repairs.contains("even_odd"), "{repairs}"); + // -0.0 and 0.0: one ring, left out once as a duplicate rather than canceled. + let neg: Ring = &[(-0.0, 0.), (10., 0.), (10., 10.), (-0.0, 10.), (-0.0, 0.)]; + let (got, _, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[neg]])); + assert_eq!(got.map(|g| area(&g)), Some(100.0)); + assert!(messages.iter().any(|m| m.contains("1 duplicate ring was left out")), "{messages:?}"); +} + +/// Even-Odd leaves out only duplicates no one means, gives an arealess part nothing, and says what +/// it did with holes and parts, without taking a part's own hole or an island in it for an overlap. +#[test] +fn even_odd_builds_part_by_part() { + let far: Ring = &[(20., 0.), (22., 2.), (22., 0.), (20., 2.), (20., 0.)]; + let inner: Ring = &[(2., 2.), (8., 2.), (8., 8.), (2., 8.), (2., 2.)]; + let middle: Ring = &[(4., 4.), (6., 4.), (6., 6.), (4., 6.), (4., 4.)]; + // Two parts with the same hole: the hole is in neither, so it stays out, and the inner part, + // inside the outer, is a hole. + let (got, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE, middle], &[inner, middle]])); + assert_eq!(got.map(|g| area(&g)), Some(64.0), "{repairs}"); + assert!(repairs.contains("parts_overlap"), "{repairs}"); + assert!(!messages.iter().any(|m| m.contains("duplicate")), "{messages:?}"); + // An island in another part's hole, and a bowtie part well apart: no overlap. + let (got, repairs, _) = one_by("Even-Odd", multi(&[&[SQUARE, inner], &[middle], &[far]])); + assert_eq!(repairs, "self_intersection, even_odd"); + assert_eq!(got.map(|g| area(&g)), Some(100.0 - 36.0 + 4.0 + 2.0)); + // A part whose exterior ring has no area takes its holes with it, as under the default. + let flat: Ring = &[(0., 0.), (5., 0.), (10., 0.), (0., 0.)]; + let g = poly(&[flat, HOLE]); + assert_eq!(one_by("Even-Odd", g.clone()).0, one_by("Shells Minus Holes", g).0); + // A hole crossing itself with a lobe outside its exterior ring: said, as under the default. + let lobes: Ring = &[(8., 4.), (12., 6.), (12., 4.), (8., 6.), (8., 4.)]; + for rule in ["Shells Minus Holes", "Even-Odd"] { + let (_, repairs, _) = one_by(rule, poly(&[SQUARE, lobes])); + assert!(repairs.contains("hole_outside_shell"), "{rule}: {repairs}"); + } + // Parts meeting along a line are joined, and that is said. + let side: Ring = &[(10., 0.), (20., 0.), (20., 10.), (10., 10.), (10., 0.)]; + let (got, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[side], &[far]])); + assert!(repairs.contains("parts_overlap"), "{repairs}"); + assert!(messages.iter().any(|m| m.contains("joins parts that meet along a line")), "{messages:?}"); + assert_eq!(got.map(|g| area(&g)), Some(202.0)); +} + +/// What Even-Odd's notices say is what it did: a part repeated whole is left out as a duplicate, +/// not said as an overlap left out, and where two holes of a part overlap is area again. +#[test] +fn even_odd_notices_match_the_result() { + let (got, repairs, messages) = one_by("Even-Odd", multi(&[&[SQUARE], &[SQUARE]])); + assert_eq!(got.map(|g| area(&g)), Some(100.0), "{repairs}"); + assert!(messages.iter().any(|m| m.contains("1 duplicate ring was left out")), "{messages:?}"); + assert!(messages.iter().any(|m| m.contains("keeps a part repeated whole once")), "{messages:?}"); + let left: Ring = &[(2., 2.), (6., 2.), (6., 6.), (2., 6.), (2., 2.)]; + let right: Ring = &[(4., 4.), (8., 4.), (8., 8.), (4., 8.), (4., 4.)]; + let (got, _, messages) = one_by("Even-Odd", poly(&[SQUARE, left, right])); + assert_eq!(got.map(|g| area(&g)), Some(100.0 - 28.0 + 4.0)); + assert!(messages.iter().any(|m| m.contains("where two holes overlap is area again")), "{messages:?}"); +} diff --git a/crates/assistant/skills/geoprocessing/SKILL.md b/crates/assistant/skills/geoprocessing/SKILL.md index 8f3886a362..dfda3f2190 100644 --- a/crates/assistant/skills/geoprocessing/SKILL.md +++ b/crates/assistant/skills/geoprocessing/SKILL.md @@ -31,7 +31,10 @@ Based on the RustGIS user guide, Analysis › Geoprocessing tools repaired; features with a missing or non-finite coordinate, even in a hole, or left with no usable points or, for polygons, no area once repaired are written without geometry; valid points and lines are kept; other features too complex to check, or whose repair fails, - are written unchanged) + are written unchanged; optional `rule`: "Shells Minus Holes", the default, merges + overlapping parts and fills a ring listed as a part, or "Even-Odd", which keeps what lies + inside an odd number of rings, so overlaps are left out and a hole stored as a part stays a + hole) - selection: `selectByLocation` (`input`, `select`, `distance`, `invert`), `selectByAttributes` (`input`, `where`) - statistics: `countPoints` (Summarize Within: `polygons`, `points`, `statistics`: summary diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index afa9a23be5..78a8e5beb2 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1304,6 +1304,31 @@ How each problem is repaired: - Lines may cross themselves, so only their repeated vertices and their parts with fewer than 2 distinct points are repaired. Points have nothing to repair but non-finite coordinates. +**Rule** (`rule`) chooses what the rebuild takes to be inside, when rings cross, overlap or sit in +the wrong place. A valid polygon, or one fixed without moving a coordinate, comes out the same +either way. + +- **Shells Minus Holes** (the default) is the rebuild above: rings keep the roles the data gives + them. +- **Even-Odd** reads each ring's own area the same way, so a bowtie keeps both triangles and a ring + traced twice counts once, then keeps what lies inside an odd number of rings, whatever their + roles. This is close to the "linework" method of make-valid tools. Where two parts overlap is left + out, a ring listed as a part inside another is a hole, and what of a hole lies outside its + exterior ring is a part. It suits data whose ring roles may be wrong, such as a hole stored as a + part of its own: Shells Minus Holes fills it, Even-Odd keeps it a hole. Each part is combined + first, then the parts together, so a hole two parts both have stays out of both, and a part + whose exterior ring has no area is dropped with its holes, as under Shells Minus Holes. Duplicates, + a hole repeated in its own part or a part repeated whole (the same corners in the same order, + from any vertex and either way round, a vertex in the middle of a straight edge aside), are left + out first, since each pair would cancel out, and the tool says how many. Features rebuilt under Even-Odd have + `even_odd` in `repairs` after the codes. + +| Shape | Shells Minus Holes | Even-Odd | +| --- | --- | --- | +| Two overlapping squares | one merged polygon | two pieces, the overlap left out | +| A square with a ring listed as a part inside it | the square, filled | the square with a hole | +| A hole partly outside its square | the square with a notch | the square with a notch, and the part outside as its own piece | + When a ring of a part crosses itself, Check Geometry doesn't check that part against its other rings or the other parts; Repair Geometry checks the feature again after the fixes that move no coordinate, and adds `hole_outside_shell`, `rings_intersect` and `parts_overlap` when the rebuild