How the POLYGON() function works in Mariadb?

The POLYGON() function is a spatial function in Mariadb that creates a Polygon object from a list of coordinates.

Posted on

The POLYGON() function is a spatial function in Mariadb that creates a Polygon object from a list of coordinates. 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 POLYGON() function is as follows:

POLYGON(linestring1, linestring2, ...)

The function takes one or more arguments:

  • linestring1: A LineString object that represents the exterior ring of the polygon. The LineString object can be created using the LINESTRING() function or the GeomFromText() function. The exterior ring must be defined in a counterclockwise direction, and the first and last points must be the same.
  • linestring2, ...: Optional LineString objects that represent the interior rings or holes of the polygon. The LineString objects can be created using the LINESTRING() function or the GeomFromText() function. The interior rings must be defined in a clockwise direction, and the first and last points must be the same.

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 list of coordinates.

SELECT POLYGON(LINESTRING(0 0, 0 10, 10 10, 10 0, 0 0)) 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(POLYGON(LINESTRING(0 0, 0 10, 10 10, 10 0, 0 0))) AS polygon;

Example 2: Creating a polygon with a hole

The following example creates a polygon with a hole from a list of coordinates. The hole is a smaller polygon inside the larger polygon.

SELECT POLYGON(LINESTRING(0 0, 0 10, 10 10, 10 0, 0 0), LINESTRING(2 2, 2 8, 8 8, 8 2, 2 2)) AS polygon;

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

SELECT ST_AsText(POLYGON(LINESTRING(0 0, 0 10, 10 10, 10 0, 0 0), LINESTRING(2 2, 2 8, 8 8, 8 2, 2 2))) 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 POLYGON(LINESTRING(0 0, 0 10, 10 10, 10 0, 0 0), 4326) AS polygon;

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

SELECT ST_AsText(POLYGON(LINESTRING(0 0, 0 10, 10 10, 10 0, 0 0), 4326)) AS polygon;

There are some other functions that are related to the POLYGON() function, such as:

  • LINESTRING(): This function creates a LineString object from a list of coordinates. A LineString object is a one-dimensional shape that consists of a sequence of points. The syntax of the function is LINESTRING(x1 y1, x2 y2, ...), where x1 y1, x2 y2, ... are the coordinates of the points.
  • GeomFromText(): This function creates a geometry 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 GeomFromText(wkt, [srid]), where wkt is a string and srid is an optional integer.

Conclusion

The POLYGON() function is a useful function to create a Polygon object from a list of coordinates. A Polygon object is a closed shape that consists of a set of linear rings. The function takes one or more arguments, each of which is a LineString object that represents a linear ring. 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.