PostGISLinear Referencing

ST_LineSubstring

What is ST_LineSubstring?

ST_LineSubstring is a PostGIS function that returns the portion of a linestring between two fractional positions. It is the linear-referencing equivalent of string slicing.

SQL
1ST_LineSubstring(geometry line, float startfraction, float endfraction)geometry
2ST_LineSubstring(geography gline, float startfraction, float endfraction) → geography

Both fractions are between 0 and 1 and the start must be less than or equal to the end.

When would you use ST_LineSubstring?

Use ST_LineSubstring to extract a segment of a route between two milestones — the middle third of a river, the section of a road between two junctions, or the detour portion of a trail. It is also the building block for splitting long routes into chunks for per-segment analysis.

SQL
1SELECT ST_LineSubstring(geom, 0.25, 0.75) AS middle_half
2FROM routes
3WHERE id = 1;

FAQs

What happens when start equals end?

The function returns a zero-length point-on-line — effectively the same as ST_LineInterpolatePoint(line, start). For defensive code, pass distinct fractions unless you really want a point.

Does ST_LineSubstring preserve M values?

Yes. Both the interior vertices and the newly interpolated endpoints carry interpolated M, which is crucial when slicing measure-parameterised routes.

Can I use it on MultiLineString inputs?

No — the input must be a single LINESTRING. For multi-part inputs, merge with ST_LineMerge or iterate with ST_Dump and substring each part individually.

How do I split a line into N equal parts?

Generate fractions with generate_series, then call ST_LineSubstring for each adjacent pair. For example, SELECT ST_LineSubstring(geom, i/10.0, (i+1)/10.0) FROM generate_series(0, 9) i slices a line into ten equal pieces.