I.M.
I.M.

Reputation: 344

Interpolation of gridded data values - python

Question : Is there a way to resample gridded values from a geodataframe to plot a smoother map?

Details : I am working with a 24x24 grid named gdf. Each cell of the grid has a value and an id as attributes:

#gdf.head()

    id      values      geometry
0   1       52.390119   POLYGON ((653179.710 6859158.392, 653179.710 6...
1   2       52.390119   POLYGON ((653179.710 6858908.392, 653179.710 6...
2   3       52.390119   POLYGON ((653179.710 6858658.392, 653179.710 6...
3   4       49.592331   POLYGON ((653179.710 6858408.392, 653429.710 6...
4   5       52.390119   POLYGON ((653429.710 6858408.392, 653179.710 6...

This is the type of map I get when I plot it:

Initial gridded data

As you can see there are very harsh changes in the values from a cell to another in the plot and I would like to smoother that out.

Is there a way to divide each cells into 2 or 3 sub-cells (horizontally and vertically) to get a grid of higher resolution and then interpolate the values to get smooth gradients instead of this? Knowing that I am trying to keep the data as a geodataframe since I need to convert them into a shapefile later on.


I found a method that allows me to do it via plt.imshow() as there is an interpolation option ; which would give me exactly what I want but this only gives an image as an output, I cannot directly modify gdf with it:

grid = np.array(file.data).reshape(-1, 24)[::-1]

fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(20, 20), subplot_kw={'xticks': [], 'yticks': []})

for ax, interp_method in zip(axs.flat, methods):
    ax.imshow(grid, interpolation='lanczos', cmap='RdYlGn_r')

plt.tight_layout()
plt.show()

Upvotes: 2

Views: 1303

Answers (1)

user7804910
user7804910

Reputation:

To complement my comment, another way is simply to consider your grid as an image and use the PIL library:

import numpy as np
from PIL import Image

image = PIL.Image.from_array(grid)
w, h = image.size
ratio = 4
image = image.resize((w*ratio, h*ratio), Image.BILINEAR)
image.show()
grid = np.array(image)

You can use different interpolation methods as well. To get your data back into a pandas dataframe:

# flatten your grid and get your values back into a column
pd.DataFrame(grid.flatten(), columns=['values'])

# add an id column that starts a 1
df['id'] = df.index + 1

Upvotes: 1

Related Questions