[英]Interpolation of gridded data values - python
Question : Is there a way to resample gridded values from a geodataframe
to plot a smoother map?问题:有没有办法从
geodataframe
重新采样网格值以绘制更平滑的地图?
Details : I am working with a 24x24 grid named gdf
.详细信息:我正在使用名为
gdf
的 24x24 网格。 Each cell
of the grid has a value
and an id
as attributes:网格的每个
cell
格都有一个value
和一个id
作为属性:
#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?有没有办法将每个单元格分成 2 或 3 个子单元格(水平和垂直)以获得更高分辨率的网格,然后插入值以获得平滑的梯度而不是这样? Knowing that I am trying to keep the data as a
geodataframe
since I need to convert them into a shapefile
later on .知道我正在尝试将数据保留为
geodataframe
数据geodataframe
因为我稍后需要将它们转换为shapefile
。
I found a method that allows me to do it via plt.imshow()
as there is an interpolation
option ;我找到了一种方法,它允许我通过
plt.imshow()
来完成,因为有一个interpolation
选项; which would give me exactly what I want but this only gives an image as an output, I cannot directly modify gdf
with it:这将给我我想要的东西,但这只会给出一个图像作为输出,我不能用它直接修改
gdf
:
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:为了补充我的评论,另一种方法是将您的网格视为图像并使用
PIL
库:
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:
要将您的数据恢复到 Pandas 数据框中:
# 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.