I would like to know if it is possible to sort a list of line features in the exact order in which they appear on the canvas, given the starting feature. In this case the list of linefeatures represents a connection from point A to point B passing by point C using line features from a layer as the possible route.
I used the points to route script (from here) to generate the new routelayer using a startpoint, endpoint and points to pass by. Unfortunately this generated routelayer does not contain information about underlying line features and their spatial order. For that I used geometry.contains() and geometry.overlaps() functions to get the original features from the underlying line layer. The problem with that is, there is no order to the line features.
The code for that is as follows:
selections=[]
for f in routelayer.getFeatures(): #iterating over route
for a in originallayer.getFeatures(): #itering over all existing line features
if f.geometry().contains(a.geometry()):
selections.append( a )
elif f.geometry().overlaps(a.geometry()):
selections.append( a )
Here is a visual example of the problem:
The colored lines represent elements of selected features from the code above. Each feature has an ID as shown in the image. It's important to note that the IDs are not in any logical order, so sorting them doesn't achieve what I need.
My unsorted list looks a follows (example):
ID
4
56
10
8
15
I would like it to be in the correct order of line elements for the route from A to B.
ID
8
4
56
15
10
Anyone got an idea on how to solve this? If there is a way to get the correct order of underlying line features at the stage of generating the route it would also work.