Skip to content
Advertisement

How to split linestrings into their individual segments?

I am new to PostGIS. I have a set of (thousands) of Linestrings and associated multiple points along the line. I want to divide each line into a set of line segments consisting of only 2 points. I find a few answers, But none of the answers I want. for example https://gis.stackexchange.com/questions/21648/explode-multilinestring-into-individual-segments-in-postgis-1-5

My question:

My question picture

Advertisement

Answer

Just dump the points of your large LineString and create small ones using ST_MakeLine with a single point and the next one in the sequence using the window function LEAD(). Then finally create a MultiLinestring with the small LineStrings using ST_Collect:

Demo: db<>fiddle

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement