How the GLENGTH() function works in Mariadb?

The MariaDB GLENGTH() function is used to return the length of a lstring in units of its spatial reference.

Posted on

The MariaDB GLENGTH() function is used to return the length of a lstring in units of its spatial reference. It is commonly utilized in geographic information systems (GIS) to measure the distance between two points.

Syntax

The syntax for the MariaDB GLENGTH() function is as follows:

GLENGTH(lstring)

The function takes a lstring as an argument and returns a numeric value representing its length.

Examples

Example 1: Basic Usage of GLENGTH()

This example demonstrates the basic usage of the GLENGTH() function to calculate the length of a line.

SELECT GLENGTH(lstring(Point(0,0), Point(3,4)));
+---------------------------------------------+
| GLENGTH(lstring(Point(0,0), Point(3,4))) |
+---------------------------------------------+
|                                           5 |
+---------------------------------------------+

The output is 5, which is the length of the line segment connecting the points (0,0) and (3,4).

Example 2: Using GLENGTH() with Variables

This example shows how to use GLENGTH() with variables representing points.

SET @Start = Point(0,0);
SET @End = Point(5,12);

SELECT GLENGTH(lstring(@Start, @End));
+-----------------------------------+
| GLENGTH(lstring(@Start, @End)) |
+-----------------------------------+
|                                13 |
+-----------------------------------+

The output is 13, indicating the length of the line segment defined by the variables @Start and @End.

Example 3: GLENGTH() with a Defined lstring

Here, we define a lstring and then use GLENGTH() to find its length.

SET @Line = lstring(Point(0,0), Point(6,8));

SELECT GLENGTH(@Line);
+----------------+
| GLENGTH(@Line) |
+----------------+
|             10 |
+----------------+

The output 10 is the length of the line segment stored in the variable @Line.

Example 4: GLENGTH() with Invalid Input

This example shows what happens when GLENGTH() is used with invalid input.

SELECT GLENGTH(Point(0,0));
+---------------------+
| GLENGTH(Point(0,0)) |
+---------------------+
|                   0 |
+---------------------+

The output is 0 because GLENGTH() expects a lstring, not a Point.

Below are a few functions related to the MariaDB GLENGTH() function:

  • MariaDB ST_Length() function is used to return the length of a lstring in its spatial reference.
  • MariaDB ST_Distance() function is used to return the shortest distance between two geometries.

Conclusion

Understanding the GLENGTH() function in MariaDB is essential for working with spatial data, especially when calculating distances between points. By following the syntax and examples provided, users can effectively implement this function in their GIS applications.