ST_Polygonize

更新时间:
复制 MD 格式

An aggregate function that builds possible polygons from a set of linework geometries and returns them as a GeometryCollection.

Syntax

geometry ST_Polygonize(geometry set geomField);
geometry ST_Polygonize(geometry[] geomArray);

Parameters

ParameterDescription
geomFieldThe geometry column in the input dataset. Used when calling the function as an aggregate over a result set.
geomArrayAn array of geometry objects from which polygons are formed.

Usage notes

  • The constituent lines of each input geometry must be correctly noded before calling this function. Unnoded input produces incomplete or incorrect polygon output.

  • Most third-party tools do not support GeometryCollection results. Use ST_Dump to expand the collection into individual polygon rows.

Examples

The following example passes four LINESTRING geometries that form a closed square. ST_Polygonize assembles them into a single polygon and returns it inside a GeometryCollection.

SELECT ST_AsText(
    ST_Polygonize(
        ARRAY[
            'LINESTRING(0 0,0 1)'::geometry,
            'LINESTRING(0 1,1 1)'::geometry,
            'LINESTRING(1 1,1 0)'::geometry,
            'LINESTRING(0 0,1 0)'::geometry
        ]
    )
);

Result:

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