Name

ST_CoverageClean — Computes a clean (edge matched, non-overlapping, gap-cleared) polygonal coverage, given a non-clean input.

Synopsis

geometry ST_CoverageClean(geometry winset geom, float8 gapMaximumWidth = 0, float8 snappingDistance = -1, text overlapMergeStrategy = 'MERGE_LONGEST_BORDER');

Description

A window function which adjusts the edges of a polygonal dataset to ensure that none of the polygons overlap, narrow gaps are removed, and shared edges are identically noded. The result is a clean coverage that will pass validation by ST_CoverageInvalidEdges and can be input to coverage processing functions.

gapMaximumWidth controls the cleaning of gaps between polygons. Gaps with width less than this distance are merged into an adjacent polygon.

snappingDistance controls snapping of close vertices and edges. The default setting (-1) use an automatic snapping distance based on the input. Set to 0.0 to turn off snapping.

overlapMergeStrategy specifies how overlapping areas are merged into an adjacent polygon:

  • MERGE_LONGEST_BORDER - merges into polygon with longest common border

  • MERGE_MAX_AREA - merges into polygon with maximum area

  • MERGE_MIN_AREA - merges into polygon with minimum area

  • MERGE_MIN_INDEX - merges into polygon with smallest input index (defined by order of input polygons)

Availability: 3.6.0 - requires GEOS >= 3.14.0

Examples

-- Populate demo table
CREATE TABLE example AS SELECT * FROM (VALUES
  (1, 'POLYGON ((10 190, 30 160, 40 110, 100 70, 120 10, 10 10, 10 190))'::geometry),
  (2, 'POLYGON ((100 190, 10 190, 30 160, 40 110, 50 80, 74 110.5, 100 130, 140 120, 140 160, 100 190))'::geometry),
  (3, 'POLYGON ((140 190, 190 190, 190 80, 140 80, 140 190))'::geometry),
  (4, 'POLYGON ((180 40, 120 10, 100 70, 140 80, 190 80, 180 40))'::geometry)
) AS v(id, geom);

-- Prove it is a dirty coverage
SELECT ST_AsText(ST_CoverageInvalidEdges(geom) OVER ())
  FROM example;

-- Clean the coverage
CREATE TABLE example_clean AS
  SELECT id, ST_CoverageClean(geom) OVER () AS GEOM
  FROM example;

-- Prove it is a clean coverage
SELECT ST_AsText(ST_CoverageInvalidEdges(geom) OVER ())
  FROM example_clean;