The ST_SymDifference function takes two
geometries as input parameters and returns the geometry that is the
symmetrical difference of the two geometries. The symmetrical difference
is the non-intersecting part of the two specified geometries.

The resulting geometry is represented
in the spatial reference system of the first geometry. The dimension
of the returned geometry is the same as that of the input geometries.
Both geometries must be of the same dimension.

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.

If the geometries
are equal, an empty geometry of type ST_Point is returned. If either
geometry is null, then null is returned.

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 type ST_Geometry or one of its subtypes that represents
the first geometry to compute the symmetrical difference with geometry2.

geometry2

A value of type ST_Geometry or one of its subtypes that represents
the second geometry to compute the symmetrical difference with geometry1.

Return type

ST_Geometry

Examples

Example 1

This example illustrates the use of the ST_SymDifference function.
The geometries are stored in the SAMPLE_GEOMS table.

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(75 75,90 90)',0));

In the following examples, the results have been reformatted
for readability. Your results will vary according to your display.

Example 2

This example uses ST_SymDifference to return the symmetric
difference of two disjoint polygons in the SAMPLE_GEOMS table.

SELECT a.id as id1, b.id as id2,
CAST (ST_AsText (ST_SymDifference (a.geometry, b.geometry) )
AS VARCHAR(350) ) SYM_DIFF
FROM sample_geoms a, sample_geoms b
WHERE a.id = 1 AND b.id = 2

This example uses ST_SymDifference to return the symmetric
difference of two intersecting polygons in the SAMPLE_GEOMS table.

SELECT a.id as id1, b.id as id2,
CAST (ST_AsText (ST_SymDifference (a.geometry, b.geometry) )
AS VARCHAR(500) ) SYM_DIFF
FROM sample_geoms a, sample_geoms b
WHERE a.id = 2 AND b.id = 3

This example uses ST_SymDifference to return the symmetric
difference of two intersecting linestrings in the SAMPLE_GEOMS table.

SELECT a.id as id1, b.id as id2,
CAST (ST_AsText (ST_SymDifference (a.geometry, b.geometry) )
AS VARCHAR(350) ) SYM_DIFF
FROM sample_geoms a, sample_geoms b
WHERE a.id = 4 AND b.id = 5