How the ENDPOINT() function works in Mariadb?

The ENDPOINT() function is a geometric function that returns the last point of a LineString geometry.

Posted on

The ENDPOINT() function is a geometric function that returns the last point of a LineString geometry. It is a synonym for the ST_ENDPOINT() function.

Syntax

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

ENDPOINT(line_string)

The function takes one argument, where:

  • line_string is a LineString geometry. It can be any valid expression that returns a LineString value.

The function returns a Point geometry that represents the last point of the LineString. If the argument is NULL, the function returns NULL.

Examples

Example 1: Returning the endpoint of a line string

In this example, we use the ENDPOINT() function to return the endpoint of a line string. We also use the ST_AsText() function to display the result in a human-readable format.

SELECT ST_AsText(ENDPOINT(ST_GeomFromText('LineString(1 2, 2 3, 3 4)'))) AS endpoint;

The output is:

+-----------+
| endpoint  |
+-----------+
| POINT(3 4)|
+-----------+

Example 2: Returning the endpoint of a line string column

In this example, we use the ENDPOINT() function to return the endpoint of a line string column from a table. We assume that the table has a column named path that stores the line string geometries of different routes.

CREATE TABLE routes (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  path LineString
);

INSERT INTO routes VALUES
(1, 'Route A', ST_GeomFromText('LineString(1 1, 2 2, 3 3)')),
(2, 'Route B', ST_GeomFromText('LineString(4 4, 5 5, 6 6)')),
(3, 'Route C', ST_GeomFromText('LineString(7 7, 8 8, 9 9)'));

SELECT id, name, ST_AsText(ENDPOINT(path)) AS endpoint FROM routes;

The output is:

+----+--------+-----------+
| id | name   | endpoint  |
+----+--------+-----------+
| 1  | Route A| POINT(3 3)|
| 2  | Route B| POINT(6 6)|
| 3  | Route C| POINT(9 9)|
+----+--------+-----------+

Example 3: Returning the endpoint of an empty line string

In this example, we use the ENDPOINT() function to return the endpoint of an empty line string. We expect the function to return NULL.

SELECT ST_AsText(ENDPOINT(ST_GeomFromText('LineString()'))) AS endpoint;

The output is:

+----------+
| endpoint |
+----------+
| NULL     |
+----------+

Example 4: Returning the endpoint of a line string with only one point

In this example, we use the ENDPOINT() function to return the endpoint of a line string with only one point. We expect the function to return the same point as the endpoint.

SELECT ST_AsText(ENDPOINT(ST_GeomFromText('LineString(1 1)'))) AS endpoint;

The output is:

+-----------+
| endpoint  |
+-----------+
| POINT(1 1)|
+-----------+

Example 5: Returning the endpoint of a null argument

In this example, we use the ENDPOINT() function to return the endpoint of a null argument. We expect the function to return NULL.

SELECT ST_AsText(ENDPOINT(NULL)) AS endpoint;

The output is:

+----------+
| endpoint |
+----------+
| NULL     |
+----------+

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

  • ST_ENDPOINT() function: This function is equivalent to the ENDPOINT() function. It returns the last point of a LineString geometry. For example, ST_ENDPOINT(ST_GeomFromText('LineString(1 2, 2 3, 3 4)')) returns POINT(3 4).
  • STARTPOINT() function: This function returns the first point of a LineString geometry. It is a synonym for the ST_STARTPOINT() function. For example, STARTPOINT(ST_GeomFromText('LineString(1 2, 2 3, 3 4)')) returns POINT(1 2).
  • ST_STARTPOINT() function: This function is equivalent to the STARTPOINT() function. It returns the first point of a LineString geometry. For example, ST_STARTPOINT(ST_GeomFromText('LineString(1 2, 2 3, 3 4)')) returns POINT(1 2).
  • NUMPOINTS() function: This function returns the number of points in a LineString geometry. It is a synonym for the ST_NUMPOINTS() function. For example, NUMPOINTS(ST_GeomFromText('LineString(1 2, 2 3, 3 4)')) returns 3.
  • ST_NUMPOINTS() function: This function is equivalent to the NUMPOINTS() function. It returns the number of points in a LineString geometry. For example, ST_NUMPOINTS(ST_GeomFromText('LineString(1 2, 2 3, 3 4)')) returns 3.

Conclusion

The ENDPOINT() function is a useful geometric function that can return the last point of a LineString geometry. It can be used to get the destination of a route, the end of a segment, or the terminal of a curve. It takes a LineString geometry as an argument, and returns a Point geometry that represents the endpoint. If the argument is NULL or empty, the function returns NULL. The ENDPOINT() function can be combined with other functions, such as ST_AsText(), ST_GeomFromText(), or STARTPOINT(), to perform various geometric operations.