网格化数据值的插值 - python
Interpolation of gridded data values - python
问题:有没有办法从 geodataframe
重新采样网格化值以绘制更平滑的地图?
详情:
我正在使用名为 gdf
的 24x24 网格。网格的每个 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...
这是我绘制地图时得到的地图类型:
如您所见,图中一个单元格到另一个单元格的值发生了非常剧烈的变化,我想将其平滑化。
有没有办法将每个单元格分成 2 或 3 个子单元格(水平和垂直)以获得更高分辨率的网格,然后对值进行插值以获得平滑的渐变而不是这样? 知道我正在尝试将数据保留为 geodataframe
,因为稍后我需要将它们转换为 shapefile
。
我找到了一种方法,允许我通过 plt.imshow()
执行此操作,因为有一个 interpolation
选项;这会给我我想要的东西,但这只提供图像作为输出,我不能直接用它修改 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()
为了补充我的评论,另一种方法是将您的网格视为图像并使用 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)
您也可以使用 different interpolation 方法。要将您的数据返回到 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
问题:有没有办法从 geodataframe
重新采样网格化值以绘制更平滑的地图?
详情:
我正在使用名为 gdf
的 24x24 网格。网格的每个 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...
这是我绘制地图时得到的地图类型:
如您所见,图中一个单元格到另一个单元格的值发生了非常剧烈的变化,我想将其平滑化。
有没有办法将每个单元格分成 2 或 3 个子单元格(水平和垂直)以获得更高分辨率的网格,然后对值进行插值以获得平滑的渐变而不是这样? 知道我正在尝试将数据保留为 geodataframe
,因为稍后我需要将它们转换为 shapefile
。
我找到了一种方法,允许我通过 plt.imshow()
执行此操作,因为有一个 interpolation
选项;这会给我我想要的东西,但这只提供图像作为输出,我不能直接用它修改 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()
为了补充我的评论,另一种方法是将您的网格视为图像并使用 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)
您也可以使用 different interpolation 方法。要将您的数据返回到 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