I'm looking to split a linestring every x meters. I don't know which way to start, and I'm going in circles! I can find a point at 20 % from the start with ST_LineInterpolatePoint, but not at 20 m. The idea is to determine the set of points at 20, 40, 60 m and use ST_LineSubstring then. Does that seem correct to you?
Asked
Active
Viewed 1,352 times
1 Answers
1
This can be achieved using PostGIS query itself. Use ST_LineSubstring and generate_series.
Please use below query:
WITH data(id, geom) AS (VALUES
( 'A', 'LINESTRING( 0 0, 200 0)'::geometry ),
( 'B', 'LINESTRING( 0 100, 350 100)'::geometry ),
( 'C', 'LINESTRING( 0 200, 50 200)'::geometry )
)SELECT id, i,
ST_AsText( ST_LineSubstring( geom, startfrac, LEAST( endfrac, 1 )) ) AS geom FROM (
SELECT id, geom, ST_Length(geom) len, 100 sublen FROM data
) AS d CROSS JOIN LATERAL (
SELECT i, (sublen * i) / len AS startfrac,
(sublen * (i+1)) / len AS endfrac
FROM generate_series(0, floor( len / sublen )::integer ) AS t(i)
-- skip last i if line length is exact multiple of sublen
WHERE (sublen * i) / len <> 1.0
) AS d2;
Sreejith V
- 21
- 5
ST_LineSubstringin this answer: https://gis.stackexchange.com/a/334305/14766 – dr_jts Aug 21 '21 at 23:21