1

I have an ndarray which I would like to save as rasterio raster. I have all the information I needed (image size, transform, band names etc.).
The problem is that when I try to save the image as raster with the band names, the image saved correctly, but the names never saved.

This is snippet of my code:

#open the raster that I want to process
test=rasterio.open('gs://my_bucket/my_imgs/my_elevation/elevation_img.tif')

# access the bands names, will be used later to save the output with same bands names
names=list(test.descriptions)
names
>>>['B1_OK','B2_CK']

#read as array (need it for the processing)
arr=test.read()

####after I processed the image.....

#metadata for the image that will be saved

metadata = {
    'driver': 'COG',
    'height': arr.shape[1],
    'width': arr.shape[2],
    'count': arr.shape[0],
    'dtype': arr.dtype,
    'crs':'EPSG:4326',
    'transform': test.transform,
    'descriptions': names
    }
##save the image
with rasterio.open('/home/my/folder/git/toolim/nbs/tmp/test1.tif', 'w', **metadata) as dst:
    dst.write(arr)

#check the band names of the new image:
rasterio.open('/home/my/folder/git/toolim/nbs/tmp/test1.tif').descriptions
>>>(None,None)

I could not find any reason why this happens, setting names suppose to be relatively simple, so I must do something wrong...
My end goal: be able to use the list of names as post-processed image bands, and in general, to be able to define the bands names and to understand my mistake.

1 Answer 1

1

It should work, I don't know why it doesn't. However, you can set the descriptions property on the open dataset and the descriptions will be saved:

metadata = {
    'driver': 'COG',
    'height': arr.shape[1],
    'width': arr.shape[2],
    'count': arr.shape[0],
    'dtype': arr.dtype,
    'crs':'EPSG:4326',
    'transform': test.transform
    }
##save the image
with rasterio.open('/home/my/folder/git/toolim/nbs/tmp/test1.tif', 'w', **metadata) as dst:
    dst.descriptions = names
    dst.write(arr)
1
  • yes, this solved the issue
    – ReutKeller
    Commented Sep 11, 2023 at 7:47

Your Answer

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

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