The ST_PointN() function

The ST_PointN() function takes an ST_LineString and an INTEGER index and returns a point that is the nth vertex in the ST_LineString's path. (The numbering of the vertices in the linestring starts with 1.)

Syntax

ST_PointN (ln1 ST_LineString, index integer)

Return type

ST_Point

Example

The pointn_test table is created with the gid column, which uniquely identifies each row, and the ln1 ST_LineString column:
CREATE TABLE pointn_test (gid integer,
                          ln1 ST_LineString);
The following INSERT statements insert two linestring values. The first linestring does not have Z coordinates or measures, while the second linestring has both:
INSERT INTO pointn_test VALUES( 
   1, 
   ST_LineFromText('linestring (10.02 20.01,23.73 21.92,30.10 40.23)',1000) 
);

INSERT INTO pointn_test VALUES( 
   2, 
   ST_LineFromText('linestring zm (10.02 20.01 5.0 7.0,23.73 21.92
6.5 7.1,30.10 40.23 6.9 7.2)',1000) 
);
The query lists the gid column and the second vertex of each linestring. The first row results in an ST_Point without a Z coordinate or measure, while the second row results in an ST_Point with a Z coordinate and a measure. The ST_PointN() function will also include a Z coordinate or measure value if they exist in the source linestring:
SELECT gid, ST_PointN(ln1,2) the_2nd_vertex 
   FROM pointn_test;


gid             1
the_2nd_vertex  1000 POINT (23.73 21.92) 

gid             2
the_2nd_vertex  1000 POINT ZM (23.73 21.92 6.5 7.1)