ST_Polygonize

更新时间:
复制 MD 格式

ST_Polygonize is an aggregate function that builds polygons from a set of input lines. It returns a GeometryCollection containing all polygons formed by the constituent lines of the input geometries.

Syntax

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

Parameters

ParameterDescription
geomFieldThe geometry fields in the dataset.
geomArrayAn array of geometry objects.

Usage notes

  • Input lines must be correctly noded. All intersection points must be inserted as vertices before polygonizing.

  • `GeometryCollection` output. Most third-party tools do not support GeometryCollection objects. Use ST_Dump to expand the result into individual polygon rows.

Examples

Build polygons from an array of lines

The following example builds a polygon from four line segments that form a closed square.

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
    ]
  )
);

Output:

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

What's next

  • ST_Dump: Expand a GeometryCollection into individual geometry rows.