Clean-up: remove holes and small parts, snap to a grid, reduce precision
Four operations that tidy a geometry: RemoveHoles and RemoveSmallParts drop what is too small to matter, SnapToGrid rounds the coordinates, and ReducePrecision does the same and keeps the result valid. The first three only filter or round; ReducePrecision goes through the snap-rounding overlay core.
German version: 0027-bereinigen-loecher-kleinteile-raster.de.md
Consequences
- Placement: all four in the new static class
CleaninginSpatialNET.Extensions, namespaceSpatialNET. Results come from the factory and are restricted to what it supports (ADR 0019). For the package validation this is an addition only. RemoveHoles(polygon, minArea = double.PositiveInfinity, factory?): removes the holes whose area is smaller thanminArea; the others stay. With the default every hole is removed;minArea = 0keeps every hole. A part, an island and a shell are never touched. The rings are normalized (ADR 0003). Only forIPolygon2D: other kinds have no holes.RemoveSmallParts: polygon: removes the parts whose area (shell minus its holes) is smaller than the limit; polyline: removes the paths shorter than the limit; the overload forIGeometry2Ddispatches (its parameter is calledminSize: area for a polygon, length for a polyline) and copies points and multipoints, which have no size. A value equal to the limit stays.- Limits (ADR 0012):
minArea,minLengthandminSizemust not be negative orNaN, otherwiseArgumentOutOfRangeException; infinity is allowed. SnapToGrid(geometry, gridSize, factory?): rounds X and Y of every vertex to the nearest multiple of the grid size (halves away from zero) and nothing else. Consecutive vertices that fall together are merged (Z and M of the first), a path with fewer than two and a ring with fewer than three distinct vertices vanish, and a part whose shell vanishes goes with its holes. The points of a multipoint all stay. The result may be invalid: rounding can make edges cross or touch. This is the cheap, predictable operation, and the name says so; for a valid result there isReducePrecision.ArgumentOutOfRangeExceptionfor a grid size that is not finite or not greater than 0,InvalidOperationExceptionif a rounded coordinate leaves the range ofdouble.ReducePrecision(geometry, gridSize, factory?): the geometry is snap-rounded to the grid by the overlay core and the result is valid: a polygon goes throughMakeValidunder even-odd (ADR 0020), so parts that collapse vanish and the result is a valid polygon; a polyline goes throughUnionwith an empty one (ADR 0018), so it is noded and its duplicates collapse; a multipoint loses its duplicates the same way; a point is rounded. Every vertex of the result lies on the grid. The grid size is required here (no cascade): reducing precision without saying to what makes no sense.- Tests (ADR 0008):
[Covers]tests instead of fixtures. Hand cases for each method; property tests:RemoveHolesandRemoveSmallPartsgive the area of an independent computation on the NTS parts;SnapToGridputs every vertex on the grid within half a cell diagonal of an input vertex and is idempotent;ReducePrecisionof polygons is valid, on the grid and within a tolerance of the area of the NTSGeometryPrecisionReducer, of lines on the grid and within two cells (Hausdorff) of the original. A weekly stress test reduces dirty polygons and checks validity and the grid. - Playground and documentation: descriptors for all four (ADR 0011), a section in the concept page for the simplifying and validating operations, glossary entries.
Considered Options
RemoveHoles(minArea = 0)meaning "all holes" (rejected: it reads as "holes of at least 0", which keeps them all; the limit is a threshold below which holes go, and no limit removes all)- A single
Reducethat decides between snapping and noding (rejected: the two differ in cost and in the guarantee of validity, and the caller should choose) SnapToGridthrough the noder as well (rejected: it would beReducePrecision)- Removing small parts by the area of the shell alone (rejected: a ring of land around a lake would count as large; the net area is what the part covers)
- Removing duplicates in
SnapToGridfor multipoints (rejected:SnapToGridrounds and merges neighbours; set semantics are forReducePrecisionandMakeValidleaves duplicates as well, ADR 0020)