ST_Union
ST_Union takes two geometries as input parameters and returns the geometry that is the union of the given geometries. The resulting geometry is represented in the spatial reference system of the first geometry.
Both geometries must be of the same dimension. If any of the two given geometries is null, null is returned.
If the second geometry is not represented in the same spatial reference system as the first geometry, the second geometry is converted to the other spatial reference system.
The resulting geometry is represented in the most appropriate spatial type. If it can be represented as a point, linestring, or polygon, then one of those types is used. Otherwise, the multipoint, multilinestring, or multipolygon type is used.
Syntax
Parameters
- geometry1
- A value of one of the seven distinct spatial data types that is combined with geometry2.
- geometry2
- A value of one of the seven distinct spatial data types that is combined with geometry1.
Return type
db2gse.ST_Geometry
Examples
Example 1
SET CURRENT FUNCTION PATH = CURRENT FUNCTION PATH, db2gse
CREATE TABLE sample_geoms (id INTEGER, geometry ST_Geometry)
INSERT INTO sample_geoms
VALUES (1, ST_Geometry(ST_Polygon ( 'polygon
((10 10, 10 20, 20 20, 20 10, 10 10) )', 0)))
INSERT INTO sample_geoms
VALUES (2, ST_Geometry(ST_Polygon ( 'polygon
((30 30, 30 50, 50 50, 50 30, 30 30) )', 0)))
INSERT INTO sample_geoms
VALUES (3, ST_Geometry(ST_Polygon ( 'polygon
((40 40, 40 60, 60 60, 60 40, 40 40) )', 0)))
INSERT INTO sample_geoms
VALUES (4, ST_Geometry(ST_LineString ('linestring (70 70, 80 80)', 0)))
INSERT INTO sample_geoms
VALUES (5, ST_Geometry(ST_LineString ('linestring (80 80, 100 70)', 0)))
In the following examples, the results have been reformatted
for readability. Your results will vary according to your display.Example 2
SELECT a.id, b.id, CAST ( ST_AsText( ST_Union( a.geometry, b.geometry) )
AS VARCHAR (350) ) UNION
FROM sample_geoms a, sample_geoms b
WHERE a.id = 1 AND b.id = 2
Results:
ID ID UNION
----- ----- -------------------------------------------------------
1 2 MULTIPOLYGON ((( 10.00000000 10.00000000, 20.00000000
10.00000000, 20.00000000 20.00000000, 10.00000000
20.00000000, 10.00000000 10.00000000))
(( 30.00000000 30.00000000, 50.00000000
30.00000000,50.00000000 50.00000000, 30.00000000
50.00000000,30.00000000 30.00000000)))
Example 3
SELECT a.id, b.id, CAST ( ST_AsText( ST_Union(a.geometry, b.geometry))
AS VARCHAR (250)) UNION
FROM sample_geoms a, sample_geoms b
WHERE a.id = 2 AND b.id = 3
Results:
ID ID UNION
----- ----- ----------------------------------------------------
2 3 POLYGON (( 30.00000000 30.00000000, 50.00000000
30.00000000,50.00000000 40.00000000, 60.00000000
40.00000000,60.00000000 60.00000000, 40.00000000
60.00000000 40.00000000 50.00000000, 30.00000000
50.00000000, 30.00000000 30.00000000))
Example 4
SELECT a.id, b.id, CAST ( ST_AsText( ST_Union( a.geometry, b.geometry) )
AS VARCHAR (250) ) UNION
FROM sample_geoms a, sample_geoms b
WHERE a.id = 4 AND b.id = 5
Results:
ID ID UNION
----- ----- ---------------------------------------------------------------
4 5 MULTILINESTRING (( 70.00000000 70.00000000, 80.00000000 80.00000000),
( 80.00000000 80.00000000, 100.00000000 70.00000000))
