0

I need to merge adjacent lines in QGIS 3.8, but am having trouble coming up with a way to identify and then merge them in a large data set. (3.8 is as far as my Mac can support)

I tried to convert my lines to polygons and use the "Join multiple lines" plugin. With both of these it seems like I'll need to select everything manually (not a feasible option) first or else it gets messy.

Ex. I want to locate lines like on the right here with touching endpoints, and merge them into one like on the left.

enter image description here

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
  • 3
    Convert your line ends into points https://gis.stackexchange.com/questions/306190/generate-start-and-end-points-in-qgis-for-linestrings, buffer ends with dissolve, convert multipart to single https://gis.stackexchange.com/questions/74413/how-to-convert-result-of-dissolved-buffer-into-individual-polygons (last answer) then count the points in the buffer http://jonathansoma.com/lede/foundations-2018/qgis/points-in-polygon/ - remove any with count != 2 can't be merged as they are either dangles or true intersections. Iterate the remaining polygons, select intersecting lines then dissolve them. – Michael Stimson Mar 11 '20 at 01:51
  • I had a similar problem, and just used (1) Dissolve and (2) Multipart to Single Part to resolve the merges. Ended up being a lot simpler than some of the other proposed solutions, but perhaps it doesn't work on all datasets? See also https://gis.stackexchange.com/questions/376050/merging-touching-lines-in-qgis – Tom Brennan Apr 04 '21 at 10:42

1 Answers1

1

One solution would be to union and merge touching lines in a self join SQL, then run a multipart to singlepart geometry with the QGIS geometry tools.

Make a virtual layer - where mylines is your layername - like

select st_linemerge(st_union(m1.geometry)) as geometry
from mylines m1
inner join mylines m2 on st_touches(m1.geometry, m2.geometry)

Then on the virtual layer run Vector > Geometry tools > Multipart to Singleparts

Anyone knows how to include the Multipart to Singleparts in SQL - I am happy to hear ..

Jakob
  • 7,471
  • 1
  • 23
  • 41
  • You can use recursive functions to try and emulate ST_Dump() (the equivalent function in PostGIS). See examples in these answers: https://gis.stackexchange.com/a/341570/98784 and Chainage points using virtual layer – she_weeds Apr 14 '20 at 02:25
  • Here is an example query: WITH RECURSIVE rng(x) AS( SELECT 1 UNION ALL SELECT (x+1) FROM rng LIMIT ( SELECT cast(st_numgeometries(geometry) AS int) FROM line_collected)), line_collected AS (SELECT st_linemerge(st_union(m1.geometry)) as geometry, m1.id FROM mylines m1 INNER JOIN mylines m2 ON st_touches(m1.geometry, m2.geometry)) SELECT geometry_n(lc.geometry, rng.x) AS geometry, rng.x AS part_num FROM rng, line_collected lc; – she_weeds Apr 14 '20 at 02:28