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?


1 Answer 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
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; 

Ref: http://postgis.net/docs/ST_LineSubstring.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.