返回一个代表一些Geometry对象并集的Geometry对象。

语法

geometry  ST_Union(geometry set  g1Field);
geometry  ST_Union(geometry  g1 , geometry  g2);
geometry  ST_Union(geometry[]  g1Array);
geometry  ST_Union(geometry set  g1Field, float8 gridsize);
geometry  ST_Union(geometry set  g1Field, cstring options);
geometry  ST_Union(geometry set  g1Field, float8 gridsize, cstring options);
geometry  ST_Union(geometry[]  g1Array, float8 gridsize);
geometry  ST_Union(geometry[]  g1Array, cstring options);
geometry  ST_Union(geometry[]  g1Array, float8 gridsize, cstring options);

参数

参数名称 描述
g1field Geometry对象在数据集中的字段。
g1 第一个Geometry对象。
g2 第二个Geometry对象。
g1Array Geometry对象数组。
gridsize 指定网格大小,使得union时在相同网格内的顶点会直接合并,默认值为-1.0,表示不启用。
options 如果要开启并行执行,可通过options指定并行度。

并行度范围为1~64,格式为JSON字符串,例如'{"parallel": 4}'代表开启并行计算同时并行度为4,默认为空'{}',即串行执行。

描述

  • 输出类型可以是Multi类型或GeometryCollection类型。该函数有两种形式:
    • 形式1:输入参数是两个Geometry对象。输出类型可以是Multi类型,非Multi类型或GeometryCollection。如果任意一个输入对象是NULL,返回值也是NULL。
    • 形式2:是一个聚合功能函数,输入对象是一个Geometry对象的集合,输出类型可能是Multi类型或非Multi类型。
  • ST_Collect和ST_Union经常可以互换使用。ST_Collect一般来说要比ST_Union快很多,因为ST_Collect不会去分解输入Geometry对象的边界或者检查一个MultiPolygon对象是否有重叠部分。

示例

  • 对比ST_Union和ST_Collect:
    SELECT ST_Union(g1,g2),ST_Collect(g1,g2)
                 from (select 'POLYGON((0 0,1 0,1 2,0 2,0 0))'::geometry as g1,'POLYGON((1 0,3 0,3 1,1 1,1 0))'::geometry as g2) as t;
    12
  • 指定gridsize和options:
    --指定gridsize
    select st_area(st_union(geom, 0.005)) from tableA;
    
    --指定并行度
    select st_area(st_union(geom, '{"parallel": 4}'::cstring)) from tableA;
    
    --同时指定gridsize和并行度
    select st_area(st_union(geom, 0.005, '{"parallel": 4}'::cstring)) from tableA;