I have a Shapefile containing segments of Lines/LineStrings: enter image description here Unfortunately the order of the DBF attributed table is random.

How to reorder the segments so entry #1 in the Shapefile will be "Segment 1", and the consecutive entry will be a segment which:

  1. Share same coordinate point geometry (Blue dot example: "Your end is my beginning").


  1. First coordinate point geometry is the closest to the last coordinate of previous segment (Orange & green dots example).

The logic here is that sometimes a physical road LineString explicitly separated into Lines (segments), and sometimes mistakenly a small gap can exist. (actually I think that most of the times condition 2 will met, because if 2 segments share same Geometry point, They also probably will be connected as single LineString)

Note- The question is just how to find the ordering and reorder (The next step will be to form single LineString either by connecting via the share coordinate or by connecting Last->First coordinates of consecutive lines).


  1. With Join Multiple Lines plugin It's possible to merge segments "As Expected" in the above example, including removing duplicate node (blue).

  2. The problem that it merge all other attributes into single one: For example column "ID" so will be only id=1.

  3. Then applying Explode Lines tool, splits the merged line back to segments at nodes with the correct order, however because of what mentioned at point 2 - the data inside the attributes is lost.

  • 1
    have you tried the Join Multiple Lines plugin? That will sort the head-tail ordering to make it consistent. I'm not sure how well it works with gaps, and it can have problems with some things like branches which split off and rejoin. – Steven Kay May 3 '17 at 18:57
  • @StevenKay Thanks I'll check it and write here the results. – michael May 3 '17 at 19:54
  • 1
    @michael your task is interesting, but I think it could be doable only if you have simple situations like the one you attached because you didn't specify any rule for managing overlapping or intersecting line features or even what to do when there is a big network of lines: even if you add some criteria, solving the task would be tricky. Furthermore, it should be probably set a starting point in some way (by editing its attribute or by selecting it before running the code). Can you share a sample layer for trying to do some test? – mgri May 4 '17 at 6:33
  • @mgri Unfortunately I can't share the layer. You absolutely correct regarding intersections, overlapping, start point issue etc. I just mentioned single special case of duplicate/shared node. I'll right now edit my question with what I think is the most relevant rules for special cases. – michael May 4 '17 at 6:49

Your Answer

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

Browse other questions tagged or ask your own question.