ST_LineStringN function

The ST_LineStringN function takes a multilinestring and an index as input parameters and returns the linestring that is identified by the index. The resulting linestring is represented in the spatial reference system of the specified multilinestring.

If the specified multilinestring is null or is empty, or if the index is smaller than 1 or larger than the number of linestrings, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramST_LineStringN(multi_linestring, index)

Parameter

multi_linestring
A value of type ST_MultiLineString that represents the multilinestring from which the linestring that is identified by index is returned.
index
A value of type INTEGER that identifies the nth linestring, which is to be returned from multi_linestring.

If index is smaller than 1 or larger than the number of linestrings in multi_linestring, then null is returned and a warning condition is returned (SQLSTATE 01HS0).

Return type

ST_LineString

Example

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

The SELECT statement illustrates how to choose the second geometry inside a multilinestring in the SAMPLE_MLINES table.

CREATE TABLE sample_mlines (id INTEGER, 
  geometry ST_MULTILINESTRING)

INSERT INTO sample_mlines(id, geometry)
VALUES
    (1110, ST_MultiLineString('multilinestring
                             ((33 2, 34 3, 35 6),
                             (28 4, 29 5, 31 8, 43 12),
                             (39 3, 37 4, 36 7))', 1) ),
    (1111, ST_MLineFromText('multilinestring(
                           (61 2, 64 3, 65 6),
                           (58 4, 59 5, 61 8),
                           (69 3, 67 4, 66 7, 68 9))', 1) )


SELECT id, cast(ST_AsText(ST_LineStringN(geometry, 2))
  AS varchar(110)) AS second_linestring
FROM   sample_mlines

Results:

ID    SECOND_LINESTRING
----- -----------------------------------------------------------------------       
1110  LINESTRING (28.000000 4.000000, 29.000000 5.000000, 31.000000 8.000000, 
        43.000000 12.000000)
        
1111  LINESTRING (58.000000 4.000000, 59.000000 5.000000, 61.000000 8.000000)