You can use QGIS expressions with the new overlay_nearest
function, availble since QGIS 3.16 and the array_mean()
function, available since QGIS 3.18.
If you already have an attribute in the polygon layer (lets say with fieldname values
), than applying this epxression on the polygon layer with field calculator will get you the mean value of the neighboring feature's values
field. The limit:=10
defines from how many polygons (in the order of the distance to the current feature) you take the values to calculate the mean. You can change this. Setting it to 5 means that only the value of the five nearest polygons is taken into consideration.
array_mean (
overlay_nearest(
@layer,
values,
limit:=10
)
)
Options:
If you replace overlay_nearest
with overlay_touches
, you only get the polygons that border on (touch) your polygon.
If you replace @layer
on the third line with 'points'
(the name of your point layer), you can use the same expression to refer to the nearest 10 (or 5 or whatever) points. Further options include defining a maximum distance up to which points should be consideres - just add ,max_distance:= 120
after the limit
condition.
Screenshot: the field calculator calculates a value of 18 (see the preview at the bottom), based on the 10 nearest polygons. The lines (created with an expression based on this one) are a visualization helping to understand from which values the mean is calculated: the ten nearest polygons. array_mean ()
returns the mean value of the values
attribute of these ten features: