MySQL tutorial: ST_LENGTH [EN]
top of page
CerebroSQL

MySQL: 

ST_LENGTH

ST_Length(ls [, unit])

Returns a double-precision number indicating the length of the
LineString or MultiLineString value ls in its associated spatial
reference system. The length of a MultiLineString value is equal to the
sum of the lengths of its elements.

ST_Length() computes a result as follows:

o If the geometry is a valid LineString in a Cartesian SRS, the return
value is the Cartesian length of the geometry.

o If the geometry is a valid MultiLineString in a Cartesian SRS, the
return value is the sum of the Cartesian lengths of its elements.

o If the geometry is a valid LineString in a geographic SRS, the return
value is the geodetic length of the geometry in that SRS, in meters.

o If the geometry is a valid MultiLineString in a geographic SRS, the
return value is the sum of the geodetic lengths of its elements in
that SRS, in meters.

ST_Length() handles its arguments as described in the introduction to
this section, with these exceptions:

o If the geometry is not a LineString or MultiLineString, the return
value is NULL.

o If the geometry is geometrically invalid, either the result is an
undefined length (that is, it can be any number), or an error occurs.

o If the length computation result is +inf, an ER_DATA_OUT_OF_RANGE
(https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference
.html#error_er_data_out_of_range) error occurs.

o If the geometry has a geographic SRS with a longitude or latitude
that is out of range, an error occurs:

o If a longitude argument is not in the range (?180, 180], an
ER_GEOMETRY_PARAM_LONGITUDE_OUT_OF_RANGE
(https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-referen
ce.html#error_er_geometry_param_longitude_out_of_range) error
occurs (ER_LONGITUDE_OUT_OF_RANGE
(https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-referen
ce.html#error_er_longitude_out_of_range) prior to MySQL 8.0.12).

o If a latitude argument is not in the range [?90, 90], an
ER_GEOMETRY_PARAM_LATITUDE_OUT_OF_RANGE
(https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-referen
ce.html#error_er_geometry_param_latitude_out_of_range) error occurs
(ER_LATITUDE_OUT_OF_RANGE
(https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-referen
ce.html#error_er_latitude_out_of_range) prior to MySQL 8.0.12).

Ranges shown are in degrees. The exact range limits deviate slightly
due to floating-point arithmetic.

As of MySQL 8.0.16, ST_Length() permits an optional unit argument that
specifies the linear unit for the returned length value. These rules
apply:

o If a unit is specified but not supported by MySQL, an
ER_UNIT_NOT_FOUND
(https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference
.html#error_er_unit_not_found) error occurs.

o If a supported linear unit is specified and the SRID is 0, an
ER_GEOMETRY_IN_UNKNOWN_LENGTH_UNIT
(https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference
.html#error_er_geometry_in_unknown_length_unit) error occurs.

o If a supported linear unit is specified and the SRID is not 0, the
result is in that unit.

o If a unit is not specified, the result is in the unit of the SRS of
the geometries, whether Cartesian or geographic. Currently, all MySQL
SRSs are expressed in meters.

A unit is supported if it is found in the INFORMATION_SCHEMA
ST_UNITS_OF_MEASURE table. See
https://dev.mysql.com/doc/refman/8.0/en/information-schema-st-units-of-
measure-table.html.

URL: https://dev.mysql.com/doc/refman/8.0/en/gis-linestring-property-functions.html

Example

mysql> SET @ls = ST_GeomFromText('LineString(1 1,2 2,3 3)');
mysql> SELECT ST_Length(@ls);
+--------------------+
| ST_Length(@ls) |
+--------------------+
| 2.8284271247461903 |
+--------------------+

mysql> SET @mls = ST_GeomFromText('MultiLineString((1 1,2 2,3 3),(4 4,5 5))');
mysql> SELECT ST_Length(@mls);
+-------------------+
| ST_Length(@mls) |
+-------------------+
| 4.242640687119286 |
+-------------------+

mysql> SET @ls = ST_GeomFromText('LineString(1 1,2 2,3 3)', 4326);
mysql> SELECT ST_Length(@ls);
+-------------------+
| ST_Length(@ls) |
+-------------------+
| 313701.9623204328 |
+-------------------+
mysql> SELECT ST_Length(@ls, 'metre');
+-------------------------+
| ST_Length(@ls, 'metre') |
+-------------------------+
| 313701.9623204328 |
+-------------------------+
mysql> SELECT ST_Length(@ls, 'foot');
+------------------------+
| ST_Length(@ls, 'foot') |
+------------------------+
| 1029205.9131247795 |
+------------------------+

bottom of page