How the AsWKT() function works in Mariadb?

The AsWKT() function is a spatial function that returns the Well-Known Text (WKT) representation of a geometry value. WKT is a human-readable format that can be used to represent spatial data.

Posted on

The AsWKT() function is a spatial function that returns the Well-Known Text (WKT) representation of a geometry value. WKT is a human-readable format that can be used to represent spatial data. The AsWKT() function is compatible with the Open Geospatial Consortium (OGC) standard.

Syntax

The syntax of the AsWKT() function is as follows:

AsWKT(g)

The parameter g is a geometry value. The function returns a string that represents the WKT of the geometry value. If the parameter is NULL, the function returns NULL.

Examples

Example 1: Point

In this example, we use the AsWKT() function to get the WKT of a point geometry value.

SELECT AsWKT(Point(1, 2)) AS WKT;

The output is:

+-----------------+
| WKT             |
+-----------------+
| POINT(1 2)      |
+-----------------+

The WKT of the point is a string that starts with the geometry type (POINT), followed by the coordinates of the point in parentheses, separated by a space.

Example 2: LineString

In this example, we use the AsWKT() function to get the WKT of a linestring geometry value.

SELECT AsWKT(LineString(Point(1, 2), Point(3, 4))) AS WKT;

The output is:

+--------------------------+
| WKT                      |
+--------------------------+
| LINESTRING(1 2, 3 4)     |
+--------------------------+

The WKT of the linestring is a string that starts with the geometry type (LINESTRING), followed by the coordinates of the points in parentheses, separated by commas.

Example 3: Polygon

In this example, we use the AsWKT() function to get the WKT of a polygon geometry value.

SELECT AsWKT(Polygon(LineString(Point(1, 1), Point(1, 4), Point(4, 4), Point(4, 1), Point(1, 1)))) AS WKT;

The output is:

+---------------------------------------------------+
| WKT                                               |
+---------------------------------------------------+
| POLYGON((1 1, 1 4, 4 4, 4 1, 1 1))                |
+---------------------------------------------------+

The WKT of the polygon is a string that starts with the geometry type (POLYGON), followed by the coordinates of the points in parentheses, separated by commas. The points form a closed ring that defines the boundary of the polygon.

Some of the functions that are related to the AsWKT() function are:

  • AsWKB(): This function returns the Well-Known Binary (WKB) representation of a geometry value. WKB is a binary format that can be used to store and exchange spatial data.
  • GeomFromWKT(): This function converts a WKT value to a geometry value.
  • ST_AsText(): This function is an alias for the AsWKT() function.
  • ST_AsWKT(): This function is an alias for the AsWKT() function.

Conclusion

The AsWKT() function is a useful function that can be used to get the text representation of a geometry value. It can be used to display and inspect spatial data in a readable way. The function follows the OGC standard and is compatible with other spatial functions and databases.