使用rasterio库读取文件
原来使用了numpy数组循环重新赋值的方法,速度非常缓慢,三个多小时
import rasterio
from tqdm import tqdm
with rasterio.open('output_fuel/allveg_buffer1.tif') as src:
raster_data = src.read(1) # 读取第一个波段
rows, cols = raster_data.shape
LC_data=raster_data
for i in tqdm(range(rows)): #tqdm进度条
for j in range(cols):
if LC_data[i, j]>51 and LC_data[i, j]<70:
LC_data[i, j] = 2
elif LC_data[i, j] == 91 or LC_data[i, j] == 92:
LC_data[i, j] = 2
elif LC_data[i, j]>70 and LC_data[i, j]<83:
LC_data[i, j] = 1
elif LC_data[i, j]>119 and LC_data[i, j]<131:
LC_data[i, j] = 3
elif LC_data[i, j]==152:
LC_data[i, j] = 3
elif LC_data[i, j]==153:
LC_data[i, j] = 5
更改为向量法不到十秒就跑完了
# 应用向量化的重分类规则
LC_data = np.zeros_like(raster_data, dtype=np.uint8) # 初始化重分类后的数组
LC_data[(raster_data > 51) & (raster_data < 70) | (raster_data == 91) | (raster_data == 92)] = 2
LC_data[(raster_data > 70) & (raster_data < 83)] = 1
LC_data[(raster_data > 119) & (raster_data < 131) | (raster_data == 152)] = 3
LC_data[raster_data == 153] = 5
LC_data[raster_data == 11] = 5
LC_data[(raster_data == 180) | (raster_data == 150)] = 4
# 其他值已经通过初始化为0