How the ST_ENDPOINT() function works in MariaDB?
The ST_ENDPOINT()
function in MariaDB is used to retrieve the endpoint of a linestring geometry.
The ST_ENDPOINT()
function in MariaDB is used to retrieve the endpoint of a linestring geometry. For a linestring, the endpoint is the last point in the sequence of points that defines the linestring.
Syntax
The syntax for the MariaDB ST_ENDPOINT()
function is as follows:
ST_ENDPOINT(ls)
ls
: A linestring value representing the spatial object.
The function returns a point geometry representing the endpoint of the linestring. If the input is not a linestring, it returns NULL
.
Examples
Example 1: Endpoint of a LineString
This example demonstrates how to retrieve the endpoint of a linestring.
SET @line = ST_GeomFromText('LINESTRING(0 0, 2 2, 4 4)');
SELECT ST_AsText(ST_ENDPOINT(@line));
Output:
POINT(4 4)
The output shows the endpoint of the linestring as a point geometry.
Example 2: Endpoint of a Closed LineString
This example shows the endpoint of a closed linestring (where the start and end points are the same).
SET @line = ST_GeomFromText('LINESTRING(0 0, 2 2, 4 4, 0 0)');
SELECT ST_AsText(ST_ENDPOINT(@line));
Output:
POINT(0 0)
The output represents the endpoint of the closed linestring, which is the same as the starting point.
Example 3: Endpoint of a Point
This example demonstrates the behavior of ST_ENDPOINT()
when applied to a point geometry.
SET @point = ST_GeomFromText('POINT(1 1)');
SELECT ST_AsText(ST_ENDPOINT(@point));
Output:
NULL
The output is NULL
because the input is a point, not a linestring.
Example 4: Endpoint from a Table
This example retrieves the endpoint of linestrings stored in a table.
DROP TABLE IF EXISTS lines;
CREATE TABLE lines (id INT PRIMARY KEY, line LINESTRING);
INSERT INTO lines (id, line) VALUES
(1, ST_GeomFromText('LINESTRING(0 0, 2 2, 4 4)')),
(2, ST_GeomFromText('LINESTRING(1 1, 3 3, 5 5, 7 7)'));
SELECT id, ST_AsText(ST_ENDPOINT(line)) AS endpoint
FROM lines;
Output:
1, POINT(4 4)
2, POINT(7 7)
The output shows the endpoint of each linestring stored in the table.
Example 5: Endpoint of a MultiLineString
This example demonstrates the behavior of ST_ENDPOINT()
when applied to a multi-linestring geometry.
SET @multiline = ST_GeomFromText('MULTILINESTRING((0 0, 2 2), (3 3, 5 5))');
SELECT ST_AsText(ST_ENDPOINT(@multiline));
Output:
POINT(5 5)
The output represents the endpoint of the last linestring in the multi-linestring geometry.
Related Functions
The following are some functions related to MariaDB ST_ENDPOINT()
:
- MariaDB
ST_STARTPOINT()
function is used to retrieve the starting point of a linestring geometry. - MariaDB
ST_POINTN()
function is used to retrieve a specific point along a linestring geometry. - MariaDB
ST_NUMPOINTS()
function is used to retrieve the number of points in a linestring geometry. - MariaDB
ST_LENGTH()
function is used to calculate the length of a linestring geometry.
Conclusion
The ST_ENDPOINT()
function in MariaDB is a useful tool for working with linestring geometries. It allows you to retrieve the endpoint of a linestring, which can be valuable in various spatial analyses and operations, such as calculating distances, identifying intersections, or performing spatial joins. By combining this function with other spatial functions, you can perform more complex analyses and extract valuable insights from your spatial data.