Name

ST_Polygonize — Creates a GeometryCollection made up of Polygons from a set of Geometries which contain lines that represents the Polygons edges.

Synopsis

PostgreSQL

geometry ST_Polygonize(bytea GeometryAggregate);

geometry ST_Polygonize(bytea[] Geometry_Array);

H2

geometry ST_PolygonizeAgg(bytea GeometryAggregate);

geometry ST_Polygonize(bytea[] Geometry_Array);

Alias

Polygonize

Description

Creates a GeometryCollection made up of Polygons from a set of Geometries which contain lines that represents the Polygons edges. Lines must connect to one another at the endpoint.

Coordinate Dimensions
2D3DM
Spatial Standards Support
OGC SFS for SQL. 1.1 (1999)OGC SFS for SQL. 1.1.0 (2005)OGC SFS for SQL. 1.2.0 (2006)SQL-MM Part 3
----

Examples

Array PostgreSQL

SELECT ST_AsEWKT(ST_Polygonize(
 Array[ST_GeomFromEWKT('LINESTRING (4 11, 4 4, 11 4, 11 11, 4 11)'),
       ST_GeomFromEWKT('LINESTRING (1 9, 1 5, 5 5, 5 9, 1 9)')]));

--Result
st_asewkt
--------------------------------------------------------------------------------
GEOMETRYCOLLECTION (POLYGON ((4 11, 11 11, 11 4, 4 4, 4 11)), POLYGON ((1 9, 5 9, 5 5, 1 5, 1 9)))
(1 row)

Array H2

SELECT ST_AsText(ST_Polygonize(st_accumagg(geom))) as myline from (select array_get(
(
st_geomfromtext('LINESTRING (4 11, 4 4, 11 4, 11 11, 4 11)'),
st_geomfromtext('LINESTRING (1 9, 1 5, 5 5, 5 9, 1 9)')
),x)::binary as geom from (select x from system_range(1,2) as foo) as tabla);
--Result
GEOMETRYCOLLECTION (POLYGON ((4 11, 11 11, 11 4, 4 4, 4 11)), POLYGON ((1 9, 5 9, 5 5, 1 5, 1 9)))

Aggregate

create table "lines"(id serial PRIMARY KEY);
SELECT AddGeometryColumn ('lines','GEOM',25830,'LINESTRING',2);

INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (1 1, 1 4)'));
INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (1 4, 2 4)'));
INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (2 4, 2 1)'));
INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (2 1, 1 1)'));
INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (2 4, 3 4)'));
INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (3 4, 3 1)'));
INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (2 1, 3 1)'));

--PostgreSQL
select asewkt(ST_Polygonize("lines"."GEOM")) as polygonizer from "lines";
--H2
select asewkt(ST_PolygonizeAgg("lines"."GEOM")) as polygonizer from "lines";

SELECT DropGeometryTable ('lines');

--Result
POLYGONIZER  
SRID=25830;GEOMETRYCOLLECTION (POLYGON ((2 4, 2 1, 1 1, 1 4, 2 4)), POLYGON ((3 1, 2 1, 2 4, 3 4, 3 1)))
GeomA
ST_Polygonize(GeomA)
GeomA
ST_Polygonize(GeomA)

Related functions

ST_BuildArea