How the POINT() function works in Mariadb?

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

Syntax

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

POINT(x, y)

Where x and y are numeric values that represent the coordinates of the point. If x or y is NULL, the function returns NULL.

Examples

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

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

SELECT POINT(10, 20) AS Point;

The output is:

+--------------------------+
| Point                    |
+--------------------------+
| 0x0000000001010000000000 |
| 00244000000000002440    |
+--------------------------+

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. The WKB format consists of a byte order, a type code, and the coordinates of the point.

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 POINT() function:

SELECT ST_X(POINT(10, 20)) AS X,
       ST_Y(POINT(10, 20)) 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 POINT() function:

SELECT ST_DISTANCE(POINT(10, 20), POINT(30, 40)) 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 POINT() function. They are:

  • LINESTRING(): 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 LINESTRINGFROMTEXT().
  • POLYGON(): 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 POLYGONFROMTEXT().
  • GEOMETRYCOLLECTION(): This function returns a geometry collection geometry that represents a collection of zero or more geometries of any type. The function is useful for storing and manipulating heterogeneous spatial data, such as maps, scenes, and models. The function is also known as GEOMETRYCOLLECTIONFROMTEXT().

Conclusion

The POINT() 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 POINT() function works in Mariadb.