ST_LengthSpheroid
What is ST_LengthSpheroid?
ST_LengthSpheroid returns the length in metres of a longitude/latitude linear geometry, computed on a user-specified reference spheroid. It is the high-accuracy counterpart to ST_Length for lon/lat geometry input.
ST_LengthSpheroid(geometry a_geometry, spheroid a_spheroid) → floatThe spheroid argument is a literal such as 'SPHEROID["WGS 84",6378137,298.257223563]'. If the input geometry has Z values, those are included — the function returns the 3D spheroidal length.
When would you use ST_LengthSpheroid?
Use ST_LengthSpheroid when you have lon/lat geometry and need a sub-millimetre-accurate metric length without casting to geography. Typical uses include geodetic surveys, maritime and aviation path calculations, and scientific datasets where the exact spheroid matters. For everyday use, casting to geography and calling ST_Length is simpler and supports index-accelerated proximity queries.
1SELECT ST_LengthSpheroid(
2 geom,
3 'SPHEROID["WGS 84",6378137,298.257223563]'
4) AS metres
5FROM pipelines
6WHERE id = 42;FAQs
What units does it return?
Always metres (assuming the spheroid axes are in metres, as they are for all standard spheroids).
How does it differ from ST_Length(geography)?
Functionally similar for WGS84 input. ST_Length(geography) reads the spheroid implicitly from the geography's fixed WGS84 reference, while ST_LengthSpheroid lets you specify any spheroid — useful for non-WGS84 datums.
Does it handle Z coordinates?
Yes. If the geometry has Z values, the length is the sum of spheroidal-horizontal plus Z contributions per segment — effectively a 3D spheroidal length.
Which spheroid should I use?
Match the datum of your data. For GPS / WGS84 data use 'SPHEROID["WGS 84",6378137,298.257223563]'. For legacy US data on NAD27, use a Clarke 1866 spheroid. Mismatched spheroids introduce small but systematic errors.