How the LineFromText() function works in Mariadb?

The LineFromText() function is a spatial function that converts a text representation of a line to a geometry value.

Posted on

The LineFromText() function is a spatial function that converts a text representation of a line to a geometry value. It can be used to create a line geometry from a well-known text (WKT) format, such as 'LINESTRING(0 0, 1 1, 2 2)'. The LineFromText() function is a synonym for the ST_LineFromText() function and can be used interchangeably.

Syntax

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

LineFromText(wkt, [srid])

The wkt is the text representation of the line in the WKT format. It must start with the keyword LINESTRING followed by a list of point coordinates in parentheses, separated by commas. Each point coordinate consists of two or more numeric values, representing the x, y, and optionally z and m dimensions. For example, 'LINESTRING(0 0, 1 1, 2 2)' is a valid WKT representation of a line.

The srid is an optional parameter that specifies the spatial reference system identifier (SRID) of the geometry. It can be any positive integer value that corresponds to a valid SRID in the spatial_ref_sys table. If the srid is not specified, the function assumes a default SRID of 0.

The LineFromText() function returns a geometry value that represents the line geometry constructed from the text representation. If the input text is not a valid WKT representation of a line, the function returns NULL.

Examples

In this section, we will show some examples of how to use the LineFromText() function in Mariadb.

Example 1: Creating a line geometry from a text representation

The following example shows how to use the LineFromText() function to create a line geometry from a text representation in the WKT format.

SELECT LineFromText('LINESTRING(0 0, 1 1, 2 2)');

The output is:

LineFromText('LINESTRING(0 0, 1 1, 2 2)')
--------------------------------------
0x0000000001030000000300000000000000000000000000000000000000000000000000F03F000000000000F03F0000000000000040000000000000004000000000000008400000000000000840

As you can see, the LineFromText() function returns a geometry value that represents the line geometry constructed from the text representation. The geometry value is displayed in a hexadecimal format, which can be converted to a human-readable format using the ST_AsText() function, as shown below:

SELECT ST_AsText(LineFromText('LINESTRING(0 0, 1 1, 2 2)'));

The output is:

ST_AsText(LineFromText('LINESTRING(0 0, 1 1, 2 2)'))
----------------------------------------------------
LINESTRING(0 0,1 1,2 2)

Example 2: Creating a line geometry from a text representation with a specified SRID

The following example shows how to use the LineFromText() function to create a line geometry from a text representation in the WKT format with a specified SRID.

SELECT LineFromText('LINESTRING(0 0, 1 1, 2 2)', 4326);

The output is:

LineFromText('LINESTRING(0 0, 1 1, 2 2)', 4326)
----------------------------------------------
0x00000000010A0000000300000000000000000000000000000000000000000000000000F03F000000000000F03F0000000000000040000000000000004000000000000008400000000000000840

As you can see, the LineFromText() function returns a geometry value that represents the line geometry constructed from the text representation with a specified SRID of 4326, which is the SRID for the WGS 84 coordinate system. The geometry value is displayed in a hexadecimal format, which can be converted to a human-readable format using the ST_AsText() function, as shown below:

SELECT ST_AsText(LineFromText('LINESTRING(0 0, 1 1, 2 2)', 4326));

The output is:

ST_AsText(LineFromText('LINESTRING(0 0, 1 1, 2 2)', 4326))
---------------------------------------------------------
LINESTRING(0 0,1 1,2 2)

Example 3: Creating a line geometry from an invalid text representation

The following example shows how to use the LineFromText() function to create a line geometry from an invalid text representation in the WKT format.

SELECT LineFromText('LINESTRING(0 0, 1 1, 2)');

The output is:

LineFromText('LINESTRING(0 0, 1 1, 2)')
-------------------------------------
NULL

As you can see, the LineFromText() function returns NULL, because the input text is not a valid WKT representation of a line. The input text has an invalid point coordinate, which has only two numeric values instead of three.

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

  • The LineFromWKB() function: This function converts a binary representation of a line to a geometry value. It is similar to the LineFromText() function, but it takes a well-known binary (WKB) format as the input, such as 0x0102000000030000000000000000000000000000000000000000000000000000000000F03F000000000000F03F0000000000000040000000000000004000000000000008400000000000000840.
  • The ST_AsText() function: This function converts a geometry value to a text representation in the WKT format. It is the inverse of the LineFromText() function. For example, ST_AsText(LineFromText('LINESTRING(0 0, 1 1, 2 2)')) returns 'LINESTRING(0 0, 1 1, 2 2)'.
  • The ST_AsWKB() function: This function converts a geometry value to a binary representation in the WKB format. It is the inverse of the LineFromWKB() function. For example, ST_AsWKB(LineFromText('LINESTRING(0 0, 1 1, 2 2)')) returns 0x0102000000030000000000000000000000000000000000000000000000000000000000F03F000000000000F03F0000000000000040000000000000004000000000000008400000000000000840.

Conclusion

In this article, we have learned how the LineFromText() function works in Mariadb. We have seen the syntax of the function, and some examples of how to use it with different types of text representations. We have also learned about some related functions that can be used with the LineFromText() function. The LineFromText() function is a useful function that can help us create line geometries from text representations in Mariadb.