ST_LineFromText function

The ST_LineFromText function takes a well-known text (WKT) format or a GeoJSON format of a linestring and, optionally, a spatial reference system identifier as input parameters and returns the corresponding linestring.

ST_LineFromText constructs a geometry from one of the following inputs:
  • A WKT format
  • GeoJSON format

The preferred version for this functionality is ST_LineString.

If the specified WKT or GeoJSON format is null, then null is returned.

For details about the supported formats, see Supported data formats.

Syntax

Read syntax diagramSkip visual syntax diagramST_LineFromText(wktgeojson,srs_id )

Parameters

wkt
A value of type VARCHAR or CLOB(2G) that contains the WKT format of the resulting linestring.
geojson
A value of type VARCHAR or CLOB(2G) that represents the resulting linestring using GeoJSON.
srs_id
A value of type INTEGER that identifies the spatial reference system for the resulting linestring.

If the srs_id parameter is omitted, the spatial reference system with the numeric identifier 4326 is used.

If srs_id does not identify a spatial reference system listed in the catalog view SYSGEO.ST_SPATIAL_REFERENCE_SYSTEMS, then an error is returned (SQLSTATE 38SU1).

Return type

ST_LineString

Examples

Example 1:

In the following example, the lines of results have been reformatted for readability. The spacing in your results will vary according to your online display.

The following code uses the ST_LineFromText function to create and insert a line from a well-known text (WKT) line representation. The rows are inserted into the SAMPLE_LINES table with an ID and a line value in spatial reference system 1 in WKT format.


CREATE TABLE sample_lines(id SMALLINT, geometry ST_LineString)

INSERT INTO sample_lines(id, geometry)
VALUES
    (1110, ST_LineFromText('linestring(850 250, 850 850)', 1) ),
    (1111, ST_LineFromText('linestring empty', 1) )

SELECT id, cast(ST_AsText(geometry) AS varchar(60)) AS linestring
FROM   sample_lines


Results:


ID     LINESTRING
------ ------------------------------------------------------------
  1110 LINESTRING (850.000000 250.000000, 850.000000 850.000000)
  1111 LINESTRING EMPTY


Example 2:

In the following example, the lines of results have been reformatted for readability. The spacing in your results will vary according to your online display.

The following code uses the ST_LineFromText function to create and insert lines from a GeoJSON representation. The rows are inserted into the SAMPLE_LINES table with an ID and a line value in the default spatial reference system 4326 in GeoJSON format.

CREATE TABLE sample_lines(id SMALLINT, geometry ST_LineString)

INSERT INTO sample_lines(id, geometry)
VALUES
    (1110, ST_LineFromText('{ "type": "linestring", "coordinates": [[850, 250], [850, 850]] }') ),
    (1111, ST_LineFromText('{ "type": "linestring", "coordinates": [] }') )

SELECT id, cast(ST_AsText(geometry) AS varchar(75)) AS linestring
FROM   sample_lines
Results:

ID          GEOMETRY
----------- -----------------------------------------------------------------
       1110 LINESTRING (850.000000000 250.000000000, 850.000000000 850.000000000)
       1111 LINESTRING EMPTY