Rasterio数据源的形状与给定的索引1不一致。

12

我需要将一个三频段的geotiff文件保存到磁盘上。我目前正在使用rasterio,但是当我尝试写出这个三频段图像时,我遇到了错误信息Source shape (1, 3445, 4703, 4) is inconsistent with given indexes 1

我的最终目标是能够对一幅图像执行一些分析并将结果写出到文件中。

我已经尝试过reshape_as_rasterreshape_as_image。我也尝试了其他一些组合,比如.transpose(arr, (0,1,2))

https://rasterio.readthedocs.io/en/stable/topics/image_processing.html#imageorder

with rio.open(r"C:\Users\name\Documents\project\name.tif") as src:
naip_data = src.read()
naip_meta = src.profile

image = reshape_as_raster(naip_data)

with rio.open('C:\\Users\\name\\Documents\\UAV_test_save\\filename.tif',     'w',**naip_meta) as dst:
        dst.write(image, 3)

我希望能够保存一个geotiff文件,但实际得到的是:

rasterio_io.pyx中的ValueError在rasterio._io.DatasetWriterBase.write()中

ValueError: 源形状(1、3445、4、4703)与给定索引1不一致


我找不到有人遇到过这个问题,有人能帮忙吗? - freegnome
4个回答

7

我遇到了同样的问题。我通过删除“indexes”参数来解决它。

所以,代码应该改为:

dst.write(image,indexes=3)

使用这个:

dst.write(image)

2
谢谢。我用相反的方法解决了这个问题(我在 dst.write 中添加了 , indexes = 1)。 - mikey

2

源元数据包含字段count,其值为1。您需要将该值更新为所需的输出波段数。

source_meta.update({
    "count": 3
})

1

看起来您正在尝试编写一个4条带栅格图像。

rio将(1, 3445, 4703, 4)解读为1条带,3445行,4703列和另一维的4个值...我不是rio专家。

如果您想保留4条带:

naip_data2 = np.moveaxis(naip_data.squeeze(),-1,0) # move axis with 4 entries to beginning and remove extra dimension

print(naip_data2.shape) # check new shape, should be (4,3445,4703)
print(naip_meta) # check that naip_data2.shape == (naip_meta['count'],naip_meta['height'],naip_meta['width'])

# if they are equal, naip_meta doesn't need to be updated, otherwise update it
with rio.open(fname,'w',**naip_meta) as dst:
    dst.write(naip_data2)

0

好的资源

with rio.open(r"C:\Users\name\Documents\project\name.tif") as src:
naip_data = src.read()
naip_meta = src.profile

image = naip_data.reshape(3445,4703,4)

with rio.open('C:\\Users\\name\\Documents\\UAV_test_save\\filename.tif',     
'w',**naip_meta) as dst:
    dst.write(image, [0,1,2],[2,1,0])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接