How the MultiPointFromWKB() function works in Mariadb?

The MultiPointFromWKB() function is a spatial function that converts a well-known binary (WKB) representation of a multipoint geometry into a multipoint value.

Posted on

The MultiPointFromWKB() function is a spatial function that converts a well-known binary (WKB) representation of a multipoint geometry into a multipoint value. This function is useful for importing or exporting spatial data from or to other applications that use the WKB format.

Syntax

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

MultiPointFromWKB(wkb [, srid])

The function takes two arguments:

  • wkb: A binary string that represents a multipoint geometry in WKB format. This argument is mandatory.
  • srid: An integer that represents the spatial reference system identifier (SRID) of the multipoint value. This argument is optional. If omitted, the default SRID of 0 is used.

The function returns a multipoint value that corresponds to the WKB representation. If the input is not a valid WKB representation of a multipoint geometry, the function returns NULL.

Examples

Example 1: Creating a multipoint value from a WKB representation

In this example, we use the MultiPointFromWKB() function to create a multipoint value from a WKB representation. We use the ST_AsText() function to display the result in a human-readable format.

SELECT ST_AsText(MultiPointFromWKB(0x0104000000020000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000004040));

The output is:

MULTIPOINT(1 1,2 2)

The WKB representation consists of a header that indicates the endianness, the geometry type, and the number of points, followed by the coordinates of each point. The endianness is 0x01 for little endian and 0x00 for big endian. The geometry type is 0x04 for multipoint. The number of points is 0x02 for two points. The coordinates of the first point are 0x000000000000F03F for 1 and 0x000000000000F03F for 1. The coordinates of the second point are 0x0000000000000040 for 2 and 0x0000000000004040 for 2.

Example 2: Creating a multipoint value from a WKB representation with a specified SRID

In this example, we use the MultiPointFromWKB() function to create a multipoint value from a WKB representation with a specified SRID. We use the ST_SRID() function to display the SRID of the result.

SELECT ST_SRID(MultiPointFromWKB(0x0104000020E6100000020000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000004040, 4326));

The output is:

4326

The WKB representation is similar to the previous example, except that it has a SRID of 0x20E61000 for 4326. The SRID is placed after the endianness and before the geometry type. The SRID indicates the coordinate system of the multipoint value, which in this case is the World Geodetic System 1984 (WGS 84).

Example 3: Creating a multipoint value from an invalid WKB representation

In this example, we use the MultiPointFromWKB() function to create a multipoint value from an invalid WKB representation. We use the ISNULL() function to check if the result is NULL.

SELECT ISNULL(MultiPointFromWKB(0x0104000000030000000101000000000000000000F03F000000000000F03F0101000000000000000000000040000000000000404001010000000000000000000008400000000000000840));

The output is:

1

The WKB representation is invalid because it has three points, but the number of points is 0x02 for two points. The function returns NULL, which is indicated by 1 in the output.

There are some other functions that are related to the MultiPointFromWKB() function, such as:

  • ST_AsWKB(): This function converts a geometry value into a WKB representation. It is the inverse of the MultiPointFromWKB() function. For example:

    SELECT ST_AsWKB(ST_GeomFromText('MULTIPOINT(1 1,2 2)'));
    

    The output is:

    0x0104000000020000000101000000000000000000F03F000000000000F03F01010000000000000000000000400000000000004040

Conclusion

The MultiPointFromWKB() function is a useful function for working with multipoint geometries in Mariadb. It allows us to convert a WKB representation of a multipoint geometry into a multipoint value. We can also specify the SRID of the multipoint value, or use the default SRID of 0. The function returns NULL if the input is not a valid WKB representation of a multipoint geometry. There are some other functions that are related to the MultiPointFromWKB() function, such as ST_MultiPointFromText(), ST_MultiPointFromWKB(), and ST_AsWKB(). These functions can help us to import, export, or manipulate spatial data in Mariadb.