ST_LENGTH

Compute the total planar length of a line or perimeter of a polygon in the units of the coordinate system.

Category: geospatialReturns: DOUBLEDialect: Standard

Syntax

ST_LENGTH(geometry)

Description

## Overview Returns the planar length of a geometry in the units of its coordinate reference system. For a LINESTRING this is the sum of the Euclidean distances between consecutive vertices. For a MULTILINESTRING it is the sum of the lengths of all component lines. For a POLYGON it is the perimeter (sum of boundary edge lengths); for a MULTIPOLYGON it is the total perimeter across all component polygons. POINT and empty geometries return 0. The computation is purely two-dimensional and cartesian. It does not project the input or apply Earth curvature. For raw WGS 84 longitude/latitude data the result is in degrees and is rarely useful as a physical length; project to a metric CRS first to get meters. ## When to use ST_LENGTH vs. alternatives - **ST_LENGTH** (this function): planar length or perimeter in CRS units. Use on projected LINESTRING, MULTILINESTRING, POLYGON, or MULTIPOLYGON data. - **ST_DISTANCE_HAVERSINE**: straight-line great-circle distance between two points in meters. Use for endpoint-to-endpoint distance on raw WGS 84 data. - **ST_AREA**: two-dimensional surface extent of polygons; pair with ST_LENGTH to compute compactness ratios. ## Behavior - Returns a DOUBLE in CRS units. If the CRS uses meters, the length is in meters. - LINESTRING length is the sum of segment lengths between consecutive vertices. - MULTILINESTRING length is the sum of its component line lengths. - POLYGON length is the perimeter (exterior ring plus any interior rings). - MULTIPOLYGON length is the sum of perimeters of each component polygon. - POINT and empty geometries return 0. - Returns NULL if the input geometry is NULL. - Does not track or validate SRID. - Does not apply Earth curvature. Lengths on raw WGS 84 coordinates are in degrees, not meters. ## Input format - Accepts any GEOMETRY built with ST_GEOM_FROM_TEXT, ST_MAKE_POINT, or other ST_ constructors. - Vertex direction does not matter for the final sum; reversing a line produces the same length. - Self-intersecting lines are counted as-is; the function does not simplify or deduplicate overlapping segments. ## Compatibility - Matches the OGC Simple Features definition of planar length; polygon perimeter is computed consistently with ST_LENGTH of the boundary as a linestring. - Result units always mirror the coordinate units of the input; no SRID-aware conversion is performed.

Parameters

NameTypeDescription
geometrySpecifies the input geometry whose length to compute. LINESTRING and MULTILINESTRING values return the sum of segment lengths; POLYGON and MULTIPOLYGON values return the perimeter; POINT and empty geometries return 0.

Examples

-- A 3-4-5 triangle hypotenuse has length 5 in CRS units.
SELECT ST_LENGTH(ST_GEOM_FROM_TEXT('LINESTRING(0 0, 3 4)')) AS len;
-- 10x10 square has perimeter 40 in CRS units.
SELECT ST_LENGTH(ST_GEOM_FROM_TEXT('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))')) AS perimeter;
-- Sum of the segment lengths between consecutive waypoints.
SELECT ST_LENGTH(ST_GEOM_FROM_TEXT(
  'LINESTRING(-0.1278 51.5074, 2.3522 48.8566, 13.4050 52.5200)'
)) AS route_length;
-- A POINT has no extent.
SELECT ST_LENGTH(ST_MAKE_POINT(1, 2)) AS len;
-- Sort road segments from longest to shortest in the CRS units.
SELECT road_id, ST_LENGTH(geom) AS length
FROM gis.catalog.roads
ORDER BY length DESC
LIMIT 10;
-- Aggregate the length of every segment in a network.
SELECT SUM(ST_LENGTH(geom)) AS total_length
FROM gis.catalog.roads;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →