python - 无法从栅格中的堆栈数组创建虚拟栅格
问题描述
我需要知道如何创建虚拟栅格。我遍历了一个文件夹,其中包含一些二进制栅格 (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)
有谁知道如何解释这个错误信息?谢谢
解决方案
VRT 是只读格式,不能将数组写入 VRT。要创建 VRT 文件,您只需参考现有的磁盘光栅文件。例如,在 Python 中使用 gdal.BuildVRT 和光栅文件列表,如示例 3中所示。
推荐阅读
- javascript - 何时使用 call 或在 JavaScript 中使用 call 有什么好处?
- python-3.x - 从被调用函数访问类属性?
- sap-cloud-sdk - 使用 SAP Cloud SDK 生成的 VDM 获取导航属性
- scala - Databricks - 尝试在单元格之间捕获
- javascript - 使用fabric js在画布中进行多项选择时过滤选择
- boolean - 线性规划中的布尔表达式
- json - 解析 curl 输出
- html - 如何使用 CSS 从下到上显示/填充 div 以获得以下效果
- jenkins-pipeline - 如何在詹金斯管道中获得覆盖率
- perl - 省略参数时如何让子使用 $_?