如何在 pandas 中缩小分辨率为 300m x 300x 的土地覆盖数据集

问题描述 投票:0回答:1

我有一年的土地覆盖 netcdf 文件,分辨率为 300m。 这太详细了,我想将其缩小到大约 3k。

目前我正在使用

.nc
打开
xarray
文件 这给了我一个数据集:

Dimensions: time: 1 lat: 64800 lon: 129600 bounds: 2

它也有很多变数...我唯一感兴趣的是

lccs_class (time, lat, lon)

我删除不需要的变量:

ds = ds.drop(['processed_flag', 'current_pixel_state', 'observation_count', 'change_count', 'crs', 'time_bounds'])

我想查看特定的 1x1 度块 - 所以我这样设置:

central_lat = -33.9248685
central_lon = 18.4240553

lat_min = central_lat - 0.5
lat_max = central_lat + 0.5

lon_min = central_lon - 0.5
lon_max = central_lon + 0.5
filtered_ds = ds.sel(lat=slice(lat_max, lat_min), lon=slice(lon_min, lon_max))

我将其转换为 pandas 数据框:

<class 'pandas.core.frame.DataFrame'>
MultiIndex: 259200 entries, (Timestamp('2019-01-01 00:00:00'), -33.42638888888889, 17.92638888888891, 0) to (Timestamp('2019-01-01 00:00:00'), -34.42361111111111, 18.923611111111114, 1)
Data columns (total 3 columns):
 #   Column      Non-Null Count   Dtype  
---  ------      --------------   -----  
 0   lccs_class  259200 non-null  uint8  
 1   lat_bounds  259200 non-null  float64
 2   lon_bounds  259200 non-null  float64
dtypes: float64(2), uint8(1)
memory usage: 5.7 MB

我想缩小以查看

3000mx3000m
代表性的块。

必须有一种已知的技术来做到这一点?

我正在考虑以下方法:

取 9 个块的中点:当

(row - 1) % 3 == 0

时选择块的位置(x)
0 0 0 0 0 0 0 0 0
0 x 0 0 x 0 0 x 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 x 0 0 x 0 0 x 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 x 0 0 x 0 0 x 0
0 0 0 0 0 0 0 0 0

然后选择周围块中占主导地位的

lccs_class
。但如果数量相等怎么办?

我也不确定如何从数据集中计算 x 块和周围的块。

python pandas math geospatial netcdf
1个回答
0
投票

您可以使用

iloc
进行简单的切片:

sub_df = df.iloc[1::3, 1::3]

输出:

   1  4  7
1  x  x  x
4  x  x  x
7  x  x  x

使用的输入:

   0  1  2  3  4  5  6  7  8
0  0  0  0  0  0  0  0  0  0
1  0  x  0  0  x  0  0  x  0
2  0  0  0  0  0  0  0  0  0
3  0  0  0  0  0  0  0  0  0
4  0  x  0  0  x  0  0  x  0
5  0  0  0  0  0  0  0  0  0
6  0  0  0  0  0  0  0  0  0
7  0  x  0  0  x  0  0  x  0
8  0  0  0  0  0  0  0  0  0
© www.soinside.com 2019 - 2024. All rights reserved.