These functions test whether geometries are valid according to the OGC SFS standard. They also provide information about the nature and location of invalidity.

# Geometry Validation

ST_IsValid

Tests if a geometry is well-formed in 2D.

    			boolean ST_IsValid

    			geometry  g

    			boolean ST_IsValid

    			geometry  g
    			integer  flags

### Description

Test if an ST_Geometry value is well-formed in 2D according to the OGC rules. For geometries that are invalid, the PostgreSQL NOTICE will provide details of why it is not valid. For geometries with 3 and 4 dimensions, the validity still only tested in 2 dimensions.

For the version with flags, supported `flags` are documented in ST_IsValidDetail This version does not print a NOTICE explaining invalidity.

For more information on the definition of geometry validity, refer to "Ensuring OpenGIS compliancy of geometries"

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(ST_GeomFromText('LINESTRING(0 0, 1 1)')) As good_line,
    	ST_IsValid(ST_GeomFromText('POLYGON((0 0, 1 1, 1 2, 1 1, 0 0))')) As bad_poly
    --results
    NOTICE:  Self-intersection at or near point 0 0
     good_line | bad_poly
    -----------+----------
     t         | f

### See Also

ST_IsSimple, ST_IsValidReason, ST_IsValidDetail, ST_Summary

ST_IsValidDetail

Returns a `valid_detail` row stating if a geometry is valid, and if not a reason why and a location.

    			valid_detail ST_IsValidDetail
    			geometry  geom
    			integer  flags

### Description

Returns a valid_detail row, formed by 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 substitute and improve the combination of ST_IsValid and ST_IsValidReason to generate a detailed report of invalid geometries.

The 'flags' argument is a bitfield. It can have the following values:

- 1: Consider self-intersecting rings forming holes as valid. This is also know as "the ESRI flag". Note that this is against the OGC model.

Performed by the GEOS module.

Availability: 2.0.0

### Examples

    --First 3 Rejects from a successful quintuplet experiment
    SELECT gid, reason(ST_IsValidDetail(the_geom)), ST_AsText(location(ST_IsValidDetail(the_geom))) as location
    FROM
    (SELECT ST_MakePolygon(ST_ExteriorRing(e.buff), array_agg(f.line)) As the_geom, gid
    FROM (SELECT ST_Buffer(ST_MakePoint(x1*10,y1), z1) As buff, x1*10 + y1*100 + z1*1000 As gid
    	FROM generate_series(-4,6) x1
    	CROSS JOIN generate_series(2,5) y1
    	CROSS JOIN generate_series(1,8) z1
    	WHERE x1 > y1*0.5 AND z1 < x1*y1) As e
    	INNER JOIN (SELECT ST_Translate(ST_ExteriorRing(ST_Buffer(ST_MakePoint(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(the_geom) = false
    ORDER BY gid
    LIMIT 3;

     gid  |      reason       |  location
    ------+-------------------+-------------
     5330 | Self-intersection | POINT(32 5)
     5340 | Self-intersection | POINT(42 5)
     5350 | Self-intersection | POINT(52 5)

     --simple example
    SELECT * FROM ST_IsValidDetail('LINESTRING(220227 150406,2220227 150407,222020 150410)');

     valid | reason | location
    -------+--------+----------
     t     |        |

### See Also

ST_IsValid, ST_IsValidReason

ST_IsValidReason

Returns text stating if a geometry is valid, or a reason for invalidity.

    			text ST_IsValidReason
    			geometry  geomA
    			integer  flags

    			text ST_IsValidReason
    			geometry  geomA

### Description

Returns text stating if a geometry is valid or not an if not valid, a reason why.

Useful in combination with ST_IsValid to generate a detailed report of invalid geometries and reasons.

Allowed `flags` are documented in ST_IsValidDetail.

Performed by the GEOS module.

Availability: 1.4

Availability: 2.0 version taking flags.

### Examples

    --First 3 Rejects from a successful quintuplet experiment
    SELECT gid, ST_IsValidReason(the_geom) as validity_info
    FROM
    (SELECT ST_MakePolygon(ST_ExteriorRing(e.buff), array_agg(f.line)) As the_geom, gid
    FROM (SELECT ST_Buffer(ST_MakePoint(x1*10,y1), z1) As buff, x1*10 + y1*100 + z1*1000 As gid
    	FROM generate_series(-4,6) x1
    	CROSS JOIN generate_series(2,5) y1
    	CROSS JOIN generate_series(1,8) z1
    	WHERE x1 > y1*0.5 AND z1 < x1*y1) As e
    	INNER JOIN (SELECT ST_Translate(ST_ExteriorRing(ST_Buffer(ST_MakePoint(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(the_geom) = false
    ORDER BY gid
    LIMIT 3;

     gid  |      validity_info
    ------+--------------------------
     5330 | Self-intersection [32 5]
     5340 | Self-intersection [42 5]
     5350 | Self-intersection [52 5]

     --simple example
    SELECT ST_IsValidReason('LINESTRING(220227 150406,2220227 150407,222020 150410)');

     st_isvalidreason
    ------------------
     Valid Geometry

### See Also

ST_IsValid, ST_Summary