#### 12.16.5.4 Functions for Testing Spatial Relations Between Geometric Objects

12.16.5.4.1 Relations on Geometry Minimal Bounding Rectangles (MBRs)
12.16.5.4.2 Functions That Test Spatial Relationships Between Geometries

The functions described in these sections take two geometries as input parameters and return a qualitative or quantitative relation between them.

##### 12.16.5.4.1 Relations on Geometry Minimal Bounding Rectangles (MBRs)

MySQL provides several functions that test relations between minimal bounding rectangles of two geometries `g1` and `g2`. The return values 1 and 0 indicate true and false, respectively.

• Returns 1 or 0 to indicate whether the Minimum Bounding Rectangle of `g1` contains the Minimum Bounding Rectangle of `g2`. This tests the opposite relationship as `MBRWithin()`.

```mysql> `SET @g1 = GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');`
mysql> `SET @g2 = GeomFromText('Point(1 1)');`
mysql> `SELECT MBRContains(@g1,@g2), MBRContains(@g2,@g1);`
----------------------+----------------------+
| MBRContains(@g1,@g2) | MBRContains(@g2,@g1) |
+----------------------+----------------------+
|                    1 |                    0 |
+----------------------+----------------------+
```
• Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries `g1` and `g2` are disjoint (do not intersect).

• Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries `g1` and `g2` are the same.

• Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries `g1` and `g2` intersect.

• Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries `g1` and `g2` overlap. The term spatially overlaps is used if two geometries intersect and their intersection results in a geometry of the same dimension but not equal to either of the given geometries.

• Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries `g1` and `g2` touch. Two geometries spatially touch if the interiors of the geometries do not intersect, but the boundary of one of the geometries intersects either the boundary or the interior of the other.

• Returns 1 or 0 to indicate whether the Minimum Bounding Rectangle of `g1` is within the Minimum Bounding Rectangle of `g2`. This tests the opposite relationship as `MBRContains()`.

```mysql> `SET @g1 = GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');`
mysql> `SET @g2 = GeomFromText('Polygon((0 0,0 5,5 5,5 0,0 0))');`
mysql> `SELECT MBRWithin(@g1,@g2), MBRWithin(@g2,@g1);`
+--------------------+--------------------+
| MBRWithin(@g1,@g2) | MBRWithin(@g2,@g1) |
+--------------------+--------------------+
|                  1 |                  0 |
+--------------------+--------------------+
```
##### 12.16.5.4.2 Functions That Test Spatial Relationships Between Geometries

The OpenGIS specification defines the following functions. They test the relationship between two geometry values `g1` and `g2`.

The return values 1 and 0 indicate true and false, respectively.

Note

Currently, MySQL does not implement these functions according to the specification. Those that are implemented return the same result as the corresponding MBR-based functions.

• Returns 1 or 0 to indicate whether `g1` completely contains `g2`. This tests the opposite relationship as `Within()`.

• Returns 1 if `g1` spatially crosses `g2`. Returns `NULL` if `g1` is a `Polygon` or a `MultiPolygon`, or if `g2` is a `Point` or a `MultiPoint`. Otherwise, returns 0.

The term spatially crosses denotes a spatial relation between two given geometries that has the following properties:

• The two geometries intersect

• Their intersection results in a geometry that has a dimension that is one less than the maximum dimension of the two given geometries

• Their intersection is not equal to either of the two given geometries

• Returns 1 or 0 to indicate whether `g1` is spatially disjoint from (does not intersect) `g2`.

• Returns 1 or 0 to indicate whether `g1` is spatially equal to `g2`.

• Returns 1 or 0 to indicate whether `g1` spatially intersects `g2`.

• Returns 1 or 0 to indicate whether `g1` spatially overlaps `g2`. The term spatially overlaps is used if two geometries intersect and their intersection results in a geometry of the same dimension but not equal to either of the given geometries.

• Returns 1 or 0 to indicate whether `g1` spatially touches `g2`. Two geometries spatially touch if the interiors of the geometries do not intersect, but the boundary of one of the geometries intersects either the boundary or the interior of the other.

• Returns 1 or 0 to indicate whether `g1` is spatially within `g2`. This tests the opposite relationship as `Contains()`.