How the GeomFromWKB() function works in Mariadb?

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

Posted on

The GeomFromWKB() function is a spatial function that creates a geometry object from a well-known binary (WKB) representation. It is an alias for the GeometryFromWKB() 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 GeomFromWKB() function is as follows:

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

SELECT AsText(GeomFromWKB(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: SRID parameter

The following example shows how to use the GeomFromWKB() function with the SRID parameter.

SELECT AsText(GeomFromWKB(AsWKB(POINT(0, 0)), 4326)) AS geom;

The output is:

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

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

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

SELECT GeomFromWKB('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 GeomFromWKB() function are:

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

Conclusion

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