Name

ST_LineMerge — Return the lines formed by sewing together a MultiLineString.

Synopsis

geometry ST_LineMerge(geometry amultilinestring);

Description

Returns a LineString or MultiLineString formed by joining together the constituent line work of a MultiLineString. Lines are joined at their endpoints at 2-way intersections. Lines are not joined across intersections of 3-way or greater degree.

[Note]

Only use with MultiLineString/LineStrings. If you pass a Polygon or GeometryCollection into this function, it returns an empty GeometryCollection

Performed by the GEOS module.

Availability: 1.1.0

[Warning]

This function will strip the M dimension.

Examples

SELECT ST_AsText(ST_LineMerge(
'MULTILINESTRING((-29 -27,-30 -29.7,-36 -31,-45 -33),(-45 -33,-46 -32))'
		));
st_astext
--------------------------------------------------------------------------------------------------
LINESTRING(-29 -27,-30 -29.7,-36 -31,-45 -33,-46 -32)

If merging is not possible due to non-touching lines, the original MultiLineString is returned.

SELECT ST_AsText(ST_LineMerge(
'MULTILINESTRING((-29 -27,-30 -29.7,-36 -31,-45 -33),(-45.2 -33.2,-46 -32))'
));
st_astext
----------------
MULTILINESTRING((-45.2 -33.2,-46 -32),(-29 -27,-30 -29.7,-36 -31,-45 -33))

Example showing Z-dimension handling.

SELECT ST_AsText(ST_LineMerge(
      'MULTILINESTRING((-29 -27 11,-30 -29.7 10,-36 -31 5,-45 -33 6), (-29 -27 12,-30 -29.7 5), (-45 -33 1,-46 -32 11))'
        ));
st_astext
--------------------------------------------------------------------------------------------------
LINESTRING Z (-30 -29.7 5,-29 -27 11,-30 -29.7 10,-36 -31 5,-45 -33 1,-46 -32 11)

See Also

ST_Segmentize, ST_LineSubstring