I often want to join sequences of GPS points to form sets of lines that represent periods of at-sea survey effort.
My GPS point file includes an integer "Order"
field with unique values, and an integer "Leg"
field. "Order"
specifies the sequence for points to be joined; "Leg"
specifies that points with the same value should be grouped into separate paths / lines. The CRS is WGS84.
I've used Points2One and Points To Paths plugins to do this successfully in the past. But in recent versions of QGIS 3, the processing toolbox's "Points to Path" joins all points in order as a single line only. The "Leg"
field appears to be ignored.
I keep the option box for closed paths un-ticked. I've tried different tables, geographical and projected CRS, converted "Order"
and "Leg"
fields between integer and text, but always the same result.
Can anyone tell me whether I'm making a mistake, if this is a bug, or an unintended application of points to path? And if there is a simple workaround I can use. (Please, if a workaround involves applying code, include an explanation of how to add and run the code…). I'm currently using QGIS 3.22.1-Białowieża.
Here is a link to a cut down copy of my GPS points file – I would like it to form two separate lines (leg 20 and leg 21):
https://we.tl/t-5UbL8YJ59R
Best Answer
Manual solution
See below for automatic solution
Use the
Id
from your points asOrder expression
. To separate the two groups (clusters) of points, create an expression forPath group expression
. As the last point of the northern line has anId = 82398
and the first of the southern line hasId = 32399
, set the expression toId <= 82398
:Separate lines created for the two groups of points:
Automatic solution
For an automatic solution, calculate the distance from each point to the next point (point with the next id). If the distance is larger than a ceratin threshold, consider this as a new line.
Proceed as follows:
Calculate the distance to the next line and create a new field named
length_to_next
with this value (consider reprojecting your points to a projected CRS for distance measurements):Create a new field
group
that generates a common value for all points belonging to the same line. To do so, aggregate all values from the fieldlength_to_next
that are larger then a certain threshold (here: 50 meters). These are the end-points of the lines (from this point, it is more then 50 meters to the next point, thus a new line starts). Get theId
of these last points, again in an array: you get theId
s of all end-points. Then check for each point if it'sId
is larger then every of the end-points Id and return all end-points-Id that fulfill this condition. In the end, get the highest of the end-point-Ids. In this way, you assign to each point the Id of the start-point of the line it belongs to. To achieve this, create a new field with this expression. Change the threshold distance (here:50
) on line 7:Use the attribute
group
created in step 2 asPath group expression
in thePoints to Path
tool.Points classified based on the field
group
from step 2. Create the line based on this attribute: