Geospatial relationship functions
The following functions express relationships between two different geometries that
you specify as input and return results of type boolean
. The order in which
you specify the pair of geometries matters: the first geometry value is called the left
geometry, the second geometry value is called the right geometry.
These functions return:
-
TRUE
if and only if the relationship described by the function is satisfied. -
FALSE
if and only if the relationship described by the function is not satisfied.
ST_Contains(geometry, geometry)
Returns TRUE
if and only if the left geometry contains the right
geometry. Examples:
SELECT ST_Contains('POLYGON((0 2,1 1,0 -1,0 2))', 'POLYGON((-1 3,2 1,0 -3,-1 3))')
SELECT ST_Contains('POLYGON((0 2,1 1,0 -1,0 2))', ST_Point(0, 0))
SELECT ST_Contains(ST_GeometryFromText('POLYGON((0 2,1 1,0 -1,0 2))'), ST_GeometryFromText('POLYGON((-1 3,2 1,0 -3,-1 3))'))
ST_Crosses(geometry, geometry)
Returns TRUE
if and only if the left geometry crosses the right
geometry. Example:
SELECT ST_Crosses(ST_Line('linestring(1 1, 2 2 )'), ST_Line('linestring(0 1, 2 2)'))
ST_Disjoint(geometry, geometry)
Returns TRUE
if and only if the intersection of the left geometry and
the right geometry is empty. Example:
SELECT ST_Disjoint(ST_Line('linestring(0 0, 0 1)'), ST_Line('linestring(1 1, 1 0)'))
ST_Equals(geometry, geometry)
Returns TRUE
if and only if the left geometry equals the right
geometry. Example:
SELECT ST_Equals(ST_Line('linestring( 0 0, 1 1)'), ST_Line('linestring(1 3, 2 2)'))
ST_Intersects(geometry, geometry)
Returns TRUE
if and only if the left geometry intersects the right
geometry. Example:
SELECT ST_Intersects(ST_Line('linestring(8 7, 7 8)'), ST_Polygon('polygon((1 1, 4 1, 4 4, 1 4))'))
ST_Overlaps(geometry, geometry)
Returns TRUE
if and only if the left geometry overlaps the right
geometry. Example:
SELECT ST_Overlaps(ST_Polygon('polygon((2 0, 2 1, 3 1))'), ST_Polygon('polygon((1 1, 1 4, 4 4, 4 1))'))
ST_Relate(geometry, geometry, varchar)
Returns TRUE
if and only if the left geometry has the specified
dimensionally extended nine-intersection model (DE-9IMvarchar
) input takes the relationship.
Example:
SELECT ST_Relate(ST_Line('linestring(0 0, 3 3)'), ST_Line('linestring(1 1, 4 4)'), 'T********')
ST_Touches(geometry, geometry)
Returns TRUE
if and only if the left geometry touches the right
geometry.
Example:
SELECT ST_Touches(ST_Point(8, 8), ST_Polygon('polygon((1 1, 1 4, 4 4, 4 1))'))
ST_Within(geometry, geometry)
Returns TRUE
if and only if the left geometry is within the right
geometry.
Example:
SELECT ST_Within(ST_Point(8, 8), ST_Polygon('polygon((1 1, 1 4, 4 4, 4 1))'))