VectorCleaner

Fixes the invalid geometries of a vector, as polygons that cross themselves, and removes the features without a geometry. For polygons, as parcels digitized by hand, it also fills the small holes, cuts the narrow spikes, fills the narrow notches and gaps of the outline and finds or resolves the overlaps between neighbors. A second vector of lines shows where it changed something, to check the cleaning.

Details

Invalid geometries, as polygons that cross themselves, are fixed, keeping as much as possible of their shape and only their parts of the same dimension, and the features without a geometry or with an empty one are removed. This is done for points, lines and polygons.

Polygons can also be cleaned. Holes with an area up to the max hole area are filled. Holes, or the parts of holes, narrower than the min width are filled too, as the gap between two parcels digitized not exactly on top of each other. On the outline, the narrow spikes are cut and the narrow notches and gaps between parts filled, if they are longer than the min sliver length, so that the sharp corners stay. The width is the diameter of the biggest circle that fits in the piece, the length half its perimeter, which works also for bent slivers. A notch is in the outline of a single polygon, a gap is between two or more parts of the feature, which filling it joins: the filling of each can be switched off. Only the pieces to clean are touched, the rest of the outline keeps its vertices. A feature that is itself a sliver is removed. Each feature is cleaned on its own: in a vector of adjacent polygons, a filled hole or notch can overlap a neighbor.

The overlaps between polygons, also those made by the cleaning, can then be marked in the changes, or resolved: the overlap is kept by the larger of the two features, or by the one that comes first in the vector, as by priority, and removed from the other, which is cleaned again of the slivers it is left with. A feature can be removed this way, if it is all inside others.

The overlays round the coordinates of the pieces they create to a grid of the precision, which makes them robust against the floating point arithmetic. In degrees, as for EPSG:4326, the precision is taken at the equator. Areas, widths and lengths are in the units of the coordinate reference system: better use a projected one.

The changes vector marks where the cleaning happened, with the outlines of the invalid geometries before they were fixed, of the filled holes, of the cut spikes, of the filled notches and gaps and of the overlaps, each with the id of its feature (of both, for an overlap), the kind of change and its area.

Parameters

Fixes invalid geometries, removes the empty ones and cleans polygons of small holes and narrow slivers.

Toolbox folder: Vector Processing
Status: Certified
Keywords: JTS, Clean, Invalid, Holes, Slivers, Vector
Authors: Andrea Antonello
Class: org.hortonmachine.modules.VectorCleaner

Inputs

Parameter

Type

Unit

Default

Description

inMap

vector file

The vector to clean: points, lines or polygons.

pMaxHoleArea

double

300.0

The max area of the holes to fill. 0 fills none.

pMinWidth

double

10.0

The min width of holes and of spikes and notches of the outline: narrower ones are cleaned. 0 cleans none.

pMinSliverLength

double

30.0

The min length of a narrow spike or notch of the outline to be cleaned, to keep the sharp corners.

doFillNotches

boolean

true

Fill the narrow notches of the outline.

doFillGaps

boolean

true

Fill the narrow gaps between the parts of a feature, joining them.

pPrecision

String

mm

The precision of the overlays: mm, cm, dm or m. Allowed values: mm, cm, dm, m.

pOverlaps

String

none

What to do with the overlaps between polygons: none (not looked for), mark (only added to the changes), largest (kept by the larger feature) or first (kept by the feature that comes first). Allowed values: none, mark, largest, first.

Outputs

Parameter

Type

Unit

Description

outMap

output file

The cleaned features, with their attributes.

outChanges

output file

The outlines of what was fixed or cleaned, as lines.

Example

The example writes some parcels with the usual errors of digitizing, then cleans them of the holes up to 50 m², of what is narrower than 5 m and of the overlaps, which the larger parcel keeps:

import org.hortonmachine.gears.io.vectorwriter.OmsVectorWriter
import org.hortonmachine.gears.utils.crs.HMCrsRegistry
import org.hortonmachine.gears.utils.features.FeatureUtilities
import org.hortonmachine.modules.VectorCleaner
import org.locationtech.jts.geom.Geometry
import org.locationtech.jts.geom.util.AffineTransformation
import org.locationtech.jts.io.WKTReader

// the folder with the elevation model: change it to yours
var folder = "/data/flanginec/"

// some parcels digitized by hand, with the usual errors, in meters from a corner
var parcels = [
    // the outline crosses itself
    "MULTIPOLYGON (((20 20, 120 120, 120 20, 20 120, 20 20)))",
    // a small hole of 36 m2, a narrow one of 2 m and a big one
    "MULTIPOLYGON (((150 20, 300 20, 300 140, 150 140, 150 20), (170 40, 176 40, 176 46, 170 46, 170 40), "
            + "(190 80, 192 80, 192 130, 190 130, 190 80), (220 60, 260 60, 260 100, 220 100, 220 60)))",
    // a spike of 2 m
    "MULTIPOLYGON (((20 160, 120 160, 120 200, 165 200, 165 202, 120 202, 120 260, 20 260, 20 160)))",
    // a notch of 2 m
    "MULTIPOLYGON (((170 160, 300 160, 300 260, 170 260, 170 212, 260 212, 260 210, 170 210, 170 160)))",
    // two neighbors that overlap by 4 m
    "MULTIPOLYGON (((20 290, 140 290, 140 380, 20 380, 20 290)))",
    "MULTIPOLYGON (((136 290, 260 290, 260 380, 136 380, 136 290)))",
    // two parts with a gap of 1.5 m between them
    "MULTIPOLYGON (((280 290, 330 290, 330 380, 280 380, 280 290)), ((331.5 290, 380 290, 380 380, 331.5 380, 331.5 290)))"
]
var reader = new WKTReader()
// moved into the area of the elevation model
var shift = AffineTransformation.translationInstance(1638500, 5112500)
var geometries = parcels.withIndex().collect { wkt, i ->
    var geometry = shift.transform(reader.read(wkt))
    // the number of the parcel, written in the userdata field
    geometry.userData = i + 1
    geometry
} as Geometry[]
var crs = HMCrsRegistry.INSTANCE.getCrs("EPSG:3003")
OmsVectorWriter.writeVector(folder + "parcels.gpkg#parcels", FeatureUtilities.featureCollectionFromGeometry(crs, geometries))

var cleaner = new VectorCleaner()
cleaner.inMap = folder + "parcels.gpkg#parcels"
// fill the holes up to 50 m2
cleaner.pMaxHoleArea = 50
// cut the spikes and fill the holes, notches and gaps narrower than 5 m, if longer than 30 m
cleaner.pMinWidth = 5
cleaner.pMinSliverLength = 30
// the larger of two neighbors keeps their overlap
cleaner.pOverlaps = "largest"
cleaner.outMap = folder + "parcels.gpkg#parcels_cleaned"
cleaner.outChanges = folder + "parcels.gpkg#parcels_changes"
cleaner.process()

The parcels before the cleaning, each in its color. The bow-tie of parcel 1 crosses itself, parcel 2 has a small, a narrow and a big hole, parcel 3 a spike and parcel 4 a notch, parcels 5 and 6 overlap along their border and the two parts of parcel 7 are divided by a narrow gap:

The parcels before the cleaning

After the cleaning, with the changes outlined in blue, the bow-tie is made of two valid triangles, the small and the narrow holes are filled while the big one stays, the spike is cut, the notch and the gap are filled and the overlap belongs to parcel 6 only:

The parcels after the cleaning

The module prints the counts and the areas of what it changed, as the total area before and after the cleaning.