user3779020
user3779020

Reputation: 43

how to get points from linestring postgis

I have Linestring (0 0 , 2 4 , 5 5);

I need to output it in the form :

x (1st cell)   ||    y (2nd cell)

0              ||    0

2              ||    4

5              ||    5 

And the same for polygon. How to do that ?

Upvotes: 3

Views: 4330

Answers (1)

John Powell
John Powell

Reputation: 12581

You can use ST_X and ST_Y in conjunction with ST_PointN to get the x and y of individual points, and use generate_series to create an index into each point of the linestring, eg,

with line as (select ST_GeomFromText('LINESTRING(0 0, 2 4, 5 5)') as geom)
    select ST_X(ST_PointN(geom,num)) as x, 
           ST_Y(ST_PointN(geom,num)) as y 
    from line,
(select generate_series(1, (select ST_NumPoints(geom) from line)) as num) 
    as series;

Upvotes: 4

Related Questions