ST_Relate function

The ST_Relate function determines whether two geometries meet the conditions specified by a Dimensionally Extended 9 Intersection Model (DE-9IM) matrix.

  • If the specified geometries meet the conditions specified by the matrix, it returns 1.
  • If either of the specified geometries is null or is empty, it returns null.
  • Otherwise, it returns 0.

If the second geometry is not represented in the same spatial reference system as the first geometry, it will be converted to the spatial reference system of the first geometry.

Syntax

Read syntax diagramSkip visual syntax diagramST_Relate(geometry1,geometry2 ,matrix)

Parameters

geometry1
A value of type ST_Geometry or one of its subtypes that represents the geometry that is tested against geometry2.
geometry2
A value of type ST_Geometry or one of its subtypes that represents the geometry that is tested against geometry1.
matrix
A value of CHAR(9) that represents the DE-9IM matrix which is to be used for the test of geometry1 and geometry2.

Return type

INTEGER

Example

The following code creates two separate polygons. Then, the ST_Relate function is used to determine several relationships between the two polygons. For example, whether the two polygons overlap.

CREATE TABLE sample_polys (id INTEGER, geometry ST_Polygon)

INSERT INTO sample_polys
  VALUES (1,
          ST_Polygon('polygon ( (40 120, 90 120, 90 150, 40 150, 40 120) )', 0))
INSERT INTO sample_polys
  VALUES (2,
          ST_Polygon('polygon ( (30 110, 50 110, 50 130, 30 130, 30 110) )', 0))

SELECT ST_Relate(a.geometry, b.geometry, 'T*T***T**')  "Overlaps  ",
       ST_Relate(a.geometry, b.geometry, 'T*T***FF*')  "Contains  ",
       ST_Relate(a.geometry, b.geometry, 'T*F**F***')  "Within    "
       ST_Relate(a.geometry, b.geometry, 'T********')  "Intersects",
       ST_Relate(a.geometry, b.geometry, 'T*F**FFF2')  "Equals    "
  FROM sample_polys a, sample_polys b
  WHERE a.id = 1 AND b.id = 2
Results:

Overlaps    Contains    Within      Intersects  Equals
----------- ----------- ----------- ----------- -----------
          1           0           0           1           0