How the PointFromText() function works in Mariadb?

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

Syntax

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

PointFromText(wkt)

Where wkt is a string value that represents a point in the Well-Known Text (WKT) format. The WKT format consists of the keyword POINT followed by the coordinates of the point in parentheses. For example, POINT(10 20) is the WKT representation of a point with the coordinates (10, 20). If wkt is NULL or not a valid WKT string, the function returns NULL.

Examples

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

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

SELECT PointFromText('POINT(10 20)') 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. 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 PointFromText() function:

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

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

  • LineStringFromText(): 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 LINESTRING().
  • PolygonFromText(): 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 POLYGON().
  • GeometryCollectionFromText(): 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 GEOMETRYCOLLECTION().

Conclusion

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