0

SELECT ST_AsText(ST_Segmentize(ST_GeomFromText('LINESTRING(0 0, 10 0)'), 3)); gives LINESTRING(0 0,3 0,6 0,9 0,10 0) but the expected result is LINESTRING(0 0,2.5 0,5 0,7.5 0,10 0). How can this be done?

See for example the documentation for qgis:densifygeometriesgivenaninterval:

"E.g. specifying a distance 3 would cause the segment [0 0] -> [10 0] to be converted to [0 0] -> [2.5 0] -> [5 0] -> [7.5 0] -> [10 0], since 3 extra nodes are required on the segment and spacing these at 2.5 increments allows them to be evenly spaced over the segment."

  • as per the docs, ST_Segmentize will return the line divided into segments with the spezified max_length parameter; you could dynamically set the max_length parameter, with e.g. (ST_Length(geom) / 4) – ThingumaBob Mar 12 '18 at 6:43
  • But then I would have to calculate and set the max_length parameter for each segment individually, right? That would complicate the code significantly. Is there another function in PostGIS that works like qgis:densifygeometriesgivenaninterval? – odalman Mar 12 '18 at 7:15
  • try ST_Segmentize(geom, ST_Length(geom) / 4) – ThingumaBob Mar 12 '18 at 7:31
  • That does not seem to work for lines with multiple segments because ST_Length(geom) returns the length of the whole polyline. – odalman Mar 12 '18 at 7:40
  • Consider a polyline with 2 segments. For example SELECT ST_AsText(ST_Segmentize(ST_GeomFromText('LINESTRING(0 0,10 0,10 4)'), 3)); returns LINESTRING(0 0,3 0,6 0,9 0,10 0,10 3,10 4) but the expected output (for parity with qgis) would be LINESTRING(0 0,2.5 0,5 0,7.5 0,10 0,10 2,10 4). There is no single expression to pass as max_length instead of 3 to get that. Therefore some other function seems to be needed. – odalman Mar 12 '18 at 8:04

Your Answer

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

Browse other questions tagged or ask your own question.