ST_Length
What is ST_Length?
ST_Length returns the 2D length of a linear geometry (LINESTRING or MULTILINESTRING). For non-linear inputs — points, polygons, collections — it returns 0.
1ST_Length(geometry a_2dlinestring) → float
2ST_Length(geography geog, boolean use_spheroid = true) → floatFor geometry, the result is in the CRS units of the input — degrees for EPSG:4326, metres for projected CRSs such as UTM. For geography, the result is always in metres on the WGS84 spheroid (set use_spheroid = false for faster sphere approximation).
When would you use ST_Length?
Use ST_Length whenever you need the length of a line feature: computing road-segment lengths for maintenance budgets, summing river-reach lengths for a basin report, measuring the length of utility runs, or ranking hiking trails by distance. It is also used in density calculations such as road length per square kilometre, and as a filter to remove slivers or artifacts below a minimum length.
1-- Total road length by municipality in metres
2SELECT m.name, SUM(ST_Length(r.geom::geography)) / 1000 AS km
3FROM roads r
4JOIN municipalities m ON ST_Intersects(r.geom, m.geom)
5GROUP BY m.name;FAQs
What units does ST_Length return?
For geometry the units are those of the SRID — degrees for lat/lon, metres for most projected CRSs. For geography the units are always metres on the WGS84 spheroid. For metre-accurate lengths on lon/lat data, cast to geography: ST_Length(geom::geography).
What happens for non-linear input?
ST_Length returns 0 for points and polygons. If you want the boundary length of a polygon, use ST_Perimeter instead.
Is there a 3D version?
Yes — ST_3DLength uses Z coordinates and returns the full 3D length (useful for slope distance on contours or the true length of a pipe with elevation changes).
Is ST_Length fast?
Yes for geometry — it is a simple sum of segment lengths. For geography, each segment is evaluated with spheroidal math, which is several times slower; use use_spheroid = false for an approximate result, or precompute length_m in a stored column maintained by trigger for hot paths.