Python 无法从rasterio中的堆栈阵列创建虚拟光栅
我需要知道如何创建虚拟光栅。我迭代了一个文件夹,其中包含一些二进制光栅1,0。我使用numpy.concatenate将这些光栅附加到numpy数组中。然后,我想创建一个虚拟光栅,使用连接的光栅数量作为该光栅将具有的波段数量。但我收到以下信息:Python 无法从rasterio中的堆栈阵列创建虚拟光栅,python,numpy,rasterio,Python,Numpy,Rasterio,我需要知道如何创建虚拟光栅。我迭代了一个文件夹,其中包含一些二进制光栅1,0。我使用numpy.concatenate将这些光栅附加到numpy数组中。然后,我想创建一个虚拟光栅,使用连接的光栅数量作为该光栅将具有的波段数量。但我收到以下信息: --------------------------------------------------------------------------- IndexError Traceback
---------------------------------------------------------------------------
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
有人知道如何解释这个错误消息吗?
感谢VRT是一种只读格式,您不能将数组写入VRT。要创建VRT文件,只能引用现有的磁盘光栅文件。例如,将Python中的gdal.BuildVRT与光栅文件列表一起使用,如中所示 多谢各位!这澄清了我的问题。我必须以GeoTiff格式编写数组。
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)