ST_MakePolygon

更新时间:
复制 MD 格式

Constructs a polygon from one or more closed LineString geometries.

Syntax

geometry ST_MakePolygon(geometry linestring);
geometry ST_MakePolygon(geometry outerlinestring, geometry[] interiorlinestrings);

Parameters

ParameterDescription
linestringA closed LineString that defines the outer shell of the polygon.
outerlinestringA closed LineString that defines the outer shell.
interiorlinestringsAn array of closed LineStrings, each defining an inner shell (hole). Build the array using ARRAY[].

Usage notes

Variant 1 accepts a single shell LineString and returns a simple polygon with no holes.

Variant 2 accepts an outer shell and an array of inner shells, and returns a polygon with one or more holes.

Both variants require a closed LineString — the first and last coordinates must be identical. Open LineStrings cause an error.

Examples

Variant 1: Single shell

Construct a simple polygon from a closed LineString.

SELECT ST_AsText(ST_MakePolygon(ST_GeomFromText('LINESTRING(1 2,3 4,5 6,1 2)')));

Output:

         st_astext
----------------------------
 POLYGON((1 2,3 4,5 6,1 2))
(1 row)

Variant 2: Outer shell with inner holes

Construct a polygon with one hole by passing an outer shell and an array containing one inner shell.

SELECT ST_AsText(
  ST_MakePolygon(
    ST_GeomFromText('LINESTRING(0 0,0 1,1 1,0 0)'),
    ARRAY[ST_GeomFromText('LINESTRING(-1 -1,-1 2,2 2,-1 -1)')]
  )
);

Output:

                     st_astext
---------------------------------------------------
 POLYGON((0 0,0 1,1 1,0 0),(-1 -1,-1 2,2 2,-1 -1))
(1 row)