# How the BOUNDARY() function works in Mariadb?

The `BOUNDARY()`

function in MariaDB is designed to return the boundary of a geometric object.

The `BOUNDARY()`

function in MariaDB is designed to return the boundary of a geometric object. It is commonly used in geographic information systems (GIS) and spatial databases to manipulate and query spatial data.

## Syntax

The syntax for the MariaDB `BOUNDARY()`

function is as follows:

```
BOUNDARY(geom)
```

This function accepts a geometric object `geom`

as an argument and returns its boundary.

## Examples

### Example 1: Boundary of a Point

To find the boundary of a point, which is an empty set:

```
SELECT AsText(BOUNDARY(Point(1, 1)));
```

```
+-------------------------------+
| AsText(BOUNDARY(Point(1, 1))) |
+-------------------------------+
| GEOMETRYCOLLECTION EMPTY |
+-------------------------------+
```

The output is a `GEOMETRYCOLLECTION EMPTY`

because the boundary of a point is an empty set.

### Example 2: Boundary of a LineString

Here’s how to get the boundary of a `LineString`

:

```
SELECT AsText(BOUNDARY(LineString(Point(0, 0), Point(3, 3))));
```

```
+--------------------------------------------------------+
| AsText(BOUNDARY(LineString(Point(0, 0), Point(3, 3)))) |
+--------------------------------------------------------+
| MULTIPOINT(0 0,3 3) |
+--------------------------------------------------------+
```

The output is `MultiPoint(Point(0, 0), Point(3, 3))`

because the boundary of a `LineString`

consists of its endpoints.

### Example 3: Boundary of a Polygon

This example shows the boundary of a `Polygon`

:

```
SELECT AsText(BOUNDARY(Polygon(LineString(Point(0, 0), Point(4, 0), Point(4, 4), Point(0, 4), Point(0, 0)))))
AS result;
```

```
+---------------------------------+
| result |
+---------------------------------+
| LINESTRING(0 0,4 0,4 4,0 4,0 0) |
+---------------------------------+
```

The output is a `LineString`

that represents the boundary of the `Polygon`

.

### Example 4: Using with a Table

To use `BOUNDARY()`

with table data, let’s create a table with spatial data:

```
DROP TABLE IF EXISTS example;
CREATE TABLE example (g GEOMETRY);
INSERT INTO example VALUES (Polygon(LineString(Point(0, 0), Point(2, 0), Point(2, 2), Point(0, 2), Point(0, 0))));
```

Now, let’s find the boundary:

```
SELECT AsText(BOUNDARY(g)) FROM example;
```

```
+---------------------------------+
| AsText(BOUNDARY(g)) |
+---------------------------------+
| LINESTRING(0 0,2 0,2 2,0 2,0 0) |
+---------------------------------+
```

The output is a `LineString`

that represents the boundary of the `Polygon`

stored in the table.

### Example 5: Boundary of a MultiPolygon

Let’s find the boundary of a `MultiPolygon`

:

```
SELECT AsText(BOUNDARY(MultiPolygon(Polygon(LineString(Point(0, 0), Point(2, 0), Point(2, 2), Point(0, 2), Point(0, 0)))))) AS result;
```

```
+---------------------------------+
| result |
+---------------------------------+
| LINESTRING(0 0,2 0,2 2,0 2,0 0) |
+---------------------------------+
```

The output is a `MultiLineString`

that represents the boundaries of the `MultiPolygon`

.

## Related Functions

Here are a few functions related to MariaDB’s `BOUNDARY()`

:

- MariaDB’s
`ENVELOPE()`

function is used to return the minimum bounding rectangle of a geometry. - MariaDB’s
`GEOMETRYN()`

function is used to return the N-th geometry from a`GeometryCollection`

. - MariaDB’s
`INTERIORRINGN()`

function is used to return the N-th interior ring of a`Polygon`

.

## Conclusion

The `BOUNDARY()`

function in MariaDB is a crucial tool for spatial data analysis, allowing users to extract and work with the boundaries of various geometric shapes. Its applications are especially valuable in fields that require precise spatial data manipulation, such as urban planning, environmental modeling, and location-based services.