The ST_Length() function

The ST_Length() function returns the length of an ST_LineString or ST_MultiLineString.

Syntax

ST_Length(ln1 ST_LineString)
ST_Length(ln1 ST_LineString, linear_uom varchar(128))

ST_Length(mln1 ST_MultiLineString)
ST_Length(mln1 ST_MultiLineString, linear_uom varchar(128))
ST_Length(ln1 ST_LineString)
ST_Length(mln1 ST_MultiLineString)

The linear_uom parameter converts the result to the specified unit of measure. To calculate the length if the line is in a geographic coordinate system where the coordinates are in an angular unit of measure, you must specify a linear unit of measure with the linear_uom parameter. Angular units of measure are converted to linear units of measure by great-circle calculations. If the line is in a projected coordinate system that has a unit of measure that is different from the unit of measure that is specified by the linear_uom parameter, then the returned value is converted to the unit of measure that is specified by the linear_uom parameter. The linear_uom parameter must be the name of a linear unit of measure from the unit_name column of the st_units_of_measure table.

Return type

DOUBLE PRECISION

Example: Find the length of steams and rivers

A local ecologist who is studying the migratory patterns of the salmon population in the county's waterways wants the length of all stream and river systems within the county.

The waterways table is created with the ID and name columns that identify each stream and river system that is stored in the table. The water column is a multilinestring, because the river and stream systems are often an aggregate of several linestrings:
CREATE TABLE waterways (id     integer,
                        name   varchar(128),
                        water  ST_MultiLineString);
The query returns the name of each system along with the length of the system that is generated by the length function:
SELECT name, ST_Length(water) Length
   FROM waterways;


name    Fedders creek
length  175853.9869703
The following figure shows the river and stream systems that lie within the county boundary.
Figure 1: Stream and river systems

This graphic is described in the surrounding text.

Examples: Find the length lines in meters

The following statement returns the length of a linestring in meters:

EXECUTE FUNCTION round(
        st_length(
                '32608 linestring(576100 15230, 576102 15230)'::st_linestring,
                'meter'),
        2);

    (expression)

2.00000000000000

1 row(s) retrieved.

The following statement returns the length of a multilinestring in meters:

EXECUTE FUNCTION round(
        st_length(
                '32608 multilinestring((576100 15230, 576100 15232, 
                 576102 15232, 576102 15230, 576100 15230),(576104 4, 
                 576104 6, 576106 6, 576106 4, 576104 4))'::st_multilinestring,
                'meter'),
        2);

    (expression)

16.0000000000000

1 row(s) retrieved.

Examples: Find the length of a line that has angular coordinates

These examples are based on the angular coordinate system WGS 84, which has SRID 4326. They calculate the length of a line between New York and Los Angeles, which is based on the following coordinates:

  • Latitude and longitude of New York: 40.67000 N, 73.94000 W
  • Latitude and longitude of Los Angles: 34.05000 N, 118.25000 W

The following statement returns the length between New York and Los Angeles in US miles:

EXECUTE FUNCTION ST_Length('4326 linestring(-73.94000 40.67000, 
                  -118.25000 34.05000)'::st_linestring, 'mile_us');

  (expression)

2454.991002988

1 row(s) retrieved.

The following statement returns the length between New York and Los Angeles in kilometers:

EXECUTE FUNCTION ST_Length('4326 linestring(-73.94000 40.67000, 
                  -118.25000 34.05000)'::st_linestring, 'kilometer');

  (expression)

3950.932942578

1 row(s) retrieved.