PostGISLinear Referencing

ST_LocateBetweenElevations

What is ST_LocateBetweenElevations?

ST_LocateBetweenElevations is a PostGIS function that returns the portions of a 3D geometry whose Z values lie between two specified elevations. It is the Z-axis analogue of ST_LocateBetween.

SQL
ST_LocateBetweenElevations(geometry geom, float elevation_start, float elevation_end)geometry

When would you use ST_LocateBetweenElevations?

Use this function whenever you need to slice a 3D feature vertically — the portion of a pipeline between the surface and 5 m below, the part of a flight path between 1000 ft and 2000 ft, or the section of a tunnel below sea level. It is invaluable for profile extraction and for analyzing vertical compliance with elevation constraints.

SQL
1SELECT ST_LocateBetweenElevations(geom, 0, -5) AS shallow_section
2FROM pipelines
3WHERE id = 3;

FAQs

Does the input have to be 3D?

Yes. The geometry must carry Z values (LINESTRING Z, MULTILINESTRING Z, POINT Z, or similar). 2D geometries have no meaningful elevation to compare.

What if only part of the line crosses the elevation band?

The function returns just that portion, with the endpoints interpolated to lie exactly at the boundary elevations where necessary. This is the expected semantics for elevation-bounded slicing.

How is it different from filtering with ST_ZMin/ST_ZMax?

ST_ZMin/ST_ZMax return scalar bounds; they tell you whether a geometry has Z within a range but don't extract the portion. ST_LocateBetweenElevations returns the actual sub-geometry that satisfies the elevation constraint.

Does it work on polygons?

It is primarily defined for points and linestrings (and multi-versions of those). For polygons and polyhedral surfaces the function may return an empty result or behave unpredictably — clip with a 3D plane via SFCGAL-based functions for those cases.