I was trying to get Rasterio to save a 3 band raster to a TIFF file. For some reason, the example in the documentation only shows how to save raster with only 1 band. Now when I tried to save a 3 band raster, I get a really uninformative error message.
The code is below. I opened a raster to collect some of the metadata. Then I created a new downsampled version of the raster and was trying to save that to a new file. The raster has 3 bands.
dat = rasterio.open('image_original.tif')
with rasterio.open('image_original.tif') as dataset:
data = dataset.read(
out_shape=(int(dataset.height / 3.75), int(dataset.width / 3.75), dataset.count),
resampling=Resampling.cubic
)
with rasterio.open(
'image_15cm.tif',
'w',
driver='GTiff',
height=data.shape[0],
width=data.shape[1],
count=data.shape[2],
dtype=data.dtype,
crs=dat.crs,
transform=dat.transform,
) as dst:
dst.write(data, 3) //PROBLEM LINE
So this last line creates problems. I wrote dst.write(data,3)
and get an error:
ValueError: Source shape (1, 11404, 15902, 3) is inconsistent with given indexes 1
Alternatively if I use the code dst.write(data)
then I get the message
ValueError: Source shape (11404, 15902, 3) is inconsistent with given indexes 3
which still does not indicate what the problem is or how to solve it.