ST_3DLength
What is ST_3DLength?
ST_3DLength returns the 3D length of a linear geometry, summing the Euclidean distance of each segment in (x, y, z) space. For 2D input (no Z values), it is equivalent to ST_Length.
ST_3DLength(geometry a_3dlinestring) → floatThe result is expressed in the CRS units of the input — typically metres for projected 3D data. There is no geography overload.
When would you use ST_3DLength?
Use ST_3DLength whenever elevation changes affect the real-world length of a line: slope distance along a hiking trail, the true length of a pipeline across varied terrain, or the three-dimensional run of a cable in a building model. It is especially useful in engineering, surveying, and BIM workflows where material quantities depend on slope length rather than plan length.
1-- Extra cable required beyond plan length
2SELECT id,
3 ST_3DLength(geom) - ST_Length2D(geom) AS extra_metres
4FROM cables;FAQs
What happens if my geometry is 2D?
ST_3DLength treats missing Z values as 0, so the result equals ST_Length. Ensure you are passing truly 3D data to get slope-aware lengths.
What units does it return?
The CRS units of the input. When X, Y are in metres (e.g. UTM) and Z is in metres, the result is metres. Mixing units — for example horizontal degrees and vertical metres — produces nonsense values; reproject to a CRS with consistent units first.
Is there a geography version?
No. For 3D length on lat/lon data with elevation, reproject to a projected CRS in metres (UTM, a national grid, or ST_Transform to a suitable projection) before calling.
Which geometry types are supported?
LINESTRING and MULTILINESTRING. Non-linear types return 0. For polygon boundary length including Z, use ST_3DPerimeter.