2

I have image as ndarray with 20 bands with the following shape: (20, 2227, 3676) . I'm trying to save it using rasterio with the following script:

savedir=pathlib.Path('myfolder/')
savedir.mkdir(parents=True,exist_ok=True)


with rasterio.open(savedir /'myImage.tif', 
                                'w',
                                driver='GTiff',
                                height=img.shape[1],
                                width=img.shape[2],
                                count=20,
                                dtype=img.dtype,
                                crs=img_crs,
                                nodata=None, # change if data has nodata value
                                transform=img_transform) as dst:  
        
        for n in np.arange(0,20,1):
                dst.write(img[n,:,:],n+1)

This fails with he following error:

---> 17 dst.write(img_arr[n,:,:],n+1)

File rasterio/_io.pyx:1700, in rasterio._io.DatasetWriterBase.write()

TypeError: object of type 'numpy.int64' has no len()

I'm not sure what is the source of this error, but I know that if I use the same script but instead of iterating through the bands I put manualy each band, it works. for example:

dst.write(img_arr[0,:,:],1)
dst.write(img_arr[1,:,:],2)
...
dst.write(img_arr[19,:,:],20)

That works.

Hence, I don't know why I get the type error, and I don't waant to put manually all the bands of the image. How can I solve this issue?

5
  • Juste one question, why don't you do dst.write(img) ?
    – remi.braun
    Feb 22 at 9:46
  • well usually if I don't specifiy the shape and the specific band, it get mismatched / fail
    – ReutKeller
    Feb 22 at 10:04
  • You cannot have a mismatch between height/width/count if you specify them correctly : count=img.shape[0], height=img.shape[1], width=img.shape[2]
    – remi.braun
    Feb 22 at 10:20
  • Ho, I just saw, but the driver should be "GTiff" -> rasterio.readthedocs.io/en/latest/…
    – remi.braun
    Feb 22 at 10:34
  • thank you for this! that happened when I edited it from the original code, I have used GTiff originally :)
    – ReutKeller
    Feb 22 at 12:23

2 Answers 2

3

It's not the dtype of the img array that's the problem, it's your n variable.

rasterio is expecting a python int not a numpy.int64 for the band number.

# this works
for n in range(0,1):
    dst.write(img[n,:, :], n+1)

# this works too
for n in np.arange(0,1):
    dst.write(img[n,:, :], int(n+1))

# this works for me
dst.write(img)

# this fails with `TypeError: object of type 'numpy.int64' has no len()`
for n in np.arange(0,1):
    dst.write(img[n,:, :], n+1) # n is np.int64 not int
1

What version of GDAL are you using ? It seems that tyou need a GDAL > 3.5.0 to have the support of writing INT64 dtype: https://github.com/OSGeo/gdal/issues/3325 (and rasterio > 1.3.0)

But do you really need INT64 ? It is really heavy on disk and usually useless.

2
  • I haven't downlaoded gdal to my venv. I'm using "rasterio==1.3.5.post1" , not sure where this post1 came from, to be honest. and for the data type - no , I don't neeed int64, the bands are either uint8 or float
    – ReutKeller
    Feb 22 at 10:03
  • 1
    OK, rasterio 1.3.5 includes GDAL 3.5, no issue from that point of view. However, I strongly suggest you to convert your img to the dtype you really need, it will save you a lot of disk space
    – remi.braun
    Feb 22 at 10:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.