ST_Collect — Return a specified ST_Geometry value from a collection of other geometries.
geometry ST_Collect(
geometry set g1field)
;
geometry ST_Collect(
geometry g1, geometry g2)
;
geometry ST_Collect(
geometry[] g1_array)
;
Output type can be a MULTI* or a GEOMETRYCOLLECTION. Comes in 2 variants. Variant 1 collects 2 geometries. Variant 2 is an aggregate function that takes a set of geometries and collects them into a single ST_Geometry.
Aggregate version: This function returns a GEOMETRYCOLLECTION or a MULTI object from a set of geometries. The ST_Collect() function is an "aggregate" function in the terminology of PostgreSQL. That means that it operates on rows of data, in the same way the SUM() and AVG() functions do. For example, "SELECT ST_Collect(GEOM) FROM GEOMTABLE GROUP BY ATTRCOLUMN" will return a separate GEOMETRYCOLLECTION for each distinct value of ATTRCOLUMN.
Non-Aggregate version: This function returns a geometry being a collection of two input geometries. Output type can be a MULTI* or a GEOMETRYCOLLECTION.
ST_Collect and ST_Union are often interchangeable except that ST_Collect will always return a GeometryCollection or MULTI geometry and ST_Union may return single geometries when it dissolves boundaries. ST_Union will also split linestrings at node intersections, whereas ST_Collect will never split linestrings and in turn just return as MULTILINESTRING. To prevent ST_Collect from returning a Geometry Collection when collecting MULTI geometries, one can use the below trick that utilizes ST_Dump to expand the MULTIs out to singles and then regroup them. |
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 This method supports Circular Strings and Curves, but will never return a MULTICURVE or MULTI as one would expect and PostGIS does not currently support those.
Aggregate example
SELECT stusps, ST_Collect(f.the_geom) as singlegeom FROM (SELECT stusps, (ST_Dump(the_geom)).geom As the_geom FROM somestatetable ) As f GROUP BY stusps
Non-Aggregate example
SELECT ST_AsText(ST_Collect(ST_GeomFromText('POINT(1 2)'), ST_GeomFromText('POINT(-2 3)') )); st_astext ---------- MULTIPOINT(1 2,-2 3) --Collect 2 d points SELECT ST_AsText(ST_Collect(ST_GeomFromText('POINT(1 2)'), ST_GeomFromText('POINT(1 2)') ) ); st_astext ---------- MULTIPOINT(1 2,1 2) --Collect 3d points SELECT ST_AsEWKT(ST_Collect(ST_GeomFromEWKT('POINT(1 2 3)'), ST_GeomFromEWKT('POINT(1 2 4)') ) ); st_asewkt ------------------------- MULTIPOINT(1 2 3,1 2 4) --Example with curves SELECT ST_AsText(ST_Collect(ST_GeomFromText('CIRCULARSTRING(220268 150415,220227 150505,220227 150406)'), ST_GeomFromText('CIRCULARSTRING(220227 150406,2220227 150407,220227 150406)'))); st_astext ------------------------------------------------------------------------------------ GEOMETRYCOLLECTION(CIRCULARSTRING(220268 150415,220227 150505,220227 150406), CIRCULARSTRING(220227 150406,2220227 150407,220227 150406)) --New ST_Collect array construct SELECT ST_Collect(ARRAY(SELECT the_geom FROM sometable)); SELECT ST_AsText(ST_Collect(ARRAY[ST_GeomFromText('LINESTRING(1 2, 3 4)'), ST_GeomFromText('LINESTRING(3 4, 4 5)')])) As wktcollect; --wkt collect -- MULTILINESTRING((1 2,3 4),(3 4,4 5))