ST_Intersects function
Use the ST_Intersects function to determine whether two geometries intersect.
Syntax
Parameter
- geometry1
- A value of type ST_Geometry or one of its subtypes that represents the geometry to test for intersection with geometry2.
- geometry2
- A value of type ST_Geometry or one of its subtypes that represents the geometry to test for intersection with geometry1.
Return type
INTEGER
Usage
ST_Intersects takes two geometries as input parameters and returns 1 if the specified geometries intersect. If the geometries do not intersect, 0 (zero) is returned.
If any of the two geometries is null or is empty, null is returned.
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.
ST_Intersects returns the exact opposite result of ST_Disjoint.
The ST_Intersects function returns 1 (one) if the conditions of any of the following pattern
matrices returns TRUE.
Geometry b Interior | Geometry b Boundary | Geometry b Exterior | |
---|---|---|---|
Geometry a Boundary | * | * | * |
Geometry a Interior | T | * | * |
Geometry a Exterior | * | * | * |
Geometry b Interior | Geometry b Boundary | Geometry b Exterior | |
---|---|---|---|
Geometry a Boundary | * | * | * |
Geometry a Interior | * | T | * |
Geometry a Exterior | * | * | * |
Geometry b Interior | Geometry b Boundary | Geometry b Exterior | |
---|---|---|---|
Geometry a Boundary | T | * | * |
Geometry a Interior | * | * | * |
Geometry a Exterior | * | * | * |
Geometry b Interior | Geometry b Boundary | Geometry b Exterior | |
---|---|---|---|
Geometry a Boundary | * | T | * |
Geometry a Interior | * | * | * |
Geometry a Exterior | * | * | * |
Usage
Example
The following statements create and populate the SAMPLE_GEOMETRIES1 and SAMPLE_GEOMETRIES2
tables.
CREATE TABLE sample_geometries1(id SMALLINT, spatial_type varchar(13),
geometry ST_GEOMETRY);
CREATE TABLE sample_geometries2(id SMALLINT, spatial_type varchar(13),
geometry ST_GEOMETRY);
INSERT INTO sample_geometries1(id, spatial_type, geometry)
VALUES
( 1, 'ST_Point', ST_Point('point(550 150)', 1) ),
(10, 'ST_LineString', ST_LineString('linestring(800 800, 900 800)', 1)),
(20, 'ST_Polygon', ST_Polygon('polygon((500 100, 500 200, 700 200,
700 100, 500 100))', 1) )
INSERT INTO sample_geometries2(id, spatial_type, geometry)
VALUES
(101, 'ST_Point', ST_Point('point(550 150)', 1) ),
(102, 'ST_Point', ST_Point('point(650 200)', 1) ),
(103, 'ST_Point', ST_Point('point(800 800)', 1) ),
(110, 'ST_LineString', ST_LineString('linestring(850 250, 850 850)', 1)),
(120, 'ST_Polygon', ST_Polygon('polygon((650 50, 650 150, 800 150,
800 50, 650 50))', 1)),
(121, 'ST_Polygon', ST_Polygon('polygon((20 20, 20 40, 40 40, 40 20,
20 20))', 1) )
The following SELECT statement determines whether the various geometries in the SAMPLE_GEOMTRIES1
and SAMPLE_GEOMTRIES2 tables intersect.
SELECT sg1.id AS sg1_id, sg1.spatial_type AS sg1_type,
sg2.id AS sg2_id, sg2.spatial_type AS sg2_type,
CASE ST_Intersects(sg1.geometry, sg2.geometry)
WHEN 0 THEN 'Geometries do not intersect'
WHEN 1 THEN 'Geometries intersect'
END AS intersects
FROM sample_geometries1 sg1, sample_geometries2 sg2
ORDER BY sg1_id, sg2_id
Results:
SG1_ID SG1_TYPE SG2_ID SG2_TYPE INTERSECTS
------ ------------- ------ ------------- ---------------------------
1 ST_Point 101 ST_Point Geometries intersect
1 ST_Point 102 ST_Point Geometries do not intersect
1 ST_Point 103 ST_Point Geometries do not intersect
1 ST_Point 110 ST_LineString Geometries do not intersect
1 ST_Point 120 ST_Polygon Geometries do not intersect
1 ST_Point 121 ST_Polygon Geometries do not intersect
10 ST_LineString 101 ST_Point Geometries do not intersect
10 ST_LineString 102 ST_Point Geometries do not intersect
10 ST_LineString 103 ST_Point Geometries intersect
10 ST_LineString 110 ST_LineString Geometries intersect
10 ST_LineString 120 ST_Polygon Geometries do not intersect
10 ST_LineString 121 ST_Polygon Geometries do not intersect
20 ST_Polygon 101 ST_Point Geometries intersect
20 ST_Polygon 102 ST_Point Geometries intersect
20 ST_Polygon 103 ST_Point Geometries do not intersect
20 ST_Polygon 110 ST_LineString Geometries do not intersect
20 ST_Polygon 120 ST_Polygon Geometries intersect
20 ST_Polygon 121 ST_Polygon Geometries do not intersect