The ST_Union() function

The ST_Union() function returns an ST_Geometry object that is the union of two source objects, the Boolean logical OR of space.

Syntax

ST_Union(g1 ST_Geometry, g2 ST_Geometry)

Usage

The source geometries must have the same dimension. ST_Union() returns the result as an ST_GeomCollection (ST_MultiPoint, ST_MultiLineString, or ST_MultiPolygon).
Figure 1: The union set of two geometries

This graphic shows the union of various geometric objects.

Return type

ST_Geometry

Example

The sensitive_areas table contains several columns that describe the threatened institutions in addition to the zone column, which stores the institutions' ST_Polygon geometries:
CREATE TABLE sensitive_areas (id     integer,
                              name   varchar(128),
                              size   float,
                              type   varchar(10),
                              zone   ST_Polygon);
The hazardous_sites table stores the identity of the sites in the site_id and name columns. The actual geographic location of each site is stored in the location point column:
CREATE TABLE hazardous_sites (site_id   integer,
                              name      varchar(40),
                              location  ST_Point);
The ST_Buffer() function generates a 5-mile buffer that surrounds the hazardous waste site locations. The ST_Union() function generates polygons from the union of the buffered hazardous waste site polygons and the sensitive areas. The ST_Area() function returns the unioned polygon area:
SELECT sa.name sensitive_area, hs.name hazardous_site,
       ST_Area(ST_Union(ST_Buffer(hs.location,(5 * 5280)),sa.zone)::
   ST_MultiPolygon) area
   FROM hazardous_sites hs, sensitive_areas sa;