无法从rasterio

时间:2019-07-12 13:46:18

标签: python numpy rasterio

我需要知道如何创建虚拟栅格。我遍历了一个文件夹,其中包含一些二进制栅格(1、0)。我使用numpy.concatenate将这些栅格附加到numpy数组中。然后,我想使用串联的栅格数和该栅格将具有的波段数来创建虚拟栅格。我虽然收到了Followgin消息:

---------------------------------------------------------------------------
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

我检查了栅格的数量,它对应于变量“ dimension”,y是在写出最终的虚拟栅格时输入的。

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)

有人知道如何解释此错误消息吗? 谢谢

1 个答案:

答案 0 :(得分:1)

VRT是只读格式,不能将阵列写入VRT。要创建VRT文件,您仅引用现有的磁盘上栅格文件。例如。在example 3 here中将栅格文件列表与Python一起使用gdal.BuildVRT。