0

I have many polygons, some have values, others don't. In the image, you see an example. The two grey polygons don't have values, while others have:

enter image description here

Is there maybe a tool in QGIS which can help with this task? It would also be fine to use some GeoPandas with it.

3
  • 3
    How (by which logic) should the value be calculated?
    – Bera
    Commented Nov 21, 2023 at 9:51
  • I thought at some kind of a mean value based on the values of the adjacent neighbors and probably weighted by the length of the shared boundary.
    – i.i.k.
    Commented Nov 21, 2023 at 9:56
  • 5
    Use QGIS expressions with overlay functions (e.g. overlay_touches, see docs.qgis.org/latest/en/docs/user_manual/expressions/…) to get the neighboring polygon's attribute values, then do the calculation you want (e.g. mean).
    – Babel
    Commented Nov 21, 2023 at 10:24

1 Answer 1

3

You dont specify how you want to interpolate.

This buffers the grey polygons and use the surrounding polygons values to calculate the average, with the intersection areas as weights.

So a polygon with a long shared border with your grey polygon contributes more to the interpolated value.

import geopandas as gpd
import numpy as np

df = gpd.read_file(r"/home/bera/Desktop/GIStest/missing_values.shp")
value_column = "val" #The name of your value column
df["tempid"] = range(df.shape[0]) #Create a temporary id column

#Create a buffered copy of the input df
buffered = df.copy()
buffered.geometry = buffered.geometry.buffer(20) #Adjust the buffer distance

#Intersect the dataframes
inter = gpd.overlay(df, buffered, how="intersection", keep_geom_type=True)
inter["interarea"] = inter.geometry.area

#Calculate a weighted average value for each tempid, with the intersection area as weight
df["weighted_value"] = inter.loc[~gpd.pd.isna(inter[value_column+"_2"])].groupby("tempid_1").apply(
    lambda x: np.average(a=x[value_column+"_2"], weights=x["interarea"]))

df["interpol"] = df["val"].combine_first(df.weighted_value)
df.to_file(r"/home/bera/Desktop/GIStest/interpolated_result.shp")

enter image description here

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.