名称

ST_Collect — 从一组几何图形创建 GeometryCollection 或 Multi* 几何图形。

大纲

geometry ST_Collect(geometry g1, geometry g2);

geometry ST_Collect(geometry[] g1_array);

geometry ST_Collect(geometry set g1field);

描述

将几何图形收集到几何图形集合中。 结果是 Multi* 或 GeometryCollection,具体取决于输入几何图形是否具有相同或不同类型(同质或异构)。输入几何图形在集合中保持不变。

第一种形式:接受两个输入几何图形

第二种形式: 接受几何数组

第三种形式: 接受几何行集的聚合函数。

[警告]

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).

可用性:引入了 1.4.0 - ST_Collect(几何)。 ST_Collect已得到增强,可以更快地处理更多几何图形。

该函数支持 3d 并且不会丢失 z-index。

此方法支持圆形字符串和曲线。

示例

Two-input variant.

合并2D点。

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

合并3D点。

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

合并曲线。

Code
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))
Figure
Geometry figure for visual-st-collect-03

Array variant.

使用子查询的数组构造函数。

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

使用数组构造函数来获取值。

Code
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))
Figure
Geometry figure for visual-st-collect-04

Aggregate variant.

通过在表中对几何进行分组来生成多个集合。

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

相关信息

ST_Dump, ST_Union