0

This is my first question. I am new to python so please bear with me. I have a shapefile that contains data as given below.

ID   Data  geometry
111  0.1   POINT(X,Y)
112  0.2   POINT(X,Y)
113  0.5   POINT(X,Y)
114  NaN   POINT(X,Y)
115  0.3   POINT(X,Y)
116  NaN   POINT(X,Y)
117  NaN   POINT(X,Y)

I want to fill missing values in Column "Data" using spatial interpolation.
I have tried this tutorial https://www.youtube.com/watch?v=OfC3KpL4PRw but it adds a lot of extra coordinates to my data. Is there any simpler way to fill those missing points using spatial interpolation?

RDK19
  • 1
  • Scipy has a 2D interpolation module. But stack overflow isn't for library recommendations. You need have (nearly) working code and a specific issue. You might get better responses at gis.stackexchange.com – Paul H Dec 06 '21 at 16:41

1 Answers1

0
  • you can work out complete distances matrix and use that to look up values of nearest points
  • I would not expect this to scale to large numbers of points
  • have simulated some data as question is abstract
import geopandas as gpd
import pandas as pd
from scipy.spatial import distance
import numpy as np
import shapely.geometry

gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).set_crs("EPSG:4326")

# get some points - UK boundary
gdf = gdf.loc[gdf["iso_a3"].eq("GBR"), "geometry"].apply(lambda g: g.geoms).explode().apply(
    lambda g: g.exterior.coords
).explode().apply(shapely.geometry.Point).reset_index(drop=True).to_frame().assign(
    data=lambda d: np.arange(0, len(d))
)
gdf = gpd.GeoDataFrame(gdf)

# randomly set 10 values to NaN
gdf.loc[gdf.sample(20).index, "data"] = np.nan

# build matrix of distances...
d = pd.DataFrame(distance.cdist(*[np.stack(gdf.geometry.apply(lambda x: [x.x, x.y]))] * 2))

# take mean of nearest 3 co-ordinates
gdf["data_i"] = gdf.apply(
    lambda r: gdf.loc[d[r.name].sort_values().index, "data"].dropna().head(3).mean()
    if np.isnan(r["data"])
    else r["data"],
    axis=1,
)

gdf.plot(column="data")
gdf.plot(column="data_i")
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30