12

I need to save out a 3 band geotiff to file. I am currently using rasterio and when I go to write out the 3 band imagery I get the error Source shape (1, 3445, 4703, 4) is inconsistent with given indexes 1.

My final goal is to be able to perform some analysis on an image and write this out to file.

I have already tried reshape_as_raster and reshape_as_image. I have tried a few other combinations as well as .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)

I am expecting to have a geotiff saved in the file. instead I get:

ValueError rasterio_io.pyx in rasterio._io.DatasetWriterBase.write()

ValueError: Source shape (1, 3445, 4, 4703) is inconsistent with given indexes 1

razdi
  • 1,388
  • 15
  • 21
freegnome
  • 121
  • 1
  • 1
  • 5

4 Answers4

7

I have had the same issue. I solved it by removing the 'indexes' argument.

so instead of:

dst.write(image,indexes=3)

use this:

dst.write(image)
sehan2
  • 1,700
  • 1
  • 10
  • 23
  • 1
    Thanks. I was able to solve the issue with the opposite approach (I added `, indexes = 1` to `dst.write`) – mikey Oct 13 '22 at 17:01
2

Source metadata contains field count and it is equal to 1. You need to update that value to the desired number of output bands.

source_meta.update({
    "count": 3
})
Ek00f
  • 3
  • 2
1

It looks like you have a 4 band raster that you are trying to write.

rio is reading (1, 3445, 4703, 4) as 1 band, 3445 rows, 4703 columns, and 4 of another dimension...I'm not a rio expert.

If you want to keep your 4 bands:

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)
befpy
  • 31
  • 2
0

Good source

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])
CalebJ
  • 1