ST_ToPolygon function

The ST_ToPolygon takes a geometry as an input parameter and converts it to a polygon. The resulting polygon is represented in the spatial reference system of the specified geometry.

The specified geometry must be empty or a polygon. If the specified geometry is null, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramST_ToPolygon(geometry)

Parameter

geometry
A value of type ST_Geometry or one of its subtypes that represents the geometry that is converted to a polygon.

A geometry can be converted to a polygon if it is empty or a polygon. If the conversion cannot be performed, then an exception condition is raised (SQLSTATE 38SUD).

Return type

ST_Polygon

Example

In the following example, the lines of results have been reformatted for readability. The spacing in your results will vary according to your online display

This example creates three geometries in SAMPLE_GEOMETRIES and converts each to a polygon.

CREATE TABLE sample_geometries (id INTEGER, geometry ST_Geometry)

INSERT INTO sample_geometries
  VALUES (1, ST_Geometry ('polygon ((0 0, 0 4, 5 4, 5 0, 0 0))', 1) ),
         (2, ST_Geometry ('point empty', 1) ),
         (3, ST_Geometry ('multipolygon empty', 1) )
In the following SELECT statement, the ST_ToPolygon function is used to return polygons converted to ST_Polygon from the static type of ST_Geometry.

SELECT CAST( ST_AsText( ST_ToPolygon(geometry) ) AS VARCHAR(130) ) POLYGONS
  FROM sample_geometries
Results:

POLYGONS
--------------------------------------------------------------------------
POLYGON ((0.000000 0.000000, 5.000000 0.000000, 5.000000 4.000000, 
          0.000000 4.000000, 0.000000 0.000000))
POLYGON EMPTY                                                                     
POLYGON EMPTY