QGIS – Points to Path Not Grouping Points to Lines Issue

pointpoints-to-linepolyline-creationqgis

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 as Order expression. To separate the two groups (clusters) of points, create an expression for Path group expression. As the last point of the northern line has an Id = 82398 and the first of the southern line has Id = 32399, set the expression to Id <= 82398:

enter image description here

Separate lines created for the two groups of points: enter image description here


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:

  1. 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):

    length (make_line ($geometry, geometry(get_feature_by_id (@layer, $id+1))))
    
  2. 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 field length_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 the Id of these last points, again in an array: you get the Ids of all end-points. Then check for each point if it's Id 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:

     array_max(
          array_foreach (
             array_foreach (
                 array_foreach (
                     array_agg( 
                         length_to_next, 
                         filter:=length_to_next > 50
                     ),
                     get_feature( 
                         @layer, 
                         'length_to_next',
                         @element
                     )
                 ),
                 attribute (
                     @element,
                     'Id'
                 )
             )
             ,
             case 
             when @element < Id then @element end
         )
     )
    
  3. Use the attribute group created in step 2 as Path group expression in the Points to Path tool.

Points classified based on the field group from step 2. Create the line based on this attribute: enter image description here

Related Question