How the MBRContains() function works in Mariadb?

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

Posted on

The MBRContains() 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 contains the MBR of another geometry. This can help you find the geometries that are within or overlap with a given area.

Syntax

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

MBRContains(g1, g2)

The function takes two arguments:

  • g1: A geometry value that represents the MBR of the containing geometry. It can be a point, a line, a polygon, or a geometry collection.
  • g2: A geometry value that represents the MBR of the contained 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 contains the MBR of g2:

  • 1 (true): The MBR of g1 contains the MBR of g2.
  • 0 (false): The MBR of g1 does not contain 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 MBRContains() function in different scenarios.

Example 1: Checking if a point is within 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 within a rectangular area defined by two points: (40, -120) and (50, -110). You can use the MBRContains() function to check if the location of each place is within the MBR of the rectangle. For example, you can execute the following statement:

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

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

+-------------+--------------------------+--------+
| name        | location                 | type   |
+-------------+--------------------------+--------+
| Seattle     | POINT(47.6062 -122.3321) | city   |
| Portland    | POINT(45.5051 -122.6750) | city   |
| Boise       | POINT(43.6150 -116.2023) | city   |
| Yellowstone | POINT(44.4280 -110.5885) | park   |
+-------------+--------------------------+--------+

Example 2: Checking if a polygon is within 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 within the boundary of China. You can use the MBRContains() function to check if the boundary of each country is within 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 MBRContains(c2.boundary, c1.boundary);

This will return the name, area, and boundary of the countries that are within 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                                                       |
+----------+----------+----------------------------------------------------------------+
| China    | 9596961  | POLYGON((73.499734 39.381740, ... , 73.499734 39.381740))      |
| Hong Kong| 1104     | POLYGON((114.113470 22.282940, ... , 114.113470 22.282940))    |
| Macau    | 32.9     | POLYGON((113.531410 22.189030, ... , 113.531410 22.189030))    |
| Taiwan   | 36197    | POLYGON((121.000210 25.295050, ... , 121.000210 25.295050))    |
+----------+----------+----------------------------------------------------------------+

Example 3: Checking if a line intersects with 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 intersect with a circular area with a radius of 10 kilometers and a center at (30, -100). You can use the MBRContains() function to check if the MBR of the path of each road contains the MBR of the circle. For example, you can execute the following statement:

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

This will return the name, length, and path of the roads that 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                                                          |
+----------+--------+---------------------------------------------------------------+
| Route 66 | 3940   | LINESTRING(34.746480 -92.289590, ... , 34.052230 -118.243680) |
| I-10     | 2460   | LINESTRING(30.396030 -88.885310, ... , 34.073620 -118.400360) |
+----------+--------+---------------------------------------------------------------+

There are some other functions that are related to the MBRContains() 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.
  • MBRDisjoint(): This function returns whether the MBRs of two geometries are disjoint from each other.

Conclusion

The MBRContains() 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 contains the MBR of another geometry. This can help you find the geometries that are within or overlap with a given area. You can also use some other related functions to perform other spatial operations, such as intersection, within, equal, or disjoint. By using these functions, you can achieve a better analysis and understanding of your spatial data.