# How the MULTIPOLYGON() function works in Mariadb?

The `MULTIPOLYGON()`

function is a spatial function that creates a multipolygon value from a set of polygon values.

The `MULTIPOLYGON()`

function is a spatial function that creates a multipolygon value from a set of polygon values. A multipolygon is a geometry that consists of one or more polygons, each of which may have one or more holes. This function is useful for representing complex shapes that cannot be expressed by a single polygon.

## Syntax

The syntax of the `MULTIPOLYGON()`

function is as follows:

```
MULTIPOLYGON(polygon [, polygon] ...)
```

The function takes one or more arguments, each of which is a polygon value. A polygon value can be created by using the `POLYGON()`

function, which takes a set of linear rings as arguments. A linear ring is a closed and simple line string, which can be created by using the `LINESTRING()`

function, which takes a set of points as arguments. A point is a pair of coordinates, which can be written as `X Y`

or `(X, Y)`

.

The function returns a multipolygon value that contains all the polygon values as its components. If any of the arguments is not a valid polygon value, the function returns NULL.

## Examples

### Example 1: Creating a multipolygon value from two polygon values

In this example, we use the `MULTIPOLYGON()`

function to create a multipolygon value from two polygon values. We use the `ST_AsText()`

function to display the result in a human-readable format.

```
SELECT ST_AsText(MULTIPOLYGON(POLYGON((0 0, 0 3, 3 3, 3 0, 0 0)), POLYGON((4 4, 4 7, 7 7, 7 4, 4 4))));
```

The output is:

`MULTIPOLYGON(((0 0,0 3,3 3,3 0,0 0)),((4 4,4 7,7 7,7 4,4 4)))`

The multipolygon value consists of two polygon values, each of which has one linear ring. The first polygon value has the coordinates (0, 0), (0, 3), (3, 3), (3, 0), and (0, 0) for its linear ring. The second polygon value has the coordinates (4, 4), (4, 7), (7, 7), (7, 4), and (4, 4) for its linear ring.

### Example 2: Creating a multipolygon value from a polygon value with a hole

In this example, we use the `MULTIPOLYGON()`

function to create a multipolygon value from a polygon value with a hole. We use the `ST_AsText()`

function to display the result in a human-readable format.

```
SELECT ST_AsText(MULTIPOLYGON(POLYGON((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 2 8, 8 8, 8 2, 2 2))));
```

The output is:

`MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0),(2 2,2 8,8 8,8 2,2 2)))`

The multipolygon value consists of one polygon value, which has two linear rings. The first linear ring is the outer boundary of the polygon, which has the coordinates (0, 0), (0, 10), (10, 10), (10, 0), and (0, 0). The second linear ring is the inner boundary of the polygon, which represents a hole, which has the coordinates (2, 2), (2, 8), (8, 8), (8, 2), and (2, 2).

### Example 3: Creating a multipolygon value from an invalid polygon value

In this example, we use the `MULTIPOLYGON()`

function to create a multipolygon value from an invalid polygon value. We use the `ISNULL()`

function to check if the result is NULL.

```
SELECT ISNULL(MULTIPOLYGON(POLYGON((0 0, 0 3, 3 3, 0 0))));
```

The output is:

`1`

The polygon value is invalid because it has only three points, which is not enough to form a closed and simple linear ring. The function returns NULL, which is indicated by 1 in the output.

## Related Functions

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

function, such as:

`ST_AsWKB()`

: This function converts a geometry value into a WKB representation. For example:

```
SELECT ST_AsWKB(MULTIPOLYGON(POLYGON((0 0, 0 3, 3 3, 3 0, 0 0)), POLYGON((4 4, 4 7, 7 7, 7 4, 4 4))));
```

The output is:

`0x0106000000020000000103000000010000000500000000000000000000000000000000000000000000000000000000000000000000000008400000000000000840000000000000084000000000000008400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000103000000010000000500000000000000000010400000000000001040000000000000104000000000000010400000000000001040000000000000104000000000000010400000000000001040000000000000104000000000000010400000000000001040`

## Conclusion

The `MULTIPOLYGON()`

function is a useful function for working with multipolygon geometries in Mariadb. It allows us to create a multipolygon value from a set of polygon values. We can also create polygon values with holes by using multiple linear rings. The function returns NULL if any of the arguments is not a valid polygon value. There are some other functions that are related to the `MULTIPOLYGON()`

function, such as `ST_MultiPolygonFromText()`

, `ST_MultiPolygonFromWKB()`

, and `ST_AsWKB()`

. These functions can help us to import, export, or manipulate spatial data in Mariadb.