How the MBRDisjoint() function works in Mariadb?

The MBRDisjoint() function is a useful tool for performing spatial queries in Mariadb.

Posted on

The MBRDisjoint() function is a useful tool for performing spatial queries in Mariadb. It allows you to check whether the minimum bounding rectangle (MBR) of one geometry is disjoint from the MBR of another geometry. This means that the two geometries do not share any point in common, not even at the boundary.

Syntax

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

MBRDisjoint(g1, g2)

The function takes two arguments:

  • g1: A geometry value that represents the MBR of the first geometry. It can be a point, a line, a polygon, or a geometry collection.
  • g2: A geometry value that represents the MBR of the second geometry. It can be a point, a line, a polygon, or a geometry collection.

The function returns a boolean value that indicates whether the MBR of g1 is disjoint from the MBR of g2:

  • 1 (true): The MBR of g1 is disjoint from the MBR of g2.
  • 0 (false): The MBR of g1 is not disjoint from the MBR of g2.
  • NULL: Either g1 or g2 is NULL or invalid.

Examples

In this section, we will show some examples of how to use the MBRDisjoint() function in different scenarios.

Example 1: Checking if a point is disjoint from a rectangle

Suppose you have a table called places that stores the information of various places, such as their name, location, and type. The location column is a point geometry that represents the latitude and longitude of the place. You want to find the places that are outside of a rectangular area defined by two points: (40, -120) and (50, -110). You can use the MBRDisjoint() function to check if the location of each place is disjoint from the MBR of the rectangle. For example, you can execute the following statement:

SELECT name, location, type FROM places
WHERE MBRDisjoint(location, ST_GeomFromText('LINESTRING(40 -120, 50 -110)'));

This will return the name, location, and type of the places that are outside of the rectangular area, or an empty result set if none of them are. For example, the result might look like this:

+--------------+--------------------------+--------+
| name         | location                 | type   |
+--------------+--------------------------+--------+
| New York     | POINT(40.7128 -74.0060)  | city   |
| Miami        | POINT(25.7617 -80.1918)  | city   |
| Denver       | POINT(39.7392 -104.9903) | city   |
| Grand Canyon | POINT(36.1069 -112.1129) | park   |
+--------------+--------------------------+--------+

Example 2: Checking if a polygon is disjoint from another polygon

Suppose you have a table called countries that stores the information of various countries, such as their name, area, and boundary. The boundary column is a polygon geometry that represents the outline of the country. You want to find the countries that are disjoint from the boundary of China. You can use the MBRDisjoint() function to check if the boundary of each country is disjoint from the MBR of the boundary of China. For example, you can execute the following statement:

SELECT c1.name, c1.area, c1.boundary FROM countries c1
JOIN countries c2 ON c2.name = 'China'
WHERE MBRDisjoint(c1.boundary, c2.boundary);

This will return the name, area, and boundary of the countries that are disjoint from the boundary of China, or an empty result set if none of them are. For example, the result might look like this:

+-----------+----------+----------------------------------------------------------------+
| name      | area     | boundary                                                       |
+-----------+----------+----------------------------------------------------------------+
| USA       | 9833517  | POLYGON((-124.731422 48.400249, ... , -124.731422 48.400249))  |
| Canada    | 9984670  | POLYGON((-141.000000 60.000000, ... , -141.000000 60.000000))  |
| Brazil    | 8515767  | POLYGON((-73.987235 -9.838979, ... , -73.987235 -9.838979))    |
| Australia | 7692024  | POLYGON((112.921454 -10.996463, ... , 112.921454 -10.996463))  |
+-----------+----------+----------------------------------------------------------------+

Example 3: Checking if a line is disjoint from a circle

Suppose you have a table called roads that stores the information of various roads, such as their name, length, and path. The path column is a linestring geometry that represents the route of the road. You want to find the roads that do not intersect with a circular area with a radius of 10 kilometers and a center at (30, -100). You can use the MBRDisjoint() function to check if the MBR of the path of each road is disjoint from the MBR of the circle. For example, you can execute the following statement:

SELECT name, length, path FROM roads
WHERE MBRDisjoint(path, ST_Buffer(ST_GeomFromText('POINT(30 -100)'), 10));

This will return the name, length, and path of the roads that do not intersect with the circular area, or an empty result set if none of them do. For example, the result might look like this:

+-----------+--------+---------------------------------------------------------------+
| name      | length | path                                                          |
+-----------+--------+---------------------------------------------------------------+
| I-5       | 2220   | LINESTRING(32.7157 -117.1611, ... , 48.7519 -122.4787)        |
| I-95      | 3050   | LINESTRING(25.7748 -80.1977, ... , 43.6615 -70.2553)          |
| Route 1   | 1060   | LINESTRING(38.9072 -77.0369, ... , 44.3106 -69.7795)          |
| Route 101 | 2460   | LINESTRING(32.7157 -117.1611, ... , 47.6062 -122.3321)        |
+-----------+--------+---------------------------------------------------------------+

There are some other functions that are related to the MBRDisjoint() function and can be used to perform other spatial queries in Mariadb. Here are some of them:

  • MBRIntersects(): This function returns whether the MBRs of two geometries intersect with each other.
  • MBRWithin(): This function returns whether the MBR of one geometry is within the MBR of another geometry.
  • MBREqual(): This function returns whether the MBRs of two geometries are equal to each other.
  • MBRContains(): This function returns whether the MBR of one geometry contains the MBR of another geometry.

Conclusion

The MBRDisjoint() function is a powerful and flexible function that can help you perform spatial queries in Mariadb. You can use it to check whether the MBR of one geometry is disjoint from the MBR of another geometry. This means that the two geometries do not share any point in common, not even at the boundary. You can also use some other related functions to perform other spatial operations, such as intersection, within, equal, or contain. By using these functions, you can achieve a better analysis and understanding of your spatial data.