16

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

1
  • I cant find anyone with this issue, Any help out there? Commented Oct 8, 2019 at 22:27

4 Answers 4

10

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I was able to solve the issue with the opposite approach (I added , indexes = 1 to dst.write)
To clarify a bit, if you have a 2d array, you should include the indexes and if you have a 3d one, you should not
2

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)

Comments

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
})

Comments

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])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.