ST_SymDifference
ST_SymDifference 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 given 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, the second geometry is 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 one of the seven distinct spatial data types that represents the first geometry to compute the symmetrical difference with geometry2.
- geometry2
- A value of one of the seven distinct spatial data types that represents the second geometry to compute the symmetrical difference 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(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
SELECT a.id, b.id,
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
Results:
ID ID SYM_DIFF
----- ----- -------------------------------------------------------------------
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_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
Results:
ID ID SYM_DIFF
--- --- -------------------------------------------------------------------
2 3 MULTIPOLYGON ((( 40.00000000 50.00000000, 50.00000000 50.00000000,
50.00000000 40.00000000, 60.00000000 40.00000000,
60.00000000 60.00000000, 40.00000000 60.00000000,
40.00000000 50.00000000)),
(( 30.00000000 30.00000000, 50.00000000 30.00000000,
50.00000000 40.00000000, 40.00000000 40.00000000,
40.00000000 50.00000000, 30.00000000 50.00000000,
30.00000000 30.00000000)))
Example 4
SELECT a.id, b.id,
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
Results:
ID ID SYM_DIFF
---- --- ---------------------------------------------------------------------
4 5 MULTILINESTRING (( 70.00000000 70.00000000, 75.00000000 75.00000000),
( 80.00000000 80.00000000, 90.00000000 90.00000000))
