How the PolygonFromText() function works in Mariadb?
The PolygonFromText()
function is a spatial function in Mariadb that creates a Polygon
object from a well-known text (WKT) representation.
The PolygonFromText()
function is a spatial function in Mariadb that creates a Polygon
object from a well-known text (WKT) 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 PolygonFromText()
function is as follows:
PolygonFromText(wkt, [srid])
The function takes two arguments:
wkt
: A string that represents the well-known text (WKT) representation of the polygon. The WKT format for a polygon isPOLYGON((x1 y1, x2 y2, ..., xn yn), (h1x1 h1y1, h1x2 h1y2, ..., h1xm h1ym), ...)
, where(x1 y1, x2 y2, ..., xn yn)
is the exterior ring and(h1x1 h1y1, h1x2 h1y2, ..., h1xm h1ym)
are the interior rings or holes. The exterior ring must be defined in a counterclockwise direction, and the interior rings must be defined in a clockwise direction. The first and last points of each ring must be the same.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 WKT string.
SELECT PolygonFromText('POLYGON((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(PolygonFromText('POLYGON((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 WKT string. The hole is a smaller polygon inside the larger polygon.
SELECT PolygonFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0), (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(PolygonFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0), (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 PolygonFromText('POLYGON((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(PolygonFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))', 4326)) AS polygon;
Related Functions
There are some other functions that are related to the PolygonFromText()
function, such as:
PolygonFromWKB()
: This function creates aPolygon
object from a well-known binary (WKB) representation. The WKB format is a binary encoding of the WKT format. The syntax of the function isPolygonFromWKB(wkb, [srid])
, wherewkb
is a binary string andsrid
is an optional integer.ST_PolygonFromText()
: This function is a synonym for thePolygonFromText()
function. The syntax of the function isST_PolygonFromText(wkt, [srid])
, wherewkt
is a string andsrid
is an optional integer.
Conclusion
The PolygonFromText()
function is a useful function to create a Polygon
object from a well-known text (WKT) representation. A Polygon
object is a closed shape that consists of a set of linear rings. The function takes two arguments: a WKT 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.