ST_Union function

The ST_Union function 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 and uses the same underlying datum, it will be 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.

This function can also be called as a method.

Syntax

Read syntax diagramSkip visual syntax diagramdb2gse.ST_Union(geometry1,geometry2 )

Parameters

geometry1
A value of type ST_Geometry or one of its subtypes that is combined with geometry2.
geometry2
A value of type ST_Geometry or one of its subtypes that is combined with geometry1.

Return type

db2gse.ST_Geometry

Examples

Example 1
The following SQL statements create and populate the SAMPLE_GEOMS table.

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( 'polygon 
         ((10 10, 10 20, 20 20, 20 10, 10 10) )', 0))

INSERT INTO sample_geoms
  VALUES (2, ST_Geometry( 'polygon 
         ((30 30, 30 50, 50 50, 50 30, 30 30) )', 0))

INSERT INTO sample_geoms
  VALUES (3, ST_Geometry( 'polygon 
         ((40 40, 40 60, 60 60, 60 40, 40 40) )', 0))

INSERT INTO sample_geoms
  VALUES (4, ST_Geometry('linestring (70 70, 80 80)', 0))

INSERT INTO sample_geoms
  VALUES (5, ST_Geometry('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
This example finds the union of two disjoint polygons.

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
This example finds the union of two intersecting polygons.

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
Find the union of two linestrings.

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