[GIS] Sorting line features into route order in attribute table using QGIS

lineqgissorting

I have a several line features stored as shapefiles, obviously constructed of several connecting segment.

I wish to export the attribute tables of each feature as a CSV to use in a separate spreadsheet. However, this requires the comprising segments being ordered in the order which they comprise the line (i.e. the bottom left segment first and the top right segment last). Is there an algorithm or tool I could use to achieve this? Ideally it would assign each segment an ID based on its location in the line feature, incrementing from the start of the line (0) to the end.

I have calculated the start/end points of each line segment but unfortunately cannot simply sort by these coordinates as the lines do not run in a continuous direction.

Expanded information:
My data is a section of a road network stored as a single polyline. This line is comprised contains a number of features, each with information of their length and average travel time (see picture):

enter image description here

I actually have several lines, each one representing a direction of travel across a route in the road network.

I have exported the line features to a CSV so that I can compare the travel times across the routes with those produced with a traffic model. However, in order to compare like-for-like I need each feature to be in order as they form the line. Currently, each feature in the attribute table corresponds to a random section of the route – I wish to sort these features so that they appear in the order which they form the line.

Best Answer

Following the above advice by @StevenKay this small experiment worked for me. Please note this is tested on 1 set of line segments.

Overall this goes like this:

  1. Join line segments into one line by Join Multiple Lines. This sorts the order and direction of segments.
  2. Split the line again into segments. (Explode lines)
  3. Look up the id field of the original line segments (refFunctions)
  4. Join attribute tables, using the id fields.

First, I have this collection of random line segments

enter image description here

By Join Multiple Lines plugin, segments are combined into single line. Most importantly it becomes continuous and orderly (see comments by Steven Kay). At the same time attribute table loses rows except for the first one.

enter image description here

Then I split the line by Explode lines in the processing toolbox.

enter image description here

As I already have refFunctions plugin, just opened the attribute table of Exploded and created a new field "original" by expression geomequals('original_lines', 'id').

enter image description here

Above picture looks exactly same as the original random line segments. Their difference is only the order of segments.

Now I can Join the attribute table of original layer and this sorted layer, to get the final product.

Related Question