Name

ST_Collect — Creates a GeometryCollection or Multi* geometry from a set of geometries.

Synopsis

geometry ST_Collect(geometry g1, geometry g2);

geometry ST_Collect(geometry[] g1_array);

geometry ST_Collect(geometry set g1field);

Description

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.

[Warning]

ST_Collect does not dissolve boundaries or resolve overlaps. Collecting overlapping polygons can produce an invalid MultiPolygon. If the result must be a valid polygonal geometry with overlaps merged, use ST_Union instead. ST_Union also splits linestrings at intersections and may return a single geometry after dissolving boundaries.

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.

Examples

Two-input variant.

Collect 2D points.

Code
SELECT ST_Collect(
    'POINT(1 2)',
    'POINT(-2 3)' );
Output
MULTIPOINT((1 2),(-2 3))
Figure
Geometry figure for visual-st-collect-01

Collect 3D points.

Code
SELECT ST_Collect(
        'POINT(1 2 3)',
        'POINT(1 2 4)' );
Output
MULTIPOINT(1 2 3,1 2 4)
Figure
Geometry figure for visual-st-collect-02

Collect curves.

Code
SELECT ST_Collect(
        'CIRCULARSTRING(220268 150415,220227 150505,220227 150406)',
        'CIRCULARSTRING(220227 150406,2220227 150407,220227 150406)');
Output
MULTICURVE(CIRCULARSTRING(220268 150415,220227 150505,220227 150406),
CIRCULARSTRING(220227 150406,2220227 150407,220227 150406))
Figure
Geometry figure for visual-st-collect-03

Array variant.

Using an array constructor for a subquery.

Code
SELECT ST_Collect(ARRAY(SELECT geom FROM sometable));

Using an array constructor for values.

Code
SELECT ST_Collect(
        ARRAY[ 'LINESTRING(1 2,3 4)'::geometry,
            'LINESTRING(3 4,4 5)'::geometry ] ) As wktcollect;
Output
MULTILINESTRING((1 2,3 4),(3 4,4 5))
Figure
Geometry figure for visual-st-collect-04

Aggregate variant.

Creating multiple collections by grouping geometries in a table.

Code
SELECT stusps, ST_Collect(geom) AS geom
FROM (
  SELECT stusps, (ST_Dump(geom)).geom AS geom
  FROM somestatetable
) AS f
GROUP BY stusps;

See Also

ST_Dump, ST_Union