# How the MBRWithin() function works in Mariadb?

The `MBRWithin()`

function is a useful tool for performing spatial queries in Mariadb.

The `MBRWithin()`

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 within the MBR of another geometry. This means that the first geometry is completely contained by the second geometry, including at the boundary.

## Syntax

The syntax of the `MBRWithin()`

function is as follows:

```
MBRWithin(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 within the MBR of `g2`

:

`1`

(true): The MBR of`g1`

is within the MBR of`g2`

.`0`

(false): The MBR of`g1`

is not within 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 `MBRWithin()`

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 `MBRWithin()`

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 MBRWithin(location, ST_GeomFromText('LINESTRING(40 -120, 50 -110)'));
```

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 |
+-------------+--------------------------+--------+
```

Note that the point (40, -120) is not within the rectangle, because it is on the boundary of the rectangle, not inside it.

### 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 `MBRWithin()`

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 MBRWithin(c1.boundary, c2.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((120.106189 23.970083, ... , 120.106189 23.970083)) |
+-----------+----------+----------------------------------------------------------------+
```

Note that the countries that are within the boundary of China, do not overlap with the boundary of China, because their MBRs are equal to the MBR of China, not overlapping with it.

### Example 3: Checking if a line is within 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 are within a circular area with a radius of 10 kilometers and a center at (30, -100). You can use the `MBRWithin()`

function to check if the MBR of the path of each road is within the MBR of the circle. For example, you can execute the following statement:

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

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

```
+--------+--------+---------------------------------------------------------------+
| name | length | path |
+--------+--------+---------------------------------------------------------------+
| Road C | 8.94 | LINESTRING(30.1 -99.9, 29.9 -100.1) |
+--------+--------+---------------------------------------------------------------+
```

Note that the line (30, -100) to (30.1, -99.9) is not within the circle, because it crosses the boundary of the circle, not inside it.

## Related Functions

There are some other functions that are related to the `MBRWithin()`

function and can be used to perform other spatial queries in Mariadb. Here are some of them:

`MBREqual()`

: This function returns whether the MBRs of two geometries are equal to each other.`MBRIntersects()`

: This function returns whether the MBRs of two geometries intersect with each other.`MBRDisjoint()`

: This function returns whether the MBRs of two geometries are disjoint from each other.`MBRContains()`

: This function returns whether the MBR of one geometry contains the MBR of another geometry.`MBRTouches()`

: This function returns whether the MBRs of two geometries touch each other.

## Conclusion

The `MBRWithin()`

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 within the MBR of another geometry. This means that the first geometry is completely contained by the second geometry, including at the boundary. You can also use some other related functions to perform other spatial operations, such as equal, intersect, disjoint, contain, or touch. By using these functions, you can achieve a better analysis and understanding of your spatial data.