Name

ST_MakeValid — Attempts to make an invalid geometry valid without losing vertices.

Synopsis

geometry ST_MakeValid(geometry input);

Description

The function attempts to create a valid representation of a given invalid geometry without losing any of the input vertices. Already-valid geometries are returned without further intervention.

Supported inputs are: POINTS, MULTIPOINTS, LINESTRINGS, MULTILINESTRINGS, POLYGONS, MULTIPOLYGONS and GEOMETRYCOLLECTIONS containing any mix of them.

In case of full or partial dimensional collapses, the output geometry may be a collection of lower-to-equal dimension geometries or a geometry of lower dimension.

Single polygons may become multi-geometries in case of self-intersections.

Performed by the GEOS module.

Availability: 2.0.0

Enhanced: 2.0.1, speed improvements

Enhanced: 2.1.0, added support for GEOMETRYCOLLECTION and MULTIPOINT.

Enhanced: 3.1.0, added removal of Coordinates with NaN values.

This function supports 3d and will not drop the z-index.

Examples

before_geom: MULTIPOLYGON of 2 overlapping polygons

after_geom: MULTIPOLYGON of 4 non-overlapping polygons

SELECT f.geom AS before_geom, ST_MakeValid( f.geom) AS after_geom
FROM (SELECT 'MULTIPOLYGON(((186 194,187 194,188 195,189 195,190 195,
191 195,192 195,193 194,194 194,194 193,195 192,195 191,
195 190,195 189,195 188,194 187,194 186,14 6,13 6,12 5,11 5,
10 5,9 5,8 5,7 6,6 6,6 7,5 8,5 9,5 10,5 11,5 12,6 13,6 14,186 194)),
((150 90,149 80,146 71,142 62,135 55,128 48,119 44,110 41,100 40,
90 41,81 44,72 48,65 55,58 62,54 71,51 80,50 90,51 100,
54 109,58 118,65 125,72 132,81 136,90 139,100 140,110 139,
119 136,128 132,135 125,142 118,146 109,149 100,150 90)))'::geometry AS geom) AS f;

before_geom: MULTIPOLYGON of 6 overlapping polygons

after_geom: MULTIPOLYGON of 14 Non-overlapping polygons

SELECT c.geom AS before_geom, ST_MakeValid(c.geom) AS after_geom
	FROM (SELECT 'MULTIPOLYGON(((91 50,79 22,51 10,23 22,11 50,23 78,51 90,79 78,91 50)),
		  ((91 100,79 72,51 60,23 72,11 100,23 128,51 140,79 128,91 100)),
		  ((91 150,79 122,51 110,23 122,11 150,23 178,51 190,79 178,91 150)),
		  ((141 50,129 22,101 10,73 22,61 50,73 78,101 90,129 78,141 50)),
		  ((141 100,129 72,101 60,73 72,61 100,73 128,101 140,129 128,141 100)),
		  ((141 150,129 122,101 110,73 122,61 150,73 178,101 190,129 178,141 150)))'::geometry AS geom) AS c;

See Also

ST_IsValid, ST_Collect, ST_CollectionExtract