The ST_Touches() function

The ST_Touches() function returns t (TRUE) if none of the points common to both geometries intersect the interiors of both geometries; otherwise, it returns f (FALSE). At least one geometry must be an ST_LineString, ST_Polygon, ST_MultiLineString, or ST_MultiPolygon.

Syntax

ST_Touches(g1 ST_Geometry, g2 ST_Geometry)

Usage

The following figure shows various geometric objects that touch but do not intersect with each other.
Figure 1: Touching geometries

This graphic shows various geometric objects that touch but do not intersect each other.

The results of the spatial relationship of the ST_Touches() function can be understood or verified by comparing the results with a pattern matrix that represents the acceptable values for the DE-9IM. The pattern matrices state that the ST_Touches() function returns TRUE when the interiors of the geometry do not intersect, and the boundary of either geometry intersects the other's interior or boundary.

The ST_Touches() function returns TRUE if the boundary of one geometry intersects the interior of the other but the interiors do not intersect.
Table 1. Pattern matrix for the ST_Touches() function

The table is a matrix. The top row and first column identify the geometry.

Interior (b) Boundary (b) Exterior (b)
Interior (a) F T *
Boundary (a) * * *
Exterior (a) * * *
The ST_Touches() function returns TRUE if the boundary of one geometry intersects the interior of the other but the interiors do not intersect.
Interior (b) Boundary (b) Exterior (b)
Interior (a) F * *
Boundary (a) T * *
Exterior (a) * * *
The ST_Touches() function returns TRUE if the boundaries of both geometries intersect but the interiors do not.
Interior (b) Boundary (b) Exterior (b)
Interior (a) F * *
Boundary (a) * T *
Exterior (a) * * *

Return type

BOOLEAN

Example

The GIS technician is asked to provide a list of all sewer lines whose endpoints intersect another sewer line.

The sewerlines table is created with three columns. The first column, sewer_id, uniquely identifies each sewer line. The INTEGER class column identifies the type of sewer line, generally associated with the line's capacity. The sewer ST_LineString column stores the sewer line's geometry:
CREATE TABLE sewerlines (sewer_id  integer,
                         class     integer,
                         sewer     ST_LineString);
The query returns a list of sewer_ids that touch one another:
SELECT s1.sewer_id, s2.sewer_id
   FROM sewerlines s1, sewerlines s2
   WHERE ST_Touches(s1.sewer, s2.sewer);