12

I have lots of different lines composed of hundreds of segments in a Shapefile.

I would like to join them spatially where every start (or end) point of a segment correspond exactly (or within a range of X mm, but this extra) to the start (or end) point of an other segment. And only there. In other word everywhere a segment touch an other at his beginning or ending, they should be joined. (Attributes are always the same except for one column with specifics IDs I don't care about.)

The dissolve tool doesn't work; it compose a unique feature containing absolutely all lines, even those which are far away. All the segments are entire features, so the "multi to single part" vector tool doesn't work either. I have also tried the "Join Lines" and "Join multiples lines" plugins; they both don't do the job. Except the "Join Lines"; it is doing it correctly, but I have to select manually two segments then click to join them. It's not possible to do so manually of hundreds of them.

It would be nice if this can be done on a selection as there is several types (rivers, roads, paths, etc) of lines in this Shapefile.

3
  • 3
    Have you tried MergeLines?
    – ArMoraer
    Jul 21, 2016 at 17:38
  • 1
    It's said it's a simplification tool. I do not need a simplification tool, the geometry must be the same in the ouput. And there is no notion of network on my data.<br/> Anyway, as I do not have other solutions yet, I gave it a try; it was really really long (~20min) to process data (6.7Mo single geojson file, or ~4Mo shape files). And there is no option to do so on lines of the same type, e.g. on a specific attribute, or on a selection. Finally, at the end of the computation, the window stay here with the 100% bar full and the shape created has some lines merged, others not, it seems random. Jul 21, 2016 at 22:02
  • This can be accomplished easily in R, follow accepted answer in this post stackoverflow.com/questions/9751619/…
    – Kartograaf
    Oct 13, 2020 at 18:07

1 Answer 1

2

The problem can be solved in several simple steps - 5 in total as you can see below. The core of the solution conists in using the Join attributes by nearest tool.

Let's say you have a the following line segments with white dots representing start- and end points and the number the id of the line segments. Some segments are connected (end point of 1 = start point of 2), other not, there are gaps in between, like between segment 2 and 3. In the following screenshots, you have 19 line segments that can be grouped to 8 groups of connected lines as follows:

  1. 1,2
  2. 3,4,5
  3. 6
  4. 7
  5. 8,9,10,11,12
  6. 13
  7. 14,15,16,17,18
  8. 19

You see this here on the screenshots: there are totally 8 gaps: enter image description here

To join the segmentes that are connected to one single line, proceed as follows:

  1. Select Menu Processing / Toolbox / Join attributes by nearest, set both input layers to your line layer and Maximum nearest neighbors to 1. You could also define a Maximum distance (to get your extra). Run the tool.
  2. You get a new layer. If you have a look at the attribute table, you see that there are several new fields, between else also a field named distance. This tells you the distance between neighboring segments: if it is 0, the two segments are connected. Those lines that have a value > 0 are no. 6, 7, 13, 19: exactly those we identified above as not being part of a group of connected line segments. To get your extra condition, you could define a distance threshold: if the distance is smaller, you could consider the lines as "connected".

enter image description here

  1. Now click the select by expression symbol in the toolbar (see screenshot above) and paste the expression "distance" = 0 to select all segments that have a distance of 0 to their nearest neighbors - thus segments that are connected.
  2. Toggle edit mode and click the Merge Selected Features symbol in the toolbar: enter image description here
  3. You now get a line with all segments (expect 6,7,13 and 19) merged to one single line. We're close to where you want to go, but not yet there.
  4. You're probably not able to save the merged lines as we have a multipart line: a single line-feature that consists of several segments that are not connected. We have to apply Menu Vector / Geometry Tools / Multipart to singleparts. At the same time, this also solves our last problem: the one large line gets split into several lines that are connected: Now, we have exactly the merged lines that we defined at the beginning. You can see this in the next screenshot - red dots are start- and end points of the new line, the number of the line labels represent the totally 8 new features (lines). As you see, the do not follow the original order any more, but that should not be a problem.

enter image description here

3
  • Thanks @Babel, however I have a problem in the step 2 in the case of lines crossing eachother without intersecting (in my case a sewer network with two pipes crossing but laid at two different depth). The distance is = 0 when it should not. Do you think there is an additional trick to solve this issue? Mar 12, 2021 at 19:44
  • Can you provide sample data? Otherwise, it's difficult to imagine
    – Babel
    Mar 12, 2021 at 21:32
  • Here is an example. The line in purple has it end vertex in the red circle. The blue circle is just an overlap. Therefore the result in step 2 should be a distance of zero at the red circle but should not have distance (or a high distance) at the blue circle [![line intersection ][1]][1] When I use your method, at step 2 I have distance=0 at both intersection. I hope I am a bit clearer than in my first message. [1]: i.stack.imgur.com/zqCLh.jpg Mar 12, 2021 at 22:07

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.