少し異なる代替策は次のようになります:
import numpy as np
from osgeo import gdal
original = gdal.Open('**PATH**\\origianl_raster.tif')
# read the original file
band = original.GetRasterBand(1) # assuming that the file has only one band
band_array = band.ReadAsArray()
#create a new array with reclassified values
new_array = np.where(band_array == np.nan, 4,
np.where(band_array == 0, 4,
np.where(band_array == 1, 1,
np.where(band_array == 2, 2,
np.where(band_array == 3, 3,
np.where(band_array == 4, 3,
np.where(band_array == 5, 4,
np.where(band_array == 6, 5,
np.where(band_array == 7, 5,
np.where(band_array == 8, 6,
np.where(band_array == 9, 7,
np.where(band_array == 10, 8,
np.where(band_array == 100, np.nan, np.nan)))))))))))))
# the last line also includes values equal to 255, as they are the only ones left
# create and save reclassified raster as a new file
outDs = gdal.GetDriverByName('GTiff').Create("**PATH**\\reclassified_raster.tif", original.RasterXSize, original.RasterYSize, 1, gdal.GDT_Float32)
outBand = outDs.GetRasterBand(1)
outBand.WriteArray(new_array)
outDs.SetGeoTransform(original.GetGeoTransform())
outDs.SetProjection(original.GetProjection())
# flush cache
outDs.FlushCache()
このスクリプトはnumpy.where(https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html)で再生されます。最後のステップ以外のすべてのステップで、条件が満たされない場合、別のnp.whereが返されます。そして、ラスターのすべての可能な値が考慮されるまで、それは続きます。