How the GeomFromText() function works in Mariadb?

The GeomFromText() function is a spatial function that creates a geometry object from a well-known text (WKT) representation.

Posted on

The GeomFromText() function is a spatial function that creates a geometry object from a well-known text (WKT) representation. It is an alias for the GeometryFromText() function, which means they have the same functionality and syntax. It can be used for storing or manipulating geometries of any type, such as point, linestring, polygon, etc.

Syntax

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

GeomFromText(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 GeomFromText() function to create a point geometry from a WKT string.

SELECT AsText(GeomFromText('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 GeomFromText() function with the SRID parameter.

SELECT AsText(GeomFromText('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 GeomFromText() function with an invalid WKT string.

SELECT GeomFromText('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.

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

  • GeometryFromText(): This function creates a geometry object from a well-known text (WKT) representation. It is the same as the GeomFromText() function, which means they have the same functionality and syntax.
  • GeomFromWKB(): This function creates a geometry object from a well-known binary (WKB) representation. It is similar to the GeomFromText() 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 the GeomFromText() function.

Conclusion

The GeomFromText() 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.