How the PointFromWKB() function works in Mariadb?

The PointFromWKB() function is a built-in function in Mariadb that returns a point geometry that represents a location in a two-dimensional space.

Posted on

The PointFromWKB() function is a built-in function in Mariadb that returns a point geometry that represents a location in a two-dimensional space. The function is useful for storing and manipulating spatial data, such as coordinates, distances, and shapes. The function is also known as POINTFROMWKB().

Syntax

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

PointFromWKB(wkb)

Where wkb is a binary value that represents a point in the Well-Known Binary (WKB) format. The WKB format consists of a byte order, a type code, and the coordinates of the point. For example, 0x000000000101000000000000244000000000002440 is the WKB representation of a point with the coordinates (10, 20). If wkb is NULL or not a valid WKB value, the function returns NULL.

Examples

Example 1: Creating a point with the PointFromWKB() function

The following example shows how to use the PointFromWKB() function to create a point with the coordinates (10, 20):

SELECT PointFromWKB(0x000000000101000000000000244000000000002440) AS Point;

The function returns a point geometry that is encoded in a binary format. The function uses the Well-Known Binary (WKB) format to represent the point geometry.

Example 2: Extracting the coordinates of a point with the ST_X() and ST_Y() functions

The following example shows how to use the ST_X() and ST_Y() functions to extract the coordinates of a point created by the PointFromWKB() function:

SELECT ST_X(PointFromWKB(0x000000000101000000000000244000000000002440)) AS X,
       ST_Y(PointFromWKB(0x000000000101000000000000244000000000002440)) AS Y;

The output is:

+------+------+
| X    | Y    |
+------+------+
| 10   | 20   |
+------+------+

The function ST_X() returns the x-coordinate of the point, and the function ST_Y() returns the y-coordinate of the point. The functions use the Standardized Spatial Function (SSF) format to access the point geometry.

Example 3: Calculating the distance between two points with the ST_DISTANCE() function

The following example shows how to use the ST_DISTANCE() function to calculate the distance between two points created by the PointFromWKB() function:

SELECT ST_DISTANCE(PointFromWKB(0x000000000101000000000000244000000000002440), PointFromWKB(0x0000000001010000000000003E4000000000003440)) AS Distance;

The output is:

+-----------------+
| Distance        |
+-----------------+
| 28.2842712474619|
+-----------------+

The function ST_DISTANCE() returns the distance between two points in the same spatial reference system. The function uses the Euclidean distance formula to calculate the distance. The distance is measured in the same units as the coordinates of the points.

There are some other functions in Mariadb that are related to the PointFromWKB() function. They are:

  • PointFromText(): This function returns a point geometry that represents a location in a two-dimensional space. The function is useful for storing and manipulating spatial data, such as coordinates, distances, and shapes. The function is also known as POINT().
  • LineStringFromWKB(): This function returns a linestring geometry that represents a sequence of connected points. The function is useful for storing and manipulating linear features, such as roads, rivers, and borders. The function is also known as LINESTRINGFROMWKB().
  • PolygonFromWKB(): This function returns a polygon geometry that represents a closed area bounded by one or more linestrings. The function is useful for storing and manipulating areal features, such as countries, lakes, and islands. The function is also known as POLYGONFROMWKB().

Conclusion

The PointFromWKB() function is a useful function in Mariadb that allows you to return a point geometry that represents a location in a two-dimensional space. The function is helpful for storing and manipulating spatial data, such as coordinates, distances, and shapes. The function uses the Well-Known Binary (WKB) format to represent the point geometry. You can also use other functions like ST_X(), ST_Y(), and ST_DISTANCE() to access and manipulate the point geometry. I hope this article helped you understand how the PointFromWKB() function works in Mariadb.