ST_MakeValid
Signatures
GEOMETRY ST_MakeValid(GEOMETRY geom);
GEOMETRY ST_MakeValid(GEOMETRY geom,
BOOLEAN preserveGeomDim);
GEOMETRY ST_MakeValid(GEOMETRY geom,
BOOLEAN preserveGeomDim,
BOOLEAN preserveDuplicateCoord);
GEOMETRY ST_MakeValid(GEOMETRY geom,
BOOLEAN preserveGeomDim,
BOOLEAN preserveDuplicateCoord,
BOOLEAN preserveCoordDim);
Description
Repair an invalid geometry.
Here, geometry can be (multi)point, (multi)linestring, (multi)polygon or geometryCollection.
Parameter: preserveGeomDim
Value |
Description |
Default value |
|---|---|---|
|
Remove degenerated geometries from the result, |
x |
|
It is up to the client to filter degenerate geometries |
Note that:
A multi-geometry will always produce a multi-geometry (eventually empty or made of a single component).
A simple
geometrymay produce a multi-geometry (i.e polygon with self-intersection will generally produce a multi-polygon). In this case, it is up to the client to explode multi-geometries if needed.
Warning
Linear geometries (dim = 1): duplicate coordinates are preserved as much as possible.
Aeral geometries (dim = 2): duplicate coordinates are generally removed due to the use of overlay operations.
Parameter: preserveDuplicateCoord
Value |
Description |
Default value |
|---|---|---|
|
Preserve duplicate coordinates as much as possible. |
x |
|
All duplicated coordinates are removed |
Parameter: preserveCoordDim
Value |
Description |
Default value |
|---|---|---|
|
Preserves third and fourth ordinates |
x |
|
Preserves third ordinates but not fourth one |
Warning
Note that the fourth dimension is not yet supported in H2GIS. So for the moment, preserveCoordDim has no impact since third ordinates (z) will always be preserved.
Warning
ST_MakeValid may add new points to node the original set of lines (especially to make polygons valid). New points just have x and y. No interpolation is performed if original geometry is in 3D or 4D.
Examples
With Point
SELECT ST_MakeValid('POINT(0 0)');
-- Answer: POINT(0 0)
Also works with z coordinates
SELECT ST_MakeValid('POINT(1 2 3)');
-- Answer: POINT(1 2 3)
With Linestring
SELECT ST_MakeValid('
LINESTRING(0 0, 10 0, 20 0, 20 0, 30 0)');
-- Answer: LINESTRING(0 0, 10 0, 20 0, 20 0, 30 0)
Same example but with preserveDuplicateCoord = false. So here duplicated coordinates are removed
SELECT ST_MakeValid('
LINESTRING(0 0, 10 0, 20 0, 20 0, 30 0)', true, false);
-- Answer: LINESTRING(0 0, 10 0, 20 0, 30 0)
Same example but with z coordinates
SELECT ST_MakeValid('
LINESTRING(0 0 1, 10 0 2, 20 0 1, 20 0 1, 30 0 1)',
true,false);
-- Answer: LINESTRING(0 0 1, 10 0 2, 20 0 1, 30 0 1)
Example with preserveGeomDim
True
SELECT ST_MakeValid('
LINESTRING(1 1, 1 1)', true);
-- Answer: LINESTRING EMPTY
False
SELECT ST_MakeValid('
LINESTRING(1 1, 1 1)', false);
-- Answer: POINT (1 1)
With Polygon
SELECT ST_MakeValid('
POLYGON((1 5, 1 1, 3 3, 5 3, 7 1, 7 5, 5 3, 3 3, 1 5))');
-- Answer: MULTIPOLYGON(((3 3, 1 1, 1 5, 3 3)),
-- ((5 3, 7 5, 7 1, 5 3)))

SELECT ST_MakeValid('
MULTIPOLYGON(((0 0, 3 0, 3 3, 0 3, 0 0)),
((3 0, 6 0, 6 3, 3 3, 3 0)))', false);
-- Answer: MULTIPOLYGON(((3 0, 0 0, 0 3, 3 3, 6 3, 6 0, 3 0)))

SELECT ST_MakeValid('
POLYGON ((1 1, 1 3, 3 3, 3 2, 2 2, 3 2, 3 1, 1 1))');
-- Answer: POLYGON((3 2, 3 1, 1 1, 1 3, 3 3, 3 2))

SELECT ST_MakeValid('
POLYGON ((1 1, 3 1, 0 2, 3 3, 1 3, 1 1))');
-- Answer: MULTIPOLYGON(
-- ((1 2.33, 1 1.66, 0 2, 1 2.33)),
-- ((1 1.66, 3 1, 1 1, 1 1.66)),
-- ((1 2.33, 1 3, 3 3, 1 2.33)))
Same example but with z coordinates. Here, created nodes have no z information.
SELECT ST_MakeValid('
POLYGON ((1 1 0, 3 1 1, 0 2 1, 3 3 0, 1 3 1, 1 1 0))');
-- Answer: MULTIPOLYGON (
-- ((1 2.33, 1 1.66, 0 2 1, 1 2.33)),
-- ((1 1.66, 3 1 1, 1 1 0, 1 1.66)),
-- ((1 2.33, 1 3 1, 3 3 0, 1 2.33)))

SELECT ST_MakeValid('
POLYGON((1 1, 1 4, 6 4, 4 4, 1 1))');
-- Answer: POLYGON((4 4, 1 1, 1 4, 4 4))

SELECT ST_MakeValid('
MULTIPOLYGON(((1 1, 1 3, 2 2, 0 2, 1 1)),
((3 3, 3 1, 4 1, 5 3, 4 3, 5 3, 3 3)))');
-- Answer: MULTIPOLYGON(((1 2, 1 1, 0 2, 1 2)),
-- ((1 2, 1 3, 2 2, 1 2)),
-- ((5 3, 4 1, 3 1, 3 3, 4 3, 5 3)))

With GeometryCollection
SELECT ST_MakeValid('
GEOMETRYCOLLECTION (
POLYGON ((1 1, 1 1, 1 3, 2 2, 0 2, 1 1)),
LINESTRING (3 1, 3 3, 3 3, 4 3),
POINT (5 2))');
-- Answer: GEOMETRYCOLLECTION (
-- POLYGON ((1 2, 1 1, 0 2, 1 2)),
-- POLYGON ((1 2, 1 3, 2 2, 1 2)),
-- LINESTRING (3 1, 3 3, 3 3, 4 3),
-- POINT (5 2))
Same example but with preserveDuplicateCoord = false
SELECT ST_MakeValid('
GEOMETRYCOLLECTION (
POLYGON ((1 1, 1 1, 1 3, 2 2, 0 2, 1 1)),
LINESTRING (3 1, 3 3, 3 3, 4 3),
POINT (5 2))', true, false);
-- Answer: GEOMETRYCOLLECTION (
-- POLYGON ((1 2, 1 1, 0 2, 1 2)),
-- POLYGON ((1 2, 1 3, 2 2, 1 2)),
-- LINESTRING (3 1, 3 3, 4 3),
-- POINT (5 2))
