1

I want to split a line in segments of a fixed length (e.g. 10m) like indicated in the picture. Using the approach of st_segmentize how-to-buffer-a-line-with-segments-in-postgis-using-unique-segmentsize works partially fine, but counts each inflection point as end/beginning of a line, which lead to unequal segment sizes. Using the GRASS function v.split in QGIS results in the right output v.split, so I want to know if there is a similar function/ approach in PostGIS 2.5.1.

enter image description here

  • 1
    Use a sequenced ST_LineSubstring. Packed into a set of PL/PgSQL functions here, readme here. C function addon here (needs rebuild from source). Reading that linked question, @JGHs comment still stands; ST_Segmentize only includes vertices so that none is further apart than the threshold! – geozelot May 18 at 20:11
1

You can use ST_LineSubstring (as suggested by @geozelot in the comment):

-- We split the line into segment using `ST_LineSubstring`
SELECT ST_LineSubstring(geom, n/length,
  CASE
    WHEN (n+dist) < length THEN (n+dist)/length
    ELSE 1
  END) AS the_geom
FROM
  (
  -- Example line of length ~48m
  SELECT 10.0 AS dist,
         ST_Length(ST_GeomFromText('LINESTRING(0 0,0 20,20 40)')) AS length,
         ST_GeomFromText('LINESTRING(0 0,0 20,20 40)') AS geom
  ) AS subQ
-- We cross join with a series.
CROSS JOIN generate_series(0,(floor(length/dist)*dist)::int,dist::int) AS n

This query will split your linestring in segment of 10m (specified by dist).

For each line we generate a serie that goes from 0 to floor(len/10)*10. In this example the line has a length of about 48m, so the serie goes from 0 to 40 with step of 10m. For the last segment of 8m: n+dist = 40+10 = 50, since 50>48 we set the end fraction to 1.

This query will also work if you have more than one line.

| improve this answer | |

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.