Name

ST_Union — Returns a Geometry object that represents the point set union of two Geometries.

Synopsis

PostgreSQL

geometry ST_Union(bytea GeomA, bytea GeomB);

geometry ST_Union(bytea[] Geometry_Array);

geometry ST_Union(bytea GeomAggregate);

H2

geometry ST_Union(bytea GeomA, bytea GeomB);

geometry ST_Union(bytea[] Geometry_Array);

geometry ST_UnionAgg(bytea GeomAggregate);

Alias

Union

Description

Returns a Geometry object that represents the point set union of two Geometries.

The geometries must have the same coordinate dimension

[Note]

This method does not support GeometryCollections

Coordinate Dimensions
2D3DM
Spatial Standards Support
OGC SFS for SQL. 1.1 (1999)OGC SFS for SQL. 1.1.0 (2005)OGC SFS for SQL. 1.2.0 (2006)SQL-MM Part 3
2.1.1.37.2.21.17.2.8.15.1.19

Examples

--2d polygons
SELECT ST_AsEWKT(ST_Union(A,B)) from 
(SELECT ST_GeomFromText('POLYGON ((40 20, 40 100, 120 100, 120 20, 40 20))',25830) as A,
ST_GeomFromText('POLYGON ((80 60, 80 130, 170 130, 170 60, 80 60))',25830) as B) as foo;

--Result
SRID=25830;POLYGON ((40 20, 40 100, 80 100, 80 130, 170 130, 170 60, 120 60, 120 20, 40 20))

-- 3d line & multipoint
SELECT ST_AsEWKT(ST_Union(B,A)) from 
(SELECT ST_GeomFromText('LINESTRING (-4 20 2, 27 51 1, 55 34 0, 87 69 0)',25830) as A,
ST_GeomFromText('MULTIPOINT ((30 24 10), (55 60 20))',25830) as B) as foo;

--Result
SRID=25830;GEOMETRYCOLLECTION (POINT (30 24 10), POINT (55 60 20), LINESTRING (-4 20 2, 27 51 1, 55 34 0, 87 69 0))

Array PostgreSQL

SELECT ST_AsText(ST_Union(Array[A,B,C])) 
FROM (SELECT ST_GeomFromText('POLYGON ((40 20, 40 100, 120 100, 120 20, 40 20))') as A,
             ST_GeomFromText('POLYGON ((80 60, 80 130, 170 130, 170 60, 80 60))') as B,
             ST_GeomFromText('POINT (80 9)') as C
) as foo;

--Result:
st_astext
-----------------------------------------------------------------------------------
GEOMETRYCOLLECTION (
  POINT (80 9), 
  POLYGON ((40 20, 40 100, 80 100, 80 130, 170 130, 170 60, 120 60, 120 20, 40 20)))
(1 row)

Array H2

SELECT ST_AsText(ST_Union(st_accumagg(geom))) as newpolygon 
from (select array_get((
 st_geomfromtext('POLYGON ((40 20, 40 100, 120 100, 120 20, 40 20))'),
 st_geomfromtext('POLYGON ((80 60, 80 130, 170 130, 170 60, 80 60))')),x)::binary as geom 
from (select x from system_range(1,2) as foo) as table);
--Result
NEWPOLYGON  
POLYGON ((40 20, 40 100, 80 100, 80 130, 170 130, 170 60, 120 60, 120 20, 40 20))

SELECT ST_AsText(ST_Union(st_accumagg(geom))) as mpoints
 from (select st_geomfromtext('SRID=10;POINT(0 0)') as geom
    union select st_geomfromtext('SRID=10;POINT(10 10)') 
    union select st_geomfromtext('SRID=10;POINT(15 15)') 
) as geom;
--Result
MPOINTS  
MULTIPOINT ((0 0), (10 10), (15 15))

Aggregate

CREATE TABLE "lines"(id serial PRIMARY KEY);
SELECT AddGeometryColumn ('lines','GEOM',25830,'LINESTRING',2);

INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (1 1, 1 4, 1 7)'));
INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (1 7, 3 7, 6 7)'));
INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (6 7, 6 3, 6 1)'));
INSERT INTO "lines" ("GEOM") VALUES (ST_GeomfromEWKT('SRID=25830;LINESTRING (1 1, 4 1, 6 1)'));

--PostgreSQL
SELECT asewkt(ST_Union("lines"."GEOM")) as lineunion from "lines";
--H2
SELECT asewkt(ST_UnionAgg("lines"."GEOM")) as lineunion from "lines";

SELECT DropGeometryTable ('lines');

--Result
SRID=25830;MULTILINESTRING ((1 1, 4 1, 6 1), (1 1, 1 4, 1 7), (6 7, 6 3, 6 1), (1 7, 3 7, 6 7))

Related functions

ST_MemUnion