Constructs a polygon from one or more closed LineString geometries.
Syntax
geometry ST_MakePolygon(geometry linestring);
geometry ST_MakePolygon(geometry outerlinestring, geometry[] interiorlinestrings);Parameters
| Parameter | Description |
|---|---|
linestring | A closed LineString that defines the outer shell of the polygon. |
outerlinestring | A closed LineString that defines the outer shell. |
interiorlinestrings | An 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)该文章对您有帮助吗?