4

I have to point layers:

  • Boreholes (with 100 features)
  • Trees (with 300 features)

The tree layer has three attributes:

enter image description here

I would like to find the closest tree (based on "Tree_ID") to each borehole, using the Field Calculator. Traditionally, we use the "Distance Matrix" tool and set K = 1. I have done that, here is the result table:

enter image description here

However, I am wondering if this is possible using the Expression Engine in Field Calculator. I already have calculated the distance to the closest tree for each borehole using this expression:

aggregate(
    layer:= 'Trees', 
    aggregate:= 'minimum', 
    expression:= round(distance(@geometry, geometry(@parent)), 2)
    )

I created a new field for the boreholes, to show the distance to the nearest tree. Here is the result:

enter image description here

Now I would like to find out if I can use the Field Calculator expression engine to find the Tree_ID of the nearest tree. Is it possible to use the expression engine in the Field Calculator to get the ID of the closest tree?

1
  • 1
    @Taras Thanks for the edits. Commented Jul 31 at 17:05

1 Answer 1

4

I would recommend to use overlay-functions for that. They are faster then aggregate. With overlay_nearest you will get the nearest object in the target layer. Be aware that overlay functions return an array of objects. The following expression will return the nearest Tree_ID:

overlay_nearest(layer:='Trees',expression:="Tree_ID")[0]
1
  • That's exactly what I was looking for. However, since you said "overlay functions are faster than aggregate" , I am wondering if this procedure is can be done using aggregate function or not? Commented Aug 2 at 3:39

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.