ST_PointN

更新时间:
复制 MD 格式

Returns the Nth point in a LineString or circular string. Negative index values count from the end of the LineString, so -1 returns the last point. Returns NULL if the input geometry is not a LineString.

Syntax

geometry ST_PointN(geometry aLinestring, integer n)

Parameters

ParameterDescription
aLinestringThe LineString or circular string geometry.
nThe 1-based index of the point to return. Negative values count from the end: -1 is the last point.

Examples

Get the first point of a LineString:

SELECT ST_AsText(ST_PointN('LINESTRING(0 0,2 2)'::geometry, 1));
 st_astext
------------
 POINT(0 0)
(1 row)

Get the last point using a negative index:

SELECT ST_AsText(ST_PointN('LINESTRING(0 0,2 2)'::geometry, -1));
 st_astext
------------
 POINT(2 2)
(1 row)

Usage notes

To get the Nth point of each LineString within a MultiLineString, use ST_PointN together with ST_Dump.
ST_PointN supports circular strings and curves, and preserves Z coordinates for 3D geometries.