The ST_SymDifference() function

The ST_SymDifference() function takes two ST_Geometry objects and returns an ST_Geometry object that is the logical XOR of space. In other words, it returns an object that is composed of the portions of the source objects that are not part of the intersection set.

Syntax

ST_SymDifference (g1 ST_Geometry, g2 ST_Geometry)

Usage

The source geometries must have the same dimension. If the geometries are equal, the ST_StartPoint() function returns an empty geometry; otherwise, the function returns the result as an ST_GeomCollection (ST_MultiPoint, ST_MultiLineString, or ST_MultiPolygon).
Figure 1: The symmetric difference of geometries

This graphic shows the symmetric difference of various geometric objects.

Return type

ST_Geometry

Example

For a special report, the county supervisor must determine the area of sensitive areas and 5-mile hazardous site radii that do not intersect.

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_StartPoint() function returns the polygons of the buffered hazardous waste site polygons and the sensitive areas that do not intersect:
SELECT sa.name sensitive_area, hs.name hazardous_site, 
       ST_Area(ST_SymDifference(ST_buffer(hs.location,(5 * 5280)),sa.zone)::
   ST_MultiPolygon) area
   FROM hazardous_sites hs, sensitive_areas sa;
The following figure shows that the symmetric difference of the hazardous waste sites and the sensitive areas results in the subtraction of the intersected areas.
Figure 2: Using the ST_SymDifference function

This graphic is described in the surrounding text.