How the GeometryFromWKB() function works in Mariadb?

The GeometryFromWKB() function is a spatial function that creates a geometry object from a well-known binary (WKB) representation.

Posted on

The GeometryFromWKB() function is a spatial function that creates a geometry object from a well-known binary (WKB) representation. It can be used for storing or manipulating geometries of any type, such as point, linestring, polygon, etc.

Syntax

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

GeometryFromWKB(wkb, [srid])

The wkb parameter is a binary string that contains the well-known binary representation of the geometry. The function returns a geometry object that corresponds to the WKB 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 GeometryFromWKB() function to create a point geometry from a WKB string.

SELECT AsText(GeometryFromWKB(AsWKB(POINT(0, 0)))) AS geom;

The output is:

+------------+
| geom       |
+------------+
| POINT(0 0) |
+------------+

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: Invalid WKB string

The following example shows how to use the GeometryFromWKB() function with an invalid WKB string.

SELECT GeometryFromWKB('Invalid WKB string') AS geom;

The output is:

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

The function returns an error when the WKB string is invalid or does not represent a geometry.

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

  • GeomFromWKB(): This function creates a geometry object from a well-known binary (WKB) representation. It is the same as the GeometryFromWKB() function, which means they have the same functionality and syntax.
  • GeometryFromText(): This function creates a geometry object from a well-known text (WKT) representation. It is similar to the GeometryFromWKB() function, except that it takes a text string instead of a binary string as the input.
  • AsBinary(): This function converts a geometry object to a well-known binary representation. It is the inverse of the GeometryFromWKB() function.

Conclusion

The GeometryFromWKB() function is a useful function for creating a geometry object from a well-known binary 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.