Geometry Validation
These functions test whether geometries are valid according to
the OGC SFS standard.
They also provide information about the nature and location of invalidity.
There is also a function to create a valid geometry out of an invalid one.
ST_IsValid
Tests if a geometry is well-formed in 2D.
boolean ST_IsValid
geometry g
boolean ST_IsValid
geometry g
integer flags
Description
Tests if an ST_Geometry value is well-formed and valid in 2D according to the OGC rules.
For geometries with 3 and 4 dimensions, the validity is still only tested in 2 dimensions.
For geometries that are invalid, a PostgreSQL NOTICE is emitted providing details of why it is not valid.
For the version with the flags parameter,
supported values are documented in
This version does not print a NOTICE explaining invalidity.
For more information on the definition of geometry validity, refer
to
SQL-MM defines the result of ST_IsValid(NULL) to be 0, while
PostGIS returns NULL.
Performed by the GEOS module.
The version accepting flags is available starting with 2.0.0.
&sfs_compliant;
&sqlmm_compliant; SQL-MM 3: 5.1.9
Neither OGC-SFS nor SQL-MM specifications include a flag argument
for ST_IsValid. The flag is a PostGIS extension.
Examples
SELECT ST_IsValid('LINESTRING(0 0,1 1)') As good_line,
ST_IsValid('POLYGON((0 0,1 1,1 2,1 1,0 0))') As bad_poly;
NOTICE: Self-intersection at or near point 0 0
good_line | bad_poly
-----------+----------
t | f
Find tables with invalid geometries in registered geometry columns.
0 THEN
INSERT INTO invalid_geometry_columns
VALUES (
format('%I.%I', rec.f_table_schema, rec.f_table_name),
rec.f_geometry_column,
invalid_count
);
END IF;
END LOOP;
END;
$$;
SELECT * FROM invalid_geometry_columns;
]]>
See Also
,
,
,
ST_IsValidDetail
Returns a valid_detail row stating if a geometry is valid or if not a reason and a location.
valid_detail ST_IsValidDetail
geometry geom
integer flags
Description
Returns a valid_detail row,
containing a boolean (valid) stating if a geometry is valid,
a varchar (reason) stating a reason why it is invalid
and a geometry (location) pointing out where it is invalid.
Useful to improve on the combination of and to generate a detailed report of invalid geometries.
The optional flags parameter is a bitfield. It can have the following values:
0: Use usual OGC SFS validity semantics.
1: Consider certain kinds of self-touching rings (inverted shells and exverted holes) as valid.
This is also known as "the ESRI flag", since this is the validity model used by those tools.
Note that this is invalid under the OGC model.
Performed by the GEOS module.
Availability: 2.0.0
Examples
This example returns the first three rejects from a successful quintuplet experiment.
y1*0.5 AND z1 < x1*y1) As e
INNER JOIN (SELECT ST_Translate(ST_ExteriorRing(ST_Buffer(ST_Point(x1*10,y1), z1)),y1*1, z1*2) As line
FROM generate_series(-3,6) x1
CROSS JOIN generate_series(2,5) y1
CROSS JOIN generate_series(1,10) z1
WHERE x1 > y1*0.75 AND z1 < x1*y1) As f
ON (ST_Area(e.buff) > 78 AND ST_Contains(e.buff, f.line))
GROUP BY gid, e.buff) As quintuplet_experiment
WHERE ST_IsValid(geom) = false
ORDER BY gid
LIMIT 3;]]>
This example reports a valid LineString.
SELECT * FROM ST_IsValidDetail('LINESTRING(220227 150406,2220227 150407,222020 150410)');
See Also
,
ST_IsValidReason
Returns text stating if a geometry is valid, or a reason for invalidity.
text ST_IsValidReason
geometry geomA
text ST_IsValidReason
geometry geomA
integer flags
Description
Returns text stating if a geometry is valid, or if invalid a reason why.
Useful in combination with to generate a detailed report of invalid geometries and reasons.
Allowed flags are documented in .
Performed by the GEOS module.
Availability: 1.4
Availability: 2.0 version taking flags.
Examples
This example uses an invalid bow-tie polygon.
SELECT ST_IsValidReason(
'POLYGON ((100 200,100 100,200 200,
200 100,100 200))'::geometry) as validity_info;
Self-intersection[150 150]
This example returns the first three rejects from a successful quintuplet experiment.
y1*0.5 AND z1 < x1*y1) As e
INNER JOIN (SELECT ST_Translate(ST_ExteriorRing(ST_Buffer(ST_Point(x1*10,y1), z1)),y1*1, z1*2) As line
FROM generate_series(-3,6) x1
CROSS JOIN generate_series(2,5) y1
CROSS JOIN generate_series(1,10) z1
WHERE x1 > y1*0.75 AND z1 < x1*y1) As f
ON (ST_Area(e.buff) > 78 AND ST_Contains(e.buff, f.line))
GROUP BY gid, e.buff) As quintuplet_experiment
WHERE ST_IsValid(geom) = false
ORDER BY gid
LIMIT 3;]]>
Simple example.
SELECT ST_IsValidReason('LINESTRING(220227 150406,2220227 150407,222020 150410)');
See Also
,
ST_MakeValid
Attempts to make an invalid geometry valid without losing vertices.
geometry ST_MakeValid
geometry input
geometry ST_MakeValid
geometry input
text params
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.
&Z_support;
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)))
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)))
Collapsed Geometry Examples
SELECT ST_MakeValid(
'LINESTRING(0 0,0 0)',
'method=structure keepcollapsed=true'
);
POINT(0 0)
SELECT ST_MakeValid(
'LINESTRING(0 0,0 0)',
'method=structure keepcollapsed=false'
);
LINESTRING EMPTY
See Also
,
,