# How the PolygonFromWKB() function works in Mariadb?

The `PolygonFromWKB()`

function is a spatial function in Mariadb that creates a `Polygon`

object from a well-known binary (WKB) representation.

The `PolygonFromWKB()`

function is a spatial function in Mariadb that creates a `Polygon`

object from a well-known binary (WKB) representation. A `Polygon`

object is a closed shape that consists of a set of linear rings. A linear ring is a closed and simple line string that does not cross itself. The first linear ring of a `Polygon`

object is the exterior ring, and the subsequent ones are the interior rings or holes.

## Syntax

The syntax of the `PolygonFromWKB()`

function is as follows:

```
PolygonFromWKB(wkb, [srid])
```

The function takes two arguments:

`wkb`

: A binary string that represents the well-known binary (WKB) representation of the polygon. The WKB format is a binary encoding of the well-known text (WKT) format. For example, the WKB representation of the polygon`POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))`

is`0x000000000103040000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`

.`srid`

: An optional integer that represents the spatial reference system identifier (SRID) of the polygon. If not specified, the default SRID is 0.

The function returns a `Polygon`

object if the input is valid, otherwise it returns `NULL`

.

## Examples

### Example 1: Creating a simple polygon

The following example creates a simple polygon with four vertices and no holes from a WKB string.

```
SELECT PolygonFromWKB(0x000000000103040000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) AS polygon;
```

The output is a binary representation of the polygon object, which can be converted to a human-readable format using the `ST_AsText()`

function.

```
SELECT ST_AsText(PolygonFromWKB(0x000000000103040000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)) AS polygon;
```

### Example 2: Creating a polygon with a hole

The following example creates a polygon with a hole from a WKB string. The hole is a smaller polygon inside the larger polygon.

```
SELECT PolygonFromWKB(0x000000000103080000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010400000000000000000000000000000001040000000000000000000000000000002040000000000000000000000000000002040000000000000000000000000000002040000000000000000000000000000001040000000000000000000000000000001040000000000000000000000000000001040000000000000000000000000000) AS polygon;
```

The output can be converted to a human-readable format using the `ST_AsText()`

function.

```
SELECT ST_AsText(PolygonFromWKB(0x000000000103080000000000000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010400000000000000000000000000000001040000000000000000000000000000002040000000000000000000000000000002040000000000000000000000000000002040000000000000000000000000000001040000000000000000000000000000001040000000000000000000000000000001040000000000000000000000000000)) AS polygon;
```

### Example 3: Creating a polygon with a specific SRID

The following example creates a polygon with a specific SRID of 4326, which corresponds to the WGS 84 coordinate system.

```
SELECT PolygonFromWKB(0x0000000001E610000003040000000000000000000000000000000000000000000000000000000000000000000000000024400000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000002440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) AS polygon;
```

The output can be converted to a human-readable format using the `ST_AsText()`

function.

## Related Functions

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

function, such as:

`PolygonFromText()`

: This function creates a`Polygon`

object from a well-known text (WKT) representation. The WKT format is a text encoding of the spatial object. For example, the WKT representation of the polygon`POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))`

is`POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))`

. The syntax of the function is`PolygonFromText(wkt, [srid])`

, where`wkt`

is a string and`srid`

is an optional integer.`ST_PolygonFromWKB()`

: This function is a synonym for the`PolygonFromWKB()`

function. The syntax of the function is`ST_PolygonFromWKB(wkb, [srid])`

, where`wkb`

is a binary string and`srid`

is an optional integer.

## Conclusion

The `PolygonFromWKB()`

function is a useful function to create a `Polygon`

object from a well-known binary (WKB) representation. A `Polygon`

object is a closed shape that consists of a set of linear rings. The function takes two arguments: a WKB string and an optional SRID. The function returns a `Polygon`

object if the input is valid, otherwise it returns `NULL`

. The function can be used to perform spatial operations on polygons, such as calculating the area, perimeter, centroid, etc. The function can also be combined with other spatial functions, such as `ST_Contains()`

, `ST_Intersects()`

, `ST_Union()`

, etc., to perform spatial analysis and queries on polygons.