[GIS] clip linestring by length

clipdistancelinestringpostgis

Is there a way to clip a linestring based on distance?
For example, linestring from a to b is 150 meters. I would like to get the linestring from a to a point that is is 75 meters away from a following the a-b linestring.

What I have thought until now is to get the line NpointCount and start adding together small line segments until I get close to 75 meters, but still, how am I supposed to add the final line segment?

I hope I am making sense here..

thanks in advance!

Best Answer

In PostGIS it's a two-stage process. First you need to find the overall length of your line geometry with ST_length()

Say that produces a value of 150. Then you need to divide your desired length, 75, by the total length, giving you 0.5. Finally, with that value call ST_Line_Interpolate_Point() or ST_Line_Substring() to return the substring rather than a point.