3

I have created an example project in QGIS to help illustrate what I am having difficulty with. Using the field calculator, I would like to automate assigning sequential numbers to my points layer according to their position in relation to the 0 vertices point of my line layer. The points do not necessarily intersect with the line and the distance from the vertices point does not necessarily increase. Both the points and the line share a connecting identical attribute, in this case "name" is 'a'. Automating this would be a massive help because i have ~1500 points and 70 lines to put into sequence.

Anyone know how to do this?

enter image description here

The result should look like this :

enter image description here

1 Answer 1

5

Assuming your point layer has a field point_att that corresponds to the field line_att in your line layer (named line_layer), and line_att is unique, as in the image below.

enter image description here

Try this expression on your point layer using Field Calculator or in the Labels expression:

array_find(
    array_agg($id,
              "point_att",
              order_by:= line_locate_point(
                  geometry(get_feature('line_layer','line_att',"point_att")),
                  $geometry)
              ),
          $id)
+1

Result:

enter image description here


Explanation:

This is a variation of this answer I posted about using Field Calc to custom sort by another field.

Here, the array_agg() function first groups together the $id values of all other points that correspond to the same matching line (= has the same point_att value), into an array.

Then instead of sorting that array by another field in the dataset per the linked answer, this expression sorts the array by each point's relative position along the matching line (line_locate_point()), calculated on the fly.

The final part is the same; for each feature return its position in that array using array_find(). Then add +1 at the end so that the sequence starts from 1.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.