ST_Overlaps
The ST_Overlaps function takes two geometries as input parameters and returns 1 if the intersection of the geometries results in a geometry of the same dimension but is not equal to either of the given geometries. Otherwise, 0 (zero) is returned.
If any of the two geometries is null or is empty, then null is returned.
If the second geometry is not represented in the same spatial reference system as the first geometry, it will be converted to the other spatial reference system.
Syntax
Parameters
- geometry1
- A value of one of the seven distinct spatial data types that represents the geometry that is tested to overlap with geometry2.
- geometry2
- A value of one of the seven distinct spatial data types that represents the geometry that is tested to overlap with geometry1.
Return type
INTEGER
Examples
Example 1
This example
illustrates the use of ST_Overlaps. Various geometries are created
and inserted into the SAMPLE_GEOMETRIES table
SET CURRENT PATH = CURRENT PATH, db2gse;
CREATE TABLE sample_geometries (id INTEGER, geometry ST_Geometry);
INSERT INTO sample_geometries
VALUES
(1, ST_GEOMETRY(ST_Point (10, 20, 1)));
INSERT INTO sample_geometries
VALUES
(2, ST_GEOMETRY(ST_Point ('point (41 41)', 1) ));
INSERT INTO sample_geometries
VALUES
(10, ST_GEOMETRY(ST_LineString ('linestring (1 10, 3 12, 10 10)', 1) ));
INSERT INTO sample_geometries
VALUES
(20, ST_GEOMETRY(ST_LineString ('linestring (50 10, 50 12, 45 10)', 1) ));
INSERT INTO sample_geometries
VALUES
(30, ST_GEOMETRY(ST_LineString ('linestring (50 12, 50 10, 60 8)', 1) ));
INSERT INTO sample_geometries
VALUES
(100, ST_GEOMETRY(ST_Polygon ('polygon ((0 0, 0 40, 40 40, 40 0,
0 0))', 1) ));
INSERT INTO sample_geometries
VALUES
(110, ST_GEOMETRY(ST_Polygon ('polygon ((30 10, 30 30, 50 30, 50 10,
30 10))', 1) ));
INSERT INTO sample_geometries
VALUES
(120, ST_GEOMETRY(ST_Polygon ('polygon ((0 50, 0 60, 40 60, 40 60,
0 50))', 1) ));
Example 2
This example finds the IDs
of points that overlap.
SELECT sg1.id, sg2.id
CASE ST_Overlaps (sg1.geometry, sg2.geometry)
WHEN 0 THEN 'Points_do_not_overlap'
WHEN 1 THEN 'Points_overlap'
END
AS OVERLAP
FROM sample_geometries sg1, sample_geometries sg2
WHERE sg1.id < 10 AND sg2.id < 10 AND sg1.id >= sg2.id;
Results:
ID ID OVERLAP
----------- ---------- ------------------------
1 1 Points_do_not_overlap
2 1 Points_do_not_overlap
2 2 Points_do_not_overlap
Example 3
This example finds the IDs
of lines that overlap.
SELECT sg1.id, sg2.id
CASE ST_Overlaps (sg1.geometry, sg2.geometry)
WHEN 0 THEN 'Lines_do_not_overlap'
WHEN 1 THEN 'Lines_overlap'
END
AS OVERLAP
FROM sample_geometries sg1, sample_geometries sg2
WHERE sg1.id >= 10 AND sg1.id < 100
AND sg2.id >= 10 AND sg2.id < 100
AND sg1.id >= sg2.id;
Results:
ID ID OVERLAP
----------- ---------- ------------------------
10 10 Lines_do_not_overlap
20 10 Lines_do_not_overlap
30 10 Lines_do_not_overlap
20 20 Lines_do_not_overlap
30 20 Lines_overlap
30 30 Lines_do_not_overlap
Example 4
This example finds the IDs
of polygons that overlap.
SELECT sg1.id, sg2.id
CASE ST_Overlaps (sg1.geometry, sg2.geometry)
WHEN 0 THEN 'Polygons_do_not_overlap'
WHEN 1 THEN 'Polygons_overlap'
END
AS OVERLAP
FROM sample_geometries sg1, sample_geometries sg2
WHERE sg1.id >= 100 AND sg2.id >= 100 AND sg1.id >= sg2.id;
Results:
ID ID OVERLAP
----------- ---------- ------------------------
100 100 Polygons_do_not_overlap
110 100 Polygons_overlap
120 100 Polygons_do_not_overlap
110 110 Polygons_do_not_overlap
120 110 Polygons_do_not_overlap
120 120 Polygons_do_not_overlap