Returns the Nth point of a LineString or circular string.
Syntax
geometry ST_PointN(geometry aLinestring, integer n);Parameters
| Parameter | Type | Description |
|---|---|---|
aLinestring | geometry | The LineString or circular string to query. |
n | integer | The 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
aLinestringis not a LineString or circular string (for example, a Polygon or MultiLineString).Negative indexing: Negative values count backward from the end of the LineString.
-1returns 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)该文章对您有帮助吗?