7

From two layers with point geometry, I use the following expression to draw a line from the 'POINT_LAYER_2' layer to the 'POINT_LAYER_1' layer.

make_line($geometry,overlay_nearest('POINT_LAYER_1',$geometry,limit:=1))

Draw line

The layer 'POINT_LAYER_2' contains the numeric field "HOUSES" representing the number of houses.

I would like to label the 'POINT_LAYER_1' layer with the sum value of the "HOUSES" field of the 'POINT_LAYER_2' layer corresponding to points connected.

So far I only manage to label the sum, but on the layer 'POINT_LAYER_2', with the following expression:

sum("HOUSES", group_by:=overlay_nearest('POINT_LAYER_1',$id,limit:=1))

Sum HOUSES

What I would need to get is an expression that works on layer 'POINT_LAYER_1' with the following result:

Result needed

I have tried another approach, but it still does not give the expected result, as it shows the total sum value without filtering the nearest point:

aggregate(
    layer:='POINT_LAYER_2',
    aggregate:='sum',
    expression:="HOUSES",
    filter:=touches(make_line($geometry,overlay_nearest('POINT_LAYER_1',$geometry,limit:=1)), $geometry)
)

enter image description here

1 Answer 1

5

If you have a unique ID in POINT_LAYER_1, you can do it as follows:

Create a virtual field in POINT_LAYER_2 with the ID of the nearest object of POINT_LAYER_1:

overlay_nearest('POINT_LAYER_1', "id" ,limit:=1)[0]

Create a virtual field in POINT_LAYER_1 with the sum of all objects of POINT_LAYER_2 with the corresponding ID:

aggregate('POINT_LAYER_2', 'sum', "HOUSES", "POINT_LAYER_1_id"=attributes(@parent)['id'])

The virtual field in POINT_LAYER_1 can then be used for labelling.

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.