## Name

ST_Union — 计算表示输入几何图形的点集并集的几何图形。

## Synopsis

`geometry ST_Union(`geometry g1, geometry g2`)`;

`geometry ST_Union(`geometry g1, geometry g2, float8 gridSize`)`;

`geometry ST_Union(`geometry[] g1_array`)`;

`geometry ST_Union(`geometry set g1field`)`;

`geometry ST_Union(`geometry set g1field, float8 gridSize`)`;

## 描述

ST_Union的数组和聚合形式使用 http://blog.cleverelephant.ca/2009/01/must-faster-unions-in-postgis-14.html 中引入的快速级联连接算法

 ST_Collect 如果不要求结果不重叠，有时可以使用 ST_Collect 代替 ST_Union。 ST_Collect 通常比 ST_Union 更快，因为它不对收集的几何图形执行任何处理。

ST_Union 创建 MultiLineString 并且不会将 LineString 缝合成单个 LineString。 使用ST_LineMerge缝合线串。

 OGC SPEC 中未明确定义聚合版本。

## 示例

```SELECT id,
ST_Union(geom) as singlegeom
FROM sometable f
GROUP BY id;
```

```select ST_AsText(ST_Union('POINT(1 2)' :: geometry, 'POINT(-2 3)' :: geometry))

st_astext
----------
MULTIPOINT(-2 3,1 2)

select ST_AsText(ST_Union('POINT(1 2)' :: geometry, 'POINT(1 2)' :: geometry))

st_astext
----------
POINT(1 2)```

3D 示例 - 3D 的类型（和混合维度！）

```select ST_AsEWKT(ST_Union(geom))
from (
select 'POLYGON((-7 4.2,-7.1 4.2,-7.1 4.3, -7 4.2))'::geometry geom
union all
select 'POINT(5 5 5)'::geometry geom
union all
select 'POINT(-2 3 1)'::geometry geom
union all
select 'LINESTRING(5 5 5, 10 10 10)'::geometry geom
) as foo;

st_asewkt
---------
GEOMETRYCOLLECTION(POINT(-2 3 1),LINESTRING(5 5 5,10 10 10),POLYGON((-7 4.2 5,-7.1 4.2 5,-7.1 4.3 5,-7 4.2 5)));
```

```select ST_AsEWKT(ST_Union(geom))
from (
select 'POLYGON((-7 4.2 2,-7.1 4.2 3,-7.1 4.3 2, -7 4.2 2))'::geometry geom
union all
select 'POINT(5 5 5)'::geometry geom
union all
select 'POINT(-2 3 1)'::geometry geom
union all
select 'LINESTRING(5 5 5, 10 10 10)'::geometry geom
) as foo;

st_asewkt
---------
GEOMETRYCOLLECTION(POINT(-2 3 1),LINESTRING(5 5 5,10 10 10),POLYGON((-7 4.2 2,-7.1 4.2 3,-7.1 4.3 2,-7 4.2 2)))

--Examples using new Array construct
SELECT ST_Union(ARRAY(SELECT geom FROM sometable));

SELECT ST_AsText(ST_Union(ARRAY[ST_GeomFromText('LINESTRING(1 2, 3 4)'),
ST_GeomFromText('LINESTRING(3 4, 4 5)')])) As wktunion;

--wktunion---
MULTILINESTRING((3 4,4 5),(1 2,3 4))

```