ST_Touches function
The ST_Touches function takes two geometries as input parameters and returns 1 if the specified geometries spatially touch. Otherwise, 0 (zero) is returned.
Two geometries touch if the interiors of both geometries do not intersect, but the boundary of one of the geometries intersects with either the boundary or the interior of the other geometry.
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.
If both of the specified geometries are points or multipoints, or if any of the specified geometries is null or empty, then null is returned.
Syntax
Parameters
- geometry1
- A value of type ST_Geometry or one of its subtypes that represents the geometry that is to be tested to touch geometry2.
- geometry2
- A value of type ST_Geometry or one of its subtypes that represents the geometry that is to be tested to touch geometry1.
Return type
INTEGER
Example
Several geometries are added to the SAMPLE_GEOMS table. The ST_Touches function is then used to
determine which geometries touch each other.
CREATE TABLE sample_geoms (id INTEGER, geometry ST_Geometry)
INSERT INTO sample_geoms
VALUES (1, ST_Geometry('polygon ( (20 30, 30 30, 30 40, 20 40, 20 30) )' , 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( 60 60, 70 70 )' , 0) )
INSERT INTO sample_geoms
VALUES (5, ST_Geometry('linestring( 30 30, 60 60 )' , 0) )
SELECT a.id as id1, b.id as id2, ST_Touches (a.geometry, b.geometry) TOUCHES
FROM sample_geoms a, sample_geoms b
WHERE b.id >= a.id
Results:
ID ID TOUCHES
----------- ----------- -----------
1 1 0
1 2 1
1 3 0
1 4 0
1 5 1
2 2 0
2 3 0
2 4 0
2 5 0
3 3 0
3 4 1
3 5 0
4 4 0
4 5 1
5 5 0