How the AsWKB() function works in Mariadb?

The AsWKB() function is a spatial function that returns the Well-Known Binary (WKB) representation of a geometry value.

Posted on

The AsWKB() function is a spatial function that returns the Well-Known Binary (WKB) representation of a geometry value. WKB is a binary format that can be used to store and exchange spatial data. The AsWKB() function is compatible with the Open Geospatial Consortium (OGC) standard.

Syntax

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

AsWKB(g)

The parameter g is a geometry value. The function returns a binary string that represents the WKB of the geometry value. If the parameter is NULL, the function returns NULL.

Examples

Example 1: Point

In this example, we use the AsWKB() function to get the WKB of a point geometry value.

SELECT AsWKB(Point(1, 2)) AS WKB;

The output is:

+------------------------------------+
| WKB                                |
+------------------------------------+
| 0x0101000000000000000000F03F0000000000000040 |
+------------------------------------+

The WKB of the point is a binary string that starts with 0x01, which indicates the byte order (little endian). The next byte is 0x01, which indicates the geometry type (point). The next eight bytes are the X coordinate of the point (1.0), and the last eight bytes are the Y coordinate of the point (2.0).

Example 2: LineString

In this example, we use the AsWKB() function to get the WKB of a linestring geometry value.

SELECT AsWKB(LineString(Point(1, 2), Point(3, 4))) AS WKB;

The output is:

+----------------------------------------------------+
| WKB                                                |
+----------------------------------------------------+
| 0x010200000002000000000000000000F03F000000000000004000000000000008400000000000001040 |
+----------------------------------------------------+

The WKB of the linestring is a binary string that starts with 0x01, which indicates the byte order (little endian). The next byte is 0x02, which indicates the geometry type (linestring). The next four bytes are the number of points in the linestring (2). The next 16 bytes are the WKB of the first point, and the last 16 bytes are the WKB of the second point.

Example 3: Polygon

In this example, we use the AsWKB() function to get the WKB of a polygon geometry value.

SELECT AsWKB(Polygon(LineString(Point(1, 1), Point(1, 4), Point(4, 4), Point(4, 1), Point(1, 1)))) AS WKB;

The output is:

+----------------------------------------------------------------------------------------------------------------+
| WKB                                                                                                            |
+----------------------------------------------------------------------------------------------------------------+
| 0x0103000000010000000500000000000000000000F03F000000000000F03F000000000000F03F0000000000001040000000000000104000000000000010400000000000001040000000000000F03F000000000000F03F000000000000F03F |
+----------------------------------------------------------------------------------------------------------------+

The WKB of the polygon is a binary string that starts with 0x01, which indicates the byte order (little endian). The next byte is 0x03, which indicates the geometry type (polygon). The next four bytes are the number of rings in the polygon (1). The next four bytes are the number of points in the first ring (5). The next 40 bytes are the WKB of the points in the first ring, which form a square.

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

  • AsWKT(): This function returns the Well-Known Text (WKT) representation of a geometry value. WKT is a human-readable format that can be used to represent spatial data.
  • GeomFromWKB(): This function converts a WKB value to a geometry value.
  • ST_AsBinary(): This function is an alias for the AsWKB() function.
  • ST_AsWKB(): This function is an alias for the AsWKB() function.

Conclusion

The AsWKB() function is a useful function that can be used to get the binary representation of a geometry value. It can be used to store and exchange spatial data in a compact and efficient way. The function follows the OGC standard and is compatible with other spatial functions and databases.