How the GeometryType() function works in Mariadb?

The GeometryType() function is a spatial function that returns the type of a geometry object as a string.

Posted on

The GeometryType() function is a spatial function that returns the type of a geometry object as a string. It can be used to identify the type of any geometry, such as point, linestring, polygon, etc.

Syntax

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

GeometryType(geom)

The geom parameter is a geometry object of any type. The function returns a string that indicates the type of the geometry.

Examples

Example 1: Basic usage

The following example shows how to use the GeometryType() function to return the type of a point geometry.

SELECT GeometryType(GeometryFromText('POINT(1 1)')) AS type;

The output is:

+-------+
| type  |
+-------+
| POINT |
+-------+

The function returns ‘POINT’ as the type of the geometry.

Example 2: Geometry collection

The following example shows how to use the GeometryType() function to return the type of a geometry collection.

SELECT GeometryType(GeometryCollectionFromText('GEOMETRYCOLLECTION(POINT(1 1), LINESTRING(2 2, 3 3), POLYGON((4 4, 5 4, 5 5, 4 5, 4 4)))')) AS type;

The output is:

+--------------------+
| type               |
+--------------------+
| GEOMETRYCOLLECTION |
+--------------------+

The function returns ‘GEOMETRYCOLLECTION’ as the type of the geometry.

Example 3: Invalid geometry

The following example shows how to use the GeometryType() function with an invalid geometry.

SELECT GeometryType('Invalid geometry') AS type;

The output is:

ERROR 4079 (HY000): Illegal parameter data type varchar for operation 'st_geometrytype'

The function returns an error when the geometry is invalid or not a geometry object.

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

  • IsSimple(): This function returns 1 if the geometry is simple, 0 if it is not, or NULL if the geometry is invalid. A simple geometry is one that has no self-intersections or self-tangencies.
  • Dimension(): This function returns the dimension of the geometry as an integer. The dimension is 0 for points, 1 for linestrings or curves, 2 for polygons or surfaces, and the highest dimension of the components for geometry collections.
  • AsText(): This function converts a geometry object to a well-known text representation. It is the inverse of the GeometryFromText() function.

Conclusion

The GeometryType() function is a useful function for returning the type of a geometry object as a string. It can be used to identify the type of any geometry, such as point, linestring, polygon, etc. It can also be combined with other spatial functions to achieve various spatial operations.