Rasterio源形状与给定索引1不一致

时间:2019-10-01 23:34:40

标签: python geotiff rasterio

我需要保存一个3波段的geotiff文件。我当前正在使用rasterio,当我写出3波段图像时,出现错误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。相反,我得到了:

  

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

     

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

2 个答案:

答案 0 :(得分:3)

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

所以而不是:

dst.write(image,indexes=3)

使用此:

dst.write(image)

答案 1 :(得分: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)