How the GLENGTH() function works in Mariadb?

The GLENGTH() function is a spatial function that returns the length of a geometric object.

Posted on

The GLENGTH() function is a spatial function that returns the length of a geometric object. It can be used to measure the distance between two points, the perimeter of a polygon, the length of a line, or the circumference of a circle. The GLENGTH() function is compatible with the OpenGIS standard and supports various spatial data types, such as POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, and GEOMETRYCOLLECTION.

Syntax

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

GLENGTH(g)

where g is a geometric object. The function returns a numeric value that represents the length of the geometric object in the same unit as the spatial reference system (SRS) of the object. If the object has no SRS, the unit is assumed to be degrees.

Examples

Example 1: Calculate the distance between two points

The following example uses the GLENGTH() function to calculate the distance between two points on a plane. The points are represented by the POINT data type, which has two coordinates: x and y.

SELECT GLENGTH(GEOMFROMTEXT('POINT(1 2), POINT(4 6)')) AS distance;

The GEOMFROMTEXT() function converts a text representation of a geometric object into a binary representation. The POINT data type has the format POINT(x y), where x and y are the coordinates of the point. The GLENGTH() function returns the length of the line segment that connects the two points. The output is:

+----------+
| distance |
+----------+
| 5.000000 |
+----------+

The distance between the two points is 5 units.

Example 2: Calculate the perimeter of a polygon

The following example uses the GLENGTH() function to calculate the perimeter of a polygon. The polygon is represented by the POLYGON data type, which has one or more rings of points. The first ring is the outer boundary of the polygon, and the optional subsequent rings are the inner boundaries (holes) of the polygon. The rings are separated by commas, and each ring is enclosed by parentheses. The points in a ring are separated by spaces, and the first and last points of a ring must be the same.

SELECT GLENGTH(GEOMFROMTEXT('POLYGON((0 0, 0 4, 4 4, 4 0, 0 0))')) AS perimeter;

The POLYGON data type has the format POLYGON(ring1, ring2, ...), where ring1 is the outer boundary and ring2 and so on are the inner boundaries. The GLENGTH() function returns the sum of the lengths of all the sides of the polygon. The output is:

+-----------+
| perimeter |
+-----------+
| 16.000000 |
+-----------+

The perimeter of the polygon is 16 units.

Example 3: Calculate the length of a line

The following example uses the GLENGTH() function to calculate the length of a line. The line is represented by the LINESTRING data type, which has two or more points. The points are separated by commas, and each point has two coordinates: x and y.

SELECT GLENGTH(GEOMFROMTEXT('LINESTRING(0 0, 1 1, 2 3, 4 5)')) AS length;

The LINESTRING data type has the format LINESTRING(point1, point2, ...), where point1 and point2 and so on are the points that form the line. The GLENGTH() function returns the sum of the lengths of the line segments that connect the points. The output is:

+----------+
| length   |
+----------+
| 7.064495 |
+----------+

The length of the line is approximately 7.06 units.

Example 4: Calculate the circumference of a circle

The following example uses the GLENGTH() function to calculate the circumference of a circle. The circle is represented by the POLYGON data type, which has a single ring of points that are equally spaced along the circle. The number of points determines the accuracy of the approximation of the circle.

SELECT GLENGTH(GEOMFROMTEXT('POLYGON((0 1, 0.707 0.707, 1 0, 0.707 -0.707, 0 -1, -0.707 -0.707, -1 0, -0.707 0.707, 0 1))')) AS circumference;

The POLYGON data type has the format POLYGON(ring), where ring is the ring of points that form the circle. The GLENGTH() function returns the sum of the lengths of the sides of the polygon. The output is:

+--------------+
| circumference|
+--------------+
| 6.283185     |
+--------------+

The circumference of the circle is approximately 6.28 units, which is close to the exact value of 2π.

Some of the functions that are related to the GLENGTH() function are:

  • ST_LENGTH(): This function is an alias for the GLENGTH() function. It has the same syntax and behavior as the GLENGTH() function.
  • LENGTH(): This function returns the length of a string or a binary value. It has a different syntax and behavior from the GLENGTH() function. For example, LENGTH('abc') returns 3, while GLENGTH('abc') returns an error.
  • ST_DISTANCE(): This function returns the minimum distance between two geometric objects. It has a different syntax and behavior from the GLENGTH() function. For example, ST_DISTANCE(POINT(0 0), POINT(1 1)) returns 1.414214, while GLENGTH(POINT(0 0), POINT(1 1)) returns an error.

Conclusion

The GLENGTH() function is a useful function to calculate the length of a geometric object. It supports various spatial data types and follows the OpenGIS standard. It can be used to measure the distance between two points, the perimeter of a polygon, the length of a line, or the circumference of a circle. It has some related functions that have similar or different functionalities. The GLENGTH() function is a powerful tool for spatial analysis and manipulation in Mariadb.