Join multiple line segments into one line – GIS

gisqgis

I have a map that I have digitised and converted to a vector layer (the rivers only).
The problem is that the vectorisation has produced a large number of segments for each river, that show up as different features (each may have multiple straight line segments, but they do not cover whole rivers). What I am looking for is a tool to merge into one feature (a polyline, I guess) all the segments whose extremes are within a given distance.
I am using QGis, and the GRASS plugin. I have tried v.clean.snap, v.build.polylines, but did not yet manage to actually merge the lines. Any help would be very appreciated!

Best Solution

You can do it using GEOS library in your programming language and maybe also commandline. In my case, I was doing it in R, using rgeos library:

require(rgdal)
require(rgeos)
lines <- readOGR("f:/dir", "itineraris")
# grouping line features by lines$ITINERARI
lines2 <- gLineMerge(lines, byid = lines$ITINERARI, id = lines$ITINERARI)
writeOGR(lines2, "f:/dir", "itineraris_merged", driver="ESRI Shapefile")

Be careful: from the note in the documentation "specifically it joins line segments with intersecting end points" it seems that the line features must be consecutive - however it is not clear whether this applies also to case when you merge by IDs.

Related Question