To calculate the distances in a table:
First ensure that your buffers layer and trees layer have a field with a unique identifier. I used tree_id
and buffer_id
.
Perform a spatial join between the trees and the buffers using a one-to-many join type. This will give you a new points layer where points are duplicated when they are within more than one buffer.
Tree T23
now appears 3 times, in buffers 2
, 3
, and 4
.
- Then use this expression in the Field Calculator on the joined layer:
length(
make_line(
$geometry,
centroid(geometry(get_feature( -- get the centroid of the buffer that matches the buffer_id of the joined point.
layer:='buffers',
attribute:='buffer_id', -- 'buffer_id' in single quotes is the field name
value:="buffer_id"))) -- "buffer_id" in double quotes is the field value of the current joined point
)
)
Now each tree point has a distance to its corresonding buffer centroid:
Example:
(The lines are for visualisation purposes only)