I have a multilinestring made up of several pieces, some of which are facing the wrong direction. (Accessible here: http://pastebin.com/89Zcixv5 )

I think what I'm looking for is some utilization of ST_MakeLine, but when I try to use it, the pieces of the trail get put together wrong. (See: http://i5.minus.com/iJ5QVbaA9rm2g.png )

enter image description here

I have a ton of trails in this format that I need to convert from MultiLineStrings to LineStrings containing the whole trail.

If anybody has any ideas, I'd greatly appreciate all the help I can get!


  • What format are those LineString points in your pastebin? Jun 16, 2016 at 0:49

2 Answers 2


Easy, use ST_LineMerge with the MultiLineString geometry.

SELECT ST_LineMerge(geom) FROM ...

Returns a LineString if it is possible, such is the example in the question.



looking from picture. Valid linestring cannot touch itself. So picture data should could create 3+ linestring.

You can use ST_Dump to dump multi in order which it has been created. Then you can try use st_linemerge until you get multiline. One solution is use st_StartPoint and st_endpoint comparison to linestrings and use st_reverse if needed.(end=end || start=start)

  • 1
    A code example would be helpful to show how to accomplish this
    – clhenrick
    May 5, 2017 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.