ST_MBRIntersects function

The ST_MBRIntersects function takes two geometries as input parameters and returns 1 if the minimum bounding rectangles of the two geometries intersect. Otherwise, 0 (zero) is returned. The minimum bounding rectangle of a point and a horizontal or vertical linestring is the geometry itself.

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 either of the specified geometries is null or is empty, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramST_MBRIntersects(geometry1,geometry2 )

Parameters

geometry1
A value of type ST_Geometry or one of its subtypes that represents the geometry whose minimum bounding rectangle is to be tested for intersection with the minimum bounding rectangle of geometry2.
geometry2
A value of type ST_Geometry or one of its subtypes that represents the geometry whose minimum bounding rectangle is to be tested for intersection with the minimum bounding rectangle of geometry1.

Return type

INTEGER

Examples

Example 1
This example illustrates the use of ST_MBRIntersects to get an approximation of whether two nonintersecting polygons are close to each other by seeing if their minimum bounding rectangles intersect. The first example uses the SQL CASE expression. The second example uses a single SELECT statement to find those polygons that intersect the minimum bounding rectangle of the polygon with ID = 2.

CREATE TABLE sample_polys (id INTEGER, geometry ST_Polygon)

INSERT INTO sample_polys
  VALUES (1, ST_Polygon ('polygon (( 0 0, 30 0, 40 30, 40 35,
                                     5 35, 5 10, 20 10, 20 5, 0 0 ))', 0) )

INSERT INTO sample_polys
  VALUES (2, ST_Polygon ('polygon (( 15 15, 15 20, 60 20, 60 15,
                                     15 15 ))', 0) )

INSERT INTO sample_polys
  VALUES (3, ST_Polygon ('polygon (( 115 15, 115 20, 160 20, 160 15,
                                     115 15 ))', 0) )

Example 2
The following SELECT statement uses a CASE expression to find the IDs of the polygons that have minimum bounding rectangles that intersect.

SELECT a.id as id1, b.id as id2, 
    CASE ST_MBRIntersects (a.geometry, b.geometry)
      WHEN 0 THEN 'MBRs do not intersect'
      WHEN 1 THEN 'MBRs intersect'
    END AS MBR_INTERSECTS
  FROM  sample_polys a, sample_polys b
  WHERE a.id <= b.id
  ORDER BY id1, id2

Results:

ID1         ID2         MBR_INTERSECTS
----------- ----------- ---------------------
          1           1 MBRs intersect
          1           2 MBRs intersect
          1           3 MBRs do not intersect
          2           2 MBRs intersect
          2           3 MBRs do not intersect
          3           3 MBRs intersect
          
Example 3
The following SELECT statement determines whether the minimum bounding rectangles for the geometries intersect that for the polygon with ID = 2.

SELECT a.id, b.id, ST_MBRIntersects (a.geometry, b.geometry) MBR_INTERSECTS 
  FROM sample_polys a, sample_polys b
  WHERE a.id = 2
  ORDER BY id1, id2
Results

ID1         ID2         MBR_INTERSECTS
----------- ----------- --------------
          2           1              1
          2           2              1
          2           3              0