ST_PointN

更新时间:
复制 MD 格式

Returns the Nth point of a LineString or circular string.

Syntax

geometry ST_PointN(geometry aLinestring, integer n);

Parameters

ParameterTypeDescription
aLinestringgeometryThe LineString or circular string to query.
nintegerThe 1-based index of the point to return. Negative values count from the end: -1 is the last point, -2 is the second-to-last, and so on.

Usage notes

  • Non-LineString input: Returns NULL if aLinestring is not a LineString or circular string (for example, a Polygon or MultiLineString).

  • Negative indexing: Negative values count backward from the end of the LineString. -1 returns the last point.

  • MultiLineString: To get the Nth point of each component LineString in a MultiLineString, combine ST_PointN with ST_Dump.

  • Circular strings and curves: ST_PointN supports circular strings and other curve types.

  • 3D objects: ST_PointN preserves Z coordinates and does not drop them.

Examples

Get the first point of a LineString

SELECT ST_AsText(ST_PointN('LINESTRING(0 0,2 2)'::geometry, 1));

Output:

 st_astext
------------
 POINT(0 0)
(1 row)

Get the last point using a negative index

Use -1 to get the last point without knowing the total number of points.

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

Output:

 st_astext
------------
 POINT(2 2)
(1 row)