Description
The function attempts to create a valid representation of a given invalid
geometry without losing any of the input vertices.
Valid geometries are returned unchanged.
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.
The params argument can be used to supply an options
string to select the method to use for building valid geometry.
The options string is in the format "method=linework|structure keepcollapsed=true|false".
If no "params" argument is provided, the "linework" algorithm will be used as the default.
The "method" key has two values.
"linework" is the default algorithm, and builds valid geometries
by first extracting all lines, noding that linework together, then building
a value output from the linework. The requirement that no vertices are
lost can generate complex collections as outputs.
"structure" is an algorithm that distinguishes between
interior and exterior rings, building new geometry by unioning
exterior rings, and then differencing all interior rings. The results
tend to be more intuitive, where collapsed lines are discarded in
the output.
The "keepcollapsed" key is only valid for the "structure" algorithm,
and takes a value of "true" or "false".
Keep collapsed of "false" is the default, and means that
geometry components that collapse to a lower dimensionality,
for example a one-point linestring will be dropped.
Keep collapsed of "true" means
geometry components that collapse to a lower dimensionality
will be retained.
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.
Enhanced: 3.2.0, added algorithm options, 'linework' and 'structure' which requires GEOS >= 3.10.0.
This function supports 3d and will not drop the z-index.
Examples
Make valid a MultiPolygon of two overlapping polygons using the linework and structure methods.
SELECT ST_MakeValid(before_geom) AS linework,
ST_MakeValid(before_geom, 'method=structure') AS structure
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 before_geom) AS f;
MULTIPOLYGON(((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,56.769 64.769,58 62,64 56,14 6,13 6,12 5,11 5)),((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,125.231 133.231,56.769 64.769,54 71)),((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,134 126,128 132,125.231 133.231,186 194)),((135 125,142 118,146 109,149 100,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,64 56,134 126,135 125))) | MULTIPOLYGON(((149 80,146 71,142 62,135 55,128 48,119 44,110 41,100 40,90 41,81 44,72 48,65 55,64 56,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,56.769 64.769,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,125.231 133.231,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,134 126,135 125,142 118,146 109,149 100,150 90,149 80)))
01060000000400000001030000000100000015000000000000000000264000000000000014400000000000002440000000000000144000000000000022400000000000001440000000000000204000000000000014400000000000001C4000000000000018400000000000001840000000000000184000000000000018400000000000001C40000000000000144000000000000020400000000000001440000000000000224000000000000014400000000000002440000000000000144000000000000026400000000000001440000000000000284000000000000018400000000000002A4000000000000018400000000000002C402776622776624C40143BB1133B3150400000000000004D400000000000004F4000000000000050400000000000004C400000000000002C4000000000000018400000000000002A4000000000000018400000000000002840000000000000144000000000000026400000000000001440010300000001000000100000000000000000004B400000000000C051400000000000804940000000000000544000000000000049400000000000805640000000000080494000000000000059400000000000004B400000000000405B400000000000004D400000000000805D4000000000004050400000000000405F40000000000000524000000000008060400000000000405440000000000000614000000000008056400000000000606140000000000000594000000000008061400000000000805B4000000000006061400000000000C05D400000000000006140ECC44EECC44E5F407662277662A760402776622776624C40143BB1133B3150400000000000004B400000000000C05140010300000001000000150000000000000000406740000000000040684000000000006067400000000000406840000000000080674000000000006068400000000000A0674000000000006068400000000000C0674000000000006068400000000000E067400000000000606840000000000000684000000000006068400000000000206840000000000040684000000000004068400000000000406840000000000040684000000000002068400000000000606840000000000000684000000000006068400000000000E0674000000000006068400000000000C0674000000000006068400000000000A067400000000000606840000000000080674000000000004068400000000000606740000000000040684000000000004067400000000000C060400000000000805F4000000000000060400000000000806040ECC44EECC44E5F407662277662A7604000000000004067400000000000406840010300000001000000140000000000000000E060400000000000405F400000000000C061400000000000805D4000000000004062400000000000405B400000000000A0624000000000000059400000000000C0624000000000008056400000000000A06240000000000000544000000000004062400000000000C051400000000000C061400000000000004F400000000000E060400000000000804B40000000000000604000000000000048400000000000C05D4000000000000046400000000000805B4000000000008044400000000000005940000000000000444000000000008056400000000000804440000000000040544000000000000046400000000000005240000000000000484000000000004050400000000000804B4000000000000050400000000000004C400000000000C060400000000000805F400000000000E060400000000000405F40 | 010600000001000000010300000001000000450000000000000000A06240000000000000544000000000004062400000000000C051400000000000C061400000000000004F400000000000E060400000000000804B40000000000000604000000000000048400000000000C05D4000000000000046400000000000805B4000000000008044400000000000005940000000000000444000000000008056400000000000804440000000000040544000000000000046400000000000005240000000000000484000000000004050400000000000804B4000000000000050400000000000004C400000000000002C4000000000000018400000000000002A40000000000000184000000000000028400000000000001440000000000000264000000000000014400000000000002440000000000000144000000000000022400000000000001440000000000000204000000000000014400000000000001C4000000000000018400000000000001840000000000000184000000000000018400000000000001C40000000000000144000000000000020400000000000001440000000000000224000000000000014400000000000002440000000000000144000000000000026400000000000001440000000000000284000000000000018400000000000002A4000000000000018400000000000002C402776622776624C40143BB1133B3150400000000000004B400000000000C051400000000000804940000000000000544000000000000049400000000000805640000000000080494000000000000059400000000000004B400000000000405B400000000000004D400000000000805D4000000000004050400000000000405F40000000000000524000000000008060400000000000405440000000000000614000000000008056400000000000606140000000000000594000000000008061400000000000805B4000000000006061400000000000C05D400000000000006140ECC44EECC44E5F407662277662A760400000000000406740000000000040684000000000006067400000000000406840000000000080674000000000006068400000000000A0674000000000006068400000000000C0674000000000006068400000000000E067400000000000606840000000000000684000000000006068400000000000206840000000000040684000000000004068400000000000406840000000000040684000000000002068400000000000606840000000000000684000000000006068400000000000E0674000000000006068400000000000C0674000000000006068400000000000A067400000000000606840000000000080674000000000004068400000000000606740000000000040684000000000004067400000000000C060400000000000805F400000000000E060400000000000405F400000000000C061400000000000805D4000000000004062400000000000405B400000000000A0624000000000000059400000000000C0624000000000008056400000000000A062400000000000005440
Make valid a MultiPolygon of six overlapping polygons using the linework and structure methods.
SELECT ST_MakeValid(before_geom) AS linework,
ST_MakeValid(before_geom, 'method=structure') AS structure
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 before_geom) AS c;
MULTIPOLYGON(((51 110,68.5 117.5,61 100,68.5 82.5,51 90,23 78,21.714 75,11 100,21.714 125,23 122,51 110)),((51 190,76 179.286,73 178,61 150,68.5 132.5,51 140,23 128,21.714 125,11 150,23 178,51 190)),((141 150,130.286 125,129 128,101 140,83.5 132.5,91 150,79 178,76 179.286,101 190,129 178,141 150)),((129 78,101 90,83.5 82.5,91 100,83.5 117.5,101 110,129 122,130.286 125,141 100,130.286 75,129 78)),((101 10,76 20.714,79 22,91 50,83.5 67.5,101 60,129 72,130.286 75,141 50,129 22,101 10)),((11 50,21.714 75,23 72,51 60,68.5 67.5,61 50,73 22,76 20.714,51 10,23 22,11 50)),((83.5 82.5,80.286 75,79 78,76 79.286,83.5 82.5)),((83.5 67.5,76 70.714,79 72,80.286 75,83.5 67.5)),((68.5 67.5,71.714 75,73 72,76 70.714,68.5 67.5)),((68.5 82.5,76 79.286,73 78,71.714 75,68.5 82.5)),((83.5 132.5,80.286 125,79 128,76 129.286,83.5 132.5)),((83.5 117.5,76 120.714,79 122,80.286 125,83.5 117.5)),((68.5 117.5,71.714 125,73 122,76 120.714,68.5 117.5)),((68.5 132.5,76 129.286,73 128,71.714 125,68.5 132.5))) | MULTIPOLYGON(((23 22,11 50,21.714 75,11 100,21.714 125,11 150,23 178,51 190,76 179.286,101 190,129 178,141 150,130.286 125,141 100,130.286 75,141 50,129 22,101 10,76 20.714,51 10,23 22)))
01060000000E0000000103000000010000000B00000000000000008049400000000000805B4000000000002051400000000000605D400000000000804E40000000000000594000000000002051400000000000A0544000000000008049400000000000805640000000000000374000000000008053406EDBB66DDBB635400000000000C05240000000000000264000000000000059406EDBB66DDBB635400000000000405F4000000000000037400000000000805E4000000000008049400000000000805B400103000000010000000B00000000000000008049400000000000C0674000000000000053409224499224696640000000000040524000000000004066400000000000804E400000000000C062400000000000205140000000000090604000000000008049400000000000806140000000000000374000000000000060406EDBB66DDBB635400000000000405F4000000000000026400000000000C062400000000000003740000000000040664000000000008049400000000000C067400103000000010000000B0000000000000000A061400000000000C0624092244992244960400000000000405F4000000000002060400000000000006040000000000040594000000000008061400000000000E0544000000000009060400000000000C056400000000000C062400000000000C0534000000000004066400000000000005340922449922469664000000000004059400000000000C06740000000000020604000000000004066400000000000A061400000000000C062400103000000010000000B00000000000000002060400000000000805340000000000040594000000000008056400000000000E054400000000000A054400000000000C0564000000000000059400000000000E054400000000000605D4000000000004059400000000000805B4000000000002060400000000000805E4092244992244960400000000000405F400000000000A06140000000000000594092244992244960400000000000C05240000000000020604000000000008053400103000000010000000B0000000000000000405940000000000000244000000000000053406EDBB66DDBB634400000000000C0534000000000000036400000000000C0564000000000000049400000000000E054400000000000E0504000000000004059400000000000004E400000000000206040000000000000524092244992244960400000000000C052400000000000A06140000000000000494000000000002060400000000000003640000000000040594000000000000024400103000000010000000B000000000000000000264000000000000049406EDBB66DDBB635400000000000C052400000000000003740000000000000524000000000008049400000000000004E4000000000002051400000000000E050400000000000804E4000000000000049400000000000405240000000000000364000000000000053406EDBB66DDBB63440000000000080494000000000000024400000000000003740000000000000364000000000000026400000000000004940010300000001000000050000000000000000E054400000000000A0544025499224491254400000000000C052400000000000C05340000000000080534000000000000053402549922449D253400000000000E054400000000000A05440010300000001000000050000000000000000E054400000000000E050400000000000005340DBB66DDBB6AD51400000000000C05340000000000000524025499224491254400000000000C052400000000000E054400000000000E050400103000000010000000500000000000000002051400000000000E05040DBB66DDBB6ED51400000000000C05240000000000040524000000000000052400000000000005340DBB66DDBB6AD514000000000002051400000000000E050400103000000010000000500000000000000002051400000000000A0544000000000000053402549922449D2534000000000004052400000000000805340DBB66DDBB6ED51400000000000C0524000000000002051400000000000A05440010300000001000000050000000000000000E05440000000000090604025499224491254400000000000405F400000000000C053400000000000006040000000000000534092244992242960400000000000E054400000000000906040010300000001000000050000000000000000E054400000000000605D400000000000005340DBB66DDBB62D5E400000000000C053400000000000805E4025499224491254400000000000405F400000000000E054400000000000605D400103000000010000000500000000000000002051400000000000605D40DBB66DDBB6ED51400000000000405F4000000000004052400000000000805E400000000000005340DBB66DDBB62D5E4000000000002051400000000000605D4001030000000100000005000000000000000020514000000000009060400000000000005340922449922429604000000000004052400000000000006040DBB66DDBB6ED51400000000000405F4000000000002051400000000000906040 | 0106000000010000000103000000010000001500000000000000000037400000000000003640000000000000264000000000000049406EDBB66DDBB635400000000000C05240000000000000264000000000000059406EDBB66DDBB635400000000000405F4000000000000026400000000000C062400000000000003740000000000040664000000000008049400000000000C067400000000000005340922449922469664000000000004059400000000000C06740000000000020604000000000004066400000000000A061400000000000C0624092244992244960400000000000405F400000000000A06140000000000000594092244992244960400000000000C052400000000000A061400000000000004940000000000020604000000000000036400000000000405940000000000000244000000000000053406EDBB66DDBB634400000000000804940000000000000244000000000000037400000000000003640