ST_Collect — Creates a GeometryCollection or Multi* geometry from a set of geometries.
geometry ST_Collect(geometry g1, geometry g2);
geometry ST_Collect(geometry[] g1_array);
geometry ST_Collect(geometry set g1field);
Collects geometries into a geometry collection. The result is either a Multi* or a GeometryCollection, depending on whether the input geometries have the same or different types (homogeneous or heterogeneous). The input geometries are left unchanged within the collection.
Variant 1: accepts two input geometries
Variant 2: accepts an array of geometries
Variant 3: aggregate function accepting a rowset of geometries.
|
|
|
|
If any input geometry is a collection (Multi* or GeometryCollection),
ST_Collect returns a GeometryCollection, since that is
the only type which can contain nested collections. To prevent this, use
ST_Dump in a subquery to expand input collections to
their atomic elements (see example below).
Availability: 1.4.0 - ST_Collect(geomarray) was introduced. ST_Collect was enhanced to handle more geometries faster.
This function supports 3d and will not drop the z-index.
This method supports Circular Strings and Curves.
Two-input variant.
Collect 2D points.
SELECT ST_Collect(
'POINT(1 2)',
'POINT(-2 3)' );
MULTIPOINT((1 2),(-2 3))
Collect 3D points.
SELECT ST_Collect(
'POINT(1 2 3)',
'POINT(1 2 4)' );
MULTIPOINT(1 2 3,1 2 4)
Collect curves.
SELECT ST_Collect(
'CIRCULARSTRING(220268 150415,220227 150505,220227 150406)',
'CIRCULARSTRING(220227 150406,2220227 150407,220227 150406)');
MULTICURVE(CIRCULARSTRING(220268 150415,220227 150505,220227 150406), CIRCULARSTRING(220227 150406,2220227 150407,220227 150406))
Array variant.
Using an array constructor for a subquery.
SELECT ST_Collect(ARRAY(SELECT geom FROM sometable));
Using an array constructor for values.
SELECT ST_Collect(
ARRAY[ 'LINESTRING(1 2,3 4)'::geometry,
'LINESTRING(3 4,4 5)'::geometry ] ) As wktcollect;
MULTILINESTRING((1 2,3 4),(3 4,4 5))
Aggregate variant.
Creating multiple collections by grouping geometries in a table.
SELECT stusps, ST_Collect(geom) AS geom FROM ( SELECT stusps, (ST_Dump(geom)).geom AS geom FROM somestatetable ) AS f GROUP BY stusps;