Documentation Home
MySQL 9.5 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 41.4Mb
PDF (A4) - 41.5Mb
Man Pages (TGZ) - 272.3Kb
Man Pages (Zip) - 378.2Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


MySQL 9.5 Reference Manual  / ...  / Functions and Operators  / Spatial Analysis Functions  / Geometry Property Functions  /  LineString and MultiLineString Property Functions

14.16.7.3 LineString and MultiLineString Property Functions

ALineString consists ofPoint values. You can extract particular points of aLineString, count the number of points that it contains, or obtain its length.

Some functions in this section also work forMultiLineString values.

Unless otherwise specified, functions in this section handle their geometry arguments as follows:

  • If any argument isNULL or any geometry argument is an empty geometry, the return value isNULL.

  • If any geometry argument is not a syntactically well-formed geometry, anER_GIS_INVALID_DATA error occurs.

  • If any geometry argument is a syntactically well-formed geometry in an undefined spatial reference system (SRS), anER_SRS_NOT_FOUND error occurs.

  • Otherwise, the return value is non-NULL.

These functions are available for obtaining linestring properties:

  • ST_EndPoint(ls)

    Returns thePoint that is the endpoint of theLineString valuels.

    ST_EndPoint() handles its arguments as described in the introduction to this section.

    mysql> SET @ls = 'LineString(1 1,2 2,3 3)';mysql> SELECT ST_AsText(ST_EndPoint(ST_GeomFromText(@ls)));+----------------------------------------------+| ST_AsText(ST_EndPoint(ST_GeomFromText(@ls))) |+----------------------------------------------+| POINT(3 3)                                   |+----------------------------------------------+
  • ST_IsClosed(ls)

    For aLineString valuels,ST_IsClosed() returns 1 ifls is closed (that is, itsST_StartPoint() andST_EndPoint() values are the same).

    For aMultiLineString valuels,ST_IsClosed() returns 1 ifls is closed (that is, theST_StartPoint() andST_EndPoint() values are the same for eachLineString inls).

    ST_IsClosed() returns 0 ifls is not closed, andNULL ifls isNULL.

    ST_IsClosed() handles its arguments as described in the introduction to this section, with this exception:

    mysql> SET @ls1 = 'LineString(1 1,2 2,3 3,2 2)';mysql> SET @ls2 = 'LineString(1 1,2 2,3 3,1 1)';mysql> SELECT ST_IsClosed(ST_GeomFromText(@ls1));+------------------------------------+| ST_IsClosed(ST_GeomFromText(@ls1)) |+------------------------------------+|                                  0 |+------------------------------------+mysql> SELECT ST_IsClosed(ST_GeomFromText(@ls2));+------------------------------------+| ST_IsClosed(ST_GeomFromText(@ls2)) |+------------------------------------+|                                  1 |+------------------------------------+mysql> SET @ls3 = 'MultiLineString((1 1,2 2,3 3),(4 4,5 5))';mysql> SELECT ST_IsClosed(ST_GeomFromText(@ls3));+------------------------------------+| ST_IsClosed(ST_GeomFromText(@ls3)) |+------------------------------------+|                                  0 |+------------------------------------+
  • ST_Length(ls [,unit])

    Returns a double-precision number indicating the length of theLineString orMultiLineString valuels in its associated spatial reference system. The length of aMultiLineString value is equal to the sum of the lengths of its elements.

    ST_Length() computes a result as follows:

    • If the geometry is a validLineString in a Cartesian SRS, the return value is the Cartesian length of the geometry.

    • If the geometry is a validMultiLineString in a Cartesian SRS, the return value is the sum of the Cartesian lengths of its elements.

    • If the geometry is a validLineString in a geographic SRS, the return value is the geodetic length of the geometry in that SRS, in meters.

    • If the geometry is a validMultiLineString 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:

    • If the geometry is not aLineString orMultiLineString, the return value isNULL.

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

    • If the length computation result is+inf, anER_DATA_OUT_OF_RANGE error occurs.

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

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

    ST_Length() permits an optionalunit argument that specifies the linear unit for the returned length value. These rules apply:

    • If a unit is specified but not supported by MySQL, anER_UNIT_NOT_FOUND error occurs.

    • If a supported linear unit is specified and the SRID is 0, anER_GEOMETRY_IN_UNKNOWN_LENGTH_UNIT error occurs.

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

    • 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 theINFORMATION_SCHEMAST_UNITS_OF_MEASURE table. SeeSection 28.3.43, “The INFORMATION_SCHEMA ST_UNITS_OF_MEASURE Table”.

    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 |+------------------------+
  • ST_NumPoints(ls)

    Returns the number ofPoint objects in theLineString valuels.

    ST_NumPoints() handles its arguments as described in the introduction to this section.

    mysql> SET @ls = 'LineString(1 1,2 2,3 3)';mysql> SELECT ST_NumPoints(ST_GeomFromText(@ls));+------------------------------------+| ST_NumPoints(ST_GeomFromText(@ls)) |+------------------------------------+|                                  3 |+------------------------------------+
  • ST_PointN(ls,N)

    Returns theN-thPoint in theLinestring valuels. Points are numbered beginning with 1.

    ST_PointN() handles its arguments as described in the introduction to this section.

    mysql> SET @ls = 'LineString(1 1,2 2,3 3)';mysql> SELECT ST_AsText(ST_PointN(ST_GeomFromText(@ls),2));+----------------------------------------------+| ST_AsText(ST_PointN(ST_GeomFromText(@ls),2)) |+----------------------------------------------+| POINT(2 2)                                   |+----------------------------------------------+
  • ST_StartPoint(ls)

    Returns thePoint that is the start point of theLineString valuels.

    ST_StartPoint() handles its arguments as described in the introduction to this section.

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