简体   繁体   中英

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:

初始网格数据

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM