How the GeometryFromText() function works in Mariadb?
The GeometryFromText()
function is a spatial function that creates a geometry object from a well-known text (WKT) representation.
The GeometryFromText()
function is a spatial function that creates a geometry object from a well-known text (WKT) representation. It can be used for storing or manipulating geometries of any type, such as point, linestring, polygon, etc.
Syntax
The syntax of the GeometryFromText()
function is as follows:
GeometryFromText(wkt, [srid])
The wkt
parameter is a string that contains the well-known text representation of the geometry. The function returns a geometry object that corresponds to the WKT string.
The optional srid
parameter is an integer that specifies the spatial reference system identifier (SRID) of the geometry. The function assigns the SRID to the geometry object if the parameter is provided. Otherwise, the SRID is set to 0 by default.
Examples
Example 1: Basic usage
The following example shows how to use the GeometryFromText()
function to create a point geometry from a WKT string.
SELECT AsText(GeometryFromText('POINT(1 1)')) AS geom;
The output is:
+------------+
| geom |
+------------+
| POINT(1 1) |
+------------+
The function returns a point geometry object that has the coordinates (1, 1). The object is stored as a binary string in the internal format of Mariadb.
Example 2: SRID parameter
The following example shows how to use the GeometryFromText()
function with the SRID parameter.
SELECT AsText(GeometryFromText('POINT(1 1)', 4326)) AS geom;
The output is:
+------------+
| geom |
+------------+
| POINT(1 1) |
+------------+
The function returns a point geometry object that has the SRID of 4326, which is the standard code for the WGS 84 coordinate system. The SRID is stored as the first four bytes of the binary string.
Example 3: Invalid WKT string
The following example shows how to use the GeometryFromText()
function with an invalid WKT string.
SELECT GeometryFromText('Invalid WKT string') AS geom;
The output is:
+------+
| geom |
+------+
| NULL |
+------+
The function returns NULL when the WKT string is invalid or does not represent a geometry.
Related Functions
Some of the functions that are related to the GeometryFromText()
function are:
GeomFromText()
: This function creates a geometry object from a well-known text (WKT) representation. It is the same as theGeometryFromText()
function, which means they have the same functionality and syntax.GeometryFromWKB()
: This function creates a geometry object from a well-known binary (WKB) representation. It is similar to theGeometryFromText()
function, except that it takes a binary string instead of a text string as the input.AsText()
: This function converts a geometry object to a well-known text representation. It is the inverse of theGeometryFromText()
function.
Conclusion
The GeometryFromText()
function is a useful function for creating a geometry object from a well-known text representation. It can be used for storing or manipulating geometries of any type, such as point, linestring, polygon, etc. It can also be combined with other spatial functions to achieve various spatial operations.