How the OVERLAPS() function works in Mariadb?

The OVERLAPS() function is a spatial function in Mariadb that returns 1 or 0 to indicate whether two geometries spatially overlap.

Posted on

The OVERLAPS() function is a spatial function in Mariadb that returns 1 or 0 to indicate whether two geometries spatially 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. For example, two polygons spatially overlap if they share some but not all of their area, and two linestrings spatially overlap if they share some but not all of their length.

The OVERLAPS() function is based on the original MySQL implementation and uses object bounding rectangles to determine the spatial relationship between the geometries. This means that the function may return false positives, i.e., it may return 1 even if the geometries do not actually overlap in their shapes. To avoid this, use the ST_OVERLAPS() function instead, which uses object shapes to determine the spatial relationship.

Syntax

The syntax of the OVERLAPS() function is as follows:

OVERLAPS(g1, g2)

The function takes two arguments:

  • g1: A geometry object that represents the first geometry to be compared. The geometry object can be created using the GeomFromText() function or the ST_GeomFromText() function. The geometry object cannot be NULL.
  • g2: A geometry object that represents the second geometry to be compared. The geometry object can be created using the GeomFromText() function or the ST_GeomFromText() function. The geometry object cannot be NULL.

The function returns an integer that represents the spatial relationship between the geometries, as follows:

  • 1: If the geometries spatially overlap, i.e., their intersection results in a geometry of the same dimension but not equal to either of the given geometries.
  • 0: If the geometries do not spatially overlap, i.e., their intersection is empty, equal to one of the given geometries, or of a lower dimension than the given geometries.

Examples

Example 1: Checking if two polygons overlap

The following example checks if two polygons overlap using the OVERLAPS() function. The polygons are defined by their WKT representations.

SELECT OVERLAPS(GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'), GeomFromText('POLYGON((5 5, 5 15, 15 15, 15 5, 5 5))')) AS overlaps;

The output is:

+----------+
| overlaps |
+----------+
|        1 |
+----------+

The output shows that the two polygons overlap, as their intersection is a polygon of the same dimension but not equal to either of the given polygons.

Example 2: Checking if two linestrings overlap

The following example checks if two linestrings overlap using the OVERLAPS() function. The linestrings are defined by their WKT representations.

SELECT OVERLAPS(GeomFromText('LINESTRING(0 0, 10 10)'), GeomFromText('LINESTRING(5 5, 15 15)')) AS overlaps;

The output is:

+----------+
| overlaps |
+----------+
|        1 |
+----------+

The output shows that the two linestrings overlap, as their intersection is a linestring of the same dimension but not equal to either of the given linestrings.

Example 3: Checking if two points overlap

The following example checks if two points overlap using the OVERLAPS() function. The points are defined by their WKT representations.

SELECT OVERLAPS(GeomFromText('POINT(0 0)'), GeomFromText('POINT(0 0)')) AS overlaps;

The output is:

+----------+
| overlaps |
+----------+
|        0 |
+----------+

The output shows that the two points do not overlap, as their intersection is equal to one of the given points.

There are some other functions that are related to the OVERLAPS() function, such as:

  • ST_OVERLAPS(): This function is similar to the OVERLAPS() function, but it uses object shapes to determine the spatial relationship between the geometries. This means that the function is more accurate and does not return false positives. The syntax of the function is ST_OVERLAPS(g1, g2), where g1 and g2 are the same as in the OVERLAPS() function. The function returns the same result as the OVERLAPS() function, except when the geometries do not actually overlap in their shapes but only in their bounding rectangles.
  • INTERSECTS(): This function returns 1 or 0 to indicate whether two geometries intersect. The term intersect is used if two geometries share any portion of space. The syntax of the function is INTERSECTS(g1, g2), where g1 and g2 are the same as in the OVERLAPS() function. The function returns 1 if the geometries intersect, and 0 if they do not. The function is equivalent to the standard SQL function ST_Intersects().

Conclusion

The OVERLAPS() function is a useful function to check if two geometries spatially 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. The function is based on the original MySQL implementation and uses object bounding rectangles to determine the spatial relationship between the geometries. This means that the function may return false positives, i.e., it may return 1 even if the geometries do not actually overlap in their shapes. To avoid this, use the ST_OVERLAPS() function instead, which uses object shapes to determine the spatial relationship. The function takes two arguments, which are the geometries to be compared. The function returns an integer that represents the spatial relationship between the geometries, as follows:

  • 1: If the geometries spatially overlap, i.e., their intersection results in a geometry of the same dimension but not equal to either of the given geometries.
  • 0: If the geometries do not spatially overlap, i.e., their intersection is empty, equal to one of the given geometries, or of a lower dimension than the given geometries.

The function can be used to perform spatial analysis and queries on geometries, such as finding overlapping regions, areas, or lengths. The function can also be combined with other spatial functions, such as ST_Contains(), ST_Distance(), ST_Union(), etc., to perform more complex operations on geometries.