Skip to content

Spatial Relationship

Idan Sheinberg edited this page Dec 16, 2022 · 8 revisions

ST_Equals

Description: returns true if the two input geometries are "spatially equal", otherwise false Input Argument #1: Geometry (WKB)
Input Argument #2: Geometry (WKB)
Output: Boolean (Geometric equality)

SELECT ST_Equals(
  ST_GeomFromText(
    'LINESTRING(0 0, 10 10)'
  ),
  FROM_HEX(
    '0102000000020000000000000000000000000000000000000000000000000024400000000000002440'
  )
)

Result

true

Note
Using this inside the WHERE clause of the query will probably make more sense

ST_Within

Description: returns true if the the first input geometry is "spatially within" the second one, otherwise false
Input Argument #1: Geometry (WKB)
Input Argument #2: Geometry (WKB)
Output: Boolean ( is Geometry #1 is "spatially within" Geometry #2 )

SELECT ST_Within(
  ST_GeomFromText(
    'Point(0 0)'
  ),
  ST_GeomFromText(
    'LINESTRING(0 0, 10 10)'
  )
)

Result

false

Note #1
For "Spatially within" to be true, both geometries have the same SRID.
Also note that a geometry's boundary is not considered "within" it.
Thus, Point(0 0) is NOT within LINESTRING(0 0, 10 10)

Note #2
Using this inside the WHERE clause of the query will probably make more sense

ST_Touches

Description: returns true if the the first input geometry is intersects with the second one but their interior DO NOT. Otherwise , it returns false
Input Argument #1: Geometry (WKB)
Input Argument #2: Geometry (WKB)
Output: Boolean ( is Geometry #1 "touches" Geometry #2 )

SELECT ST_Touches(
    ST_GeomFromText(
       'LINESTRING(0 0, 1 1, 0 2)'
    ), 
    ST_GeomFromText(
       'POINT(0 2)'
    )
)

Result

true

Note
For Point/Point inputs, the relationship is always FALSE, as points do not have a boundary

ST_Overlaps

Description: returns true if two geometries intersect, have the same dimension, and are not completely contained by each other.
Input Argument #1: Geometry (WKB)
Input Argument #2: Geometry (WKB)
Output: Boolean ( is Geometry #1 "spatially overlaps" with Geometry #2 )

SELECT ST_Overlaps(a,b)
FROM (
  SELECT 
    ST_Buffer(
      ST_GeomFromText(
        'POINT(1 0.5)'
      ), 3
    ) AS a,
    ST_Buffer(
      ST_GeomFromText(
        'LINESTRING(1 0, 1 1, 3 5)'
      ),0.5
    ) AS b
)

Result

true

ST_Intersects

Description: returns true if two geometries shares any portion of space. Otherwise, returns false
Input Argument #1: Geometry (WKB)
Input Argument #2: Geometry (WKB)
Output: Boolean (does Geometry #1 "spatially intersects" Geometry #2)

SELECT ST_Intersects(
  ST_GeomFromText(
    'POINT(0 0)'
  ), 
  ST_GeomFromText(
    'LINESTRING(2 0, 0 2)'
  )
)

Result

false

ST_Crosses

Description: returns true if the two input geometries' intersection "spatially cross", otherwise false Input Argument #1: Geometry (WKB)
Input Argument #2: Geometry (WKB)
Output: Boolean (Geometric crossing)

SELECT ST_Crosses(
  ST_GeomFromText('MULTIPOINT((1 3), (4 1), (4 3))'),
  ST_GeomFromText('LINESTRING(1 1, 5 2, 2 5)')
)

Result

true

ST_DWithin

Description: returns true if the two input geometrs are within the specified distance, otherwise false Input Argument #1: Geometry (WKB)
Input Argument #2: Geometry (WKB) Input Argument #3: The Disatnce, specified in the input Geometries' SRID (Float)
Output: Boolean (Within distance indication)

SELECT ST_DWithin(
  ST_GeomFromText('POLYGON((0 0, 10 0, 10 5, 0 5, 0 0))'),
  ST_GeomFromText('POLYGON((12 0, 14 0, 14 6, 12 6, 12 0))'),
  1.0
)

Result

false

ST_Disjoint

Description: returns true if the two input geometrs are completely non-intersecting, otherwise false Input Argument #1: Geometry (WKB)
Input Argument #2: Geometry (WKB) Output: Boolean (Geometric Disjoint)

SELECT ST_Disjoint(
  ST_GeomFromText('POLYGON((1 1, 4 1, 4 5, 1 5, 1 1))'),
  ST_GeomFromText('POLYGON((6 3, 7 3, 7 6, 6 6, 6 3))')
)

Result

true