ST_ToLineString function

The ST_ToLineString function takes a geometry as an input parameter and converts it to a linestring. The resulting linestring is represented in the spatial reference system of the specified geometry.

The specified geometry must be empty or a linestring. If the specified geometry is null, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramST_ToLineString(geometry)

Parameter

geometry
A value of type ST_Geometry or one of its subtypes that represents the geometry that is converted to a linestring.

A geometry can be converted to a linestring if it is empty or a linestring. If the conversion cannot be performed, then an exception condition is raised (SQLSTATE 38SUD).

Return type

ST_LineString

Example

This example illustrates the use of the ST_ToLineString function.

CREATE TABLE sample_geometries (id INTEGER, geometry ST_Geometry)

INSERT INTO sample_geometries
  VALUES (1, ST_Geometry ('linestring z (0 10 1, 0  0 3, 10 0 5)', 0)),
         (2, ST_Geometry ('point empty', 0) ),
         (3, ST_Geometry ('multipolygon empty', 0) )

In the following SELECT statement, the ST_ToLineString function is used to return linestrings converted to ST_LineString from the static type of ST_Geometry.

SELECT CAST( ST_AsText( ST_ToLineString(geometry) ) 
     AS VARCHAR(130) ) LINES
FROM sample_geometries
Results:

LINES
------------------------------------------------------------
LINESTRING Z(0 10 1, 0 0 3, 10 0 5)
LINESTRING EMPTY
LINESTRING EMPTY