ST_LocateBetween
What is ST_LocateBetween?
ST_LocateBetween is a PostGIS function that returns the portions of a measure-parameterised geometry that lie between two M values. The result is a multi-geometry containing one or more sub-lines (or points, where measures are crossed at a single vertex).
ST_LocateBetween(geometry geom_with_measure, float measure_start, float measure_end, float offset = 0) → geometryWhen would you use ST_LocateBetween?
Use ST_LocateBetween to carve out a section of a measure-parameterised route — the portion of a road between kilometer 5 and kilometer 10, the river reach between mileposts 100 and 110, or the segment of a pipeline between two chainage values. This is the standard tool for linear-reference-based geometric queries.
1SELECT ST_LocateBetween(geom, 5000, 10000) AS section_5_10
2FROM roads
3WHERE name = 'A1';FAQs
How does ST_LocateBetween differ from ST_LineSubstring?
ST_LineSubstring uses fractional positions between 0 and 1 (a fraction of total length). ST_LocateBetween uses actual M values stored in the geometry. If your data carries meaningful chainage or kilometer posts, ST_LocateBetween preserves that semantic; ST_LineSubstring ignores M entirely.
What does the offset parameter do?
It displaces the resulting sub-lines perpendicular to the original line by a fixed distance — positive to the left, negative to the right. Helpful for generating parallel features such as shoulders or annotation lanes alongside a highway.
Does the input need to have M?
Yes — the function is only meaningful on measure-parameterised geometries. Add measures to a 2D line with ST_AddMeasure(geom, m_start, m_end) if your data does not already carry them.
What if no portion matches?
The function returns an empty geometry. Filter with NOT ST_IsEmpty(result) to drop empties from downstream queries.