# How the MBRTouches() function works in Mariadb?

The `MBRTouches()`

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

The `MBRTouches()`

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 touches the MBR of another geometry. This means that the two geometries share some points at the boundary, but not at the interior.

## Syntax

The syntax of the `MBRTouches()`

function is as follows:

```
MBRTouches(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`

touches the MBR of `g2`

:

`1`

(true): The MBR of`g1`

touches the MBR of`g2`

.`0`

(false): The MBR of`g1`

does not touch 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 `MBRTouches()`

function in different scenarios.

### Example 1: Checking if a point touches 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 touch a rectangular area defined by two points: (40, -120) and (50, -110). You can use the `MBRTouches()`

function to check if the location of each place touches the MBR of the rectangle. For example, you can execute the following statement:

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

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

```
+----------+---------------------+--------+
| name | location | type |
+----------+---------------------+--------+
| Reno | POINT(40 -120) | city |
+----------+---------------------+--------+
```

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

### Example 2: Checking if a polygon touches 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 touch the boundary of China. You can use the `MBRTouches()`

function to check if the boundary of each country touches 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 MBRTouches(c1.boundary, c2.boundary);
```

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

```
+----------+----------+----------------------------------------------------------------+
| name | area | boundary |
+----------+----------+----------------------------------------------------------------+
| Mongolia | 1564110 | POLYGON((87.751264 49.297198, ... , 87.751264 49.297198)) |
| India | 3287263 | POLYGON((68.176645 23.691965, ... , 68.176645 23.691965)) |
| Nepal | 147181 | POLYGON((80.088425 28.794470, ... , 80.088425 28.794470)) |
| Pakistan | 881912 | POLYGON((60.874248 23.691965, ... , 60.874248 23.691965)) |
| Russia | 17125242 | POLYGON((-169.900000 66.884550, ... , -169.900000 66.884550)) |
+----------+----------+----------------------------------------------------------------+
```

Note that the countries that touch the boundary of China, do not overlap with the boundary of China, because their MBRs only share some points at the boundary, not at the interior.

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

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

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

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

```
+----------+--------+---------------------------------------------------------------+
| name | length | path |
+----------+--------+---------------------------------------------------------------+
| Road B | 12.36 | LINESTRING(30 -100, 30.1 -99.9) |
+----------+--------+---------------------------------------------------------------+
```

Note that the line (30, -100) to (30.1, -99.9) touches the circle, because it is tangent to the boundary of the circle, not crossing it.

## Related Functions

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

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

: This function returns whether the MBR of one geometry is within the MBR of another geometry.`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.`MBRIntersects()`

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

## Conclusion

The `MBRTouches()`

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 touches the MBR of another geometry. This means that the two geometries share some points at the boundary, but not at the interior. You can also use some other related functions to perform other spatial operations, such as equal, within, disjoint, contain, or intersect. By using these functions, you can achieve a better analysis and understanding of your spatial data.