I'm trying to join touching lines within a linestring dataset, so that I end up with a single feature for each set of touching features (eg the set outlined in green below). I've tried pretty much every algorithm I can find, including dissolve, merge, union (in QGIS, including the GDAL and SAGA options), ST_LineMerge in PostGIS, but the lines refuse to join, even though the end nodes of the segments are coincident. Can anyone suggest a process which would do this? I'm running this as part of a processing model in QGIS which has several hundred thousand records, so it needs to be a process I can include in this.

The next part of the process uses 'split with lines' to split the feature into components at line joins, so in the example below I need to end up with three features. Initial four segments - to be joined to one, then split in three at junction

I can manually merge the features into one using the 'merge selected features' tool, but I can't see how to run this as a process, and even so, running 'split with lines' on the result splits the lines as they were initially, resulting in four features not three.

My guess is that there is something about the data which is causing this, but I can't see a way of fixing it, or running the merge.

  • 2
    are the endpoints coincident, or is there a gap? Sep 24, 2020 at 22:29
  • thanks, no gap - it looks like it's to do with line direction as per robin loche answer below
    – Ant Scott
    Sep 25, 2020 at 12:16
  • What does an ST_Union() do... it is just their location that defines them, or is there a common attribute? Sep 25, 2020 at 14:12

1 Answer 1


There could be a gap between the data that you don't see. You should try to use ST_SnapToGrip before trying to merge to be sure.

Also check your geom with ST_AsText to be sure that the end of your geom is the start of the other. If not, that means you may have to detect which way your line is (maybe by testing the ST_Distance between ST_StartPoint and ST_EndPoint) and reverse it (with ST_Reverse) before ST_LineMerge.

  • There are no gaps, line direction seemed to be the issue. However when I started to look at vertices, I realised there were some zero-length (or not quite zero) lines around at the junctions of the lines which were failing to join. Taking these out, then running dissolve in QGIS fixed the issue (regardless of line direction). The extra lines seem to be have been added by an earlier stage in the workflow, but were hard to spot. Many thanks for your help!
    – Ant Scott
    Sep 26, 2020 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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