我需要知道如何创建虚拟栅格。我遍历了一个文件夹,其中包含一些二进制栅格 (1, 0)。我使用 numpy.concatenate 将这些栅格附加到一个 numpy 数组中。然后我想使用连接的栅格数作为该栅格将具有的波段数来创建一个虚拟栅格。我收到以下消息:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-93-7a57711dc737> in <module>
20 compress = 'lzw')
21 with rasterio.open(path_scl + "/" + "scl_stack.vrt", "w", **profile) as dst:
---> 22 dst.write(final_array.astype(rasterio.uint8), dimension)
rasterio/_io.pyx in rasterio._io.DatasetWriterBase.write()
IndexError: band index out of range
我检查了栅格的数量,它对应于变量“维度”,是在写出我的最终虚拟栅格时输入的。
path_scl = r'I:\Sentinel-2\Central\2017\T32TNT'
files = [os.path.join(root, file) for root, directories, filenames in os.walk(path_scl) for file in filenames]
scls = [file for file in files if file.endswith("_01_cloud_mask_bin.tif")]
final_array = np.zeros((10980, 10980))
for scl in scls:
with rasterio.open(scl) as ds:
profile = ds.profile
array = ds.read(1)
np.concatenate((final_array, array), axis = 0)
print(f"{scl} added")
dimension = len(scls)
with rasterio.Env():
profile = profile
profile.update(
dtype = rasterio.uint8,
compress = 'lzw')
with rasterio.open(path_scl + "/" + "scl_stack.vrt", "w", **profile) as dst:
dst.write(final_array.astype(rasterio.uint8), dimension)
有谁知道如何解释这个错误信息?谢谢