QGIS – Connecting Points to Lines in Order Using QGIS

pointpoints-to-linepolyline-creationqgisshortest path

I have a set of points and I want to create lines from them. Points belonging to one line have unique "UniqueID" to be grouped by. I try to use QGIS's "Points to path algorithm". What I don't have is an order field, so the problem is that it connects the points in random order.

enter image description here

The picture contains an example of a line with 6 points. The QGIS Points to path algorithm connects the points in the following order: 1,3,2,6,4,5 and creates visually a good looking line, but with the length longer than the line between point 1 and 6. So I would like to know how to establish the order for the points to be added when creating a line. The points need to be connected from beginning to end in order i.e. 1,2,3,4,5,6.

enter image description here

The attribute table is shown on the picture. Each point has unique "fid" and a field called "UniqueID" that groups the points to one line i.e. points that belong to the same line have the same "UniqueID". The name of the attribute comes from the data I have received so can't really make it better.

How can I introduce an order so that I can have points connected by shortest path? The end goal is to calculate the length of lines, so if that could be done easily on the lines created from the unordered points, it is also a good solution to the issue.

Best Answer

If you are looking for a simple line that is going in only one direction, a straightforward way to solve your problem is to use the derivated geometry attributes in order to join the points in the correct order.

Using the Points to path algorithm, add the geometry attribute in Order by expression.

For example, $X will join the points form the left to the right, while $Y will join the points from south to north.

You can use the UniqueIDfield as Path group expression in order to create lines between points with the same unique ID.