How the MPointFromWKB() function works in Mariadb?

The MPointFromWKB() function is a spatial function that returns a multipoint geometry value from a well-known binary (WKB) representation.

Posted on

The MPointFromWKB() function is a spatial function that returns a multipoint geometry value from a well-known binary (WKB) representation. The function can also accept an optional second argument that specifies the spatial reference system identifier (SRID) for the geometry value.

Syntax

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

MPointFromWKB(wkb, [srid])

The wkb argument is a binary string that represents the WKB representation of the multipoint geometry value. The WKB representation is a standard format for storing and exchanging spatial data in binary form.

The srid argument is an optional integer that specifies the SRID for the geometry value. The SRID is a unique identifier that defines the spatial reference system (SRS) of the geometry value. If the srid argument is omitted, the function uses the default SRID of 0.

Examples

Example 1: Basic usage

The following example shows how to use the MPointFromWKB() function to return a multipoint geometry value from a WKB representation:

SELECT MPointFromWKB(0x0104000000030000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000000040010100000000000000000000840000000000000840);

The output is:

MPointFromWKB(0x0104000000030000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000000040010100000000000000000000840000000000000840)
------------------------------------------------------------------------------------------------------------------------------------------------------------------
MULTIPOINT(1 1, 2 2, 3 3)

The function returns a multipoint geometry value that consists of three points with the coordinates (1, 1), (2, 2), and (3, 3).

Example 2: Using a different SRID

The following example shows how to use the MPointFromWKB() function with a different SRID to return a multipoint geometry value with a specific SRS:

SELECT MPointFromWKB(0x0104000020E6100000030000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000000040010100000000000000000000840000000000000840, 4326);

The output is:

MPointFromWKB(0x0104000020E6100000030000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000000040010100000000000000000000840000000000000840, 4326)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MULTIPOINT(1 1, 2 2, 3 3)

The function returns a multipoint geometry value that has the SRID of 4326, which corresponds to the WGS 84 SRS. The WGS 84 SRS is a global SRS that uses latitude and longitude as the coordinates.

Example 3: Using a hex string as the wkb argument

The following example shows how to use the MPointFromWKB() function with a hex string as the wkb argument:

SELECT MPointFromWKB(UNHEX('0104000000030000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000000040010100000000000000000000840000000000000840'));

The output is:

MPointFromWKB(UNHEX('0104000000030000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000000040010100000000000000000000840000000000000840'))
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
MULTIPOINT(1 1, 2 2, 3 3)

The function returns a multipoint geometry value, as it converts the hex string to a binary string using the UNHEX() function.

Example 4: Handling invalid arguments

The following example shows how the MPointFromWKB() function handles invalid arguments:

SELECT MPointFromWKB(0x0104000000020000000101000000000000000000F03F000000000000F03F0101000000000000000000000040);

The output is:

MPointFromWKB(0x0104000000020000000101000000000000000000F03F000000000000F03F0101000000000000000000000040)
--------------------------------------------------------------------------------------------------------------------------------------------------------
NULL

The function returns NULL, as it cannot parse the binary string as a valid WKB representation. The binary string is missing the y coordinate of the second point.

Example 5: Using the MPointFromWKB() function in a WHERE clause

The following example shows how to use the MPointFromWKB() function in a WHERE clause to filter the records based on the multipoint geometry value:

SELECT * FROM locations
WHERE location = MPointFromWKB(0x0104000000030000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000000040010100000000000000000000840000000000000840);

The output is:

id | name    | location
---|---------|---------
1  | Point A | MULTIPOINT(1 1, 2 2, 3 3)

The query returns the record that has the same multipoint geometry value as the one specified by the MPointFromWKB() function.

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

  • MPointFromText() function: Returns a multipoint geometry value from a well-known text (WKT) representation.
  • MULTIPOINT() function: Returns a multipoint geometry value from a set of point geometry values.
  • AsText() function: Returns the WKT representation of a geometry value.
  • AsBinary() function: Returns the WKB representation of a geometry value.

Conclusion

The MPointFromWKB() function is a useful function to get a multipoint geometry value from a WKB representation. The function can handle various types of arguments, such as binary or hex string values. The function can also accept an optional SRID argument to specify the SRS of the geometry value. The function is related to other spatial functions, such as MPointFromText(), MPoint(), AsText(), and AsBinary() functions.