Calculation over multiple vector layers in QGIS

field-calculationlayersqgisvector

I have a few points as a layer. Then I have another layer with the same points but with different values. For example:

Layer A: Time: 5 [hours]

Layer B: Time: 4 [hours]

Is there a QGIS function that calculates the difference between these values? Something like the raster calculator but for vector layers?

Best Answer

Try:

"layer_a_attr" - overlay_intersects('Layer B',"layer_b_attr")[0]

in field calculator of Layer A. Make sure the points do indeed really intersect, if they dont do, you can add a small buffer. The index bracket [0] is needed because there may be more than one intersecting point and therefore overlay_intersect() returns an array. [0] returns the first value of the array. overlay_intersects() is available since QGIS 3.16. For earlier versions you could use aggregate() function instead.

As suggested by @Babel, you can also use overlay_nearest('Layer B',"layer_b_attr",max_distance:=50)[0] instead of a buffer, which shortens the expression and is easier to handle.