15

I want to write a raster file (using GDAL+Python) with multiple bands. However, I do not know how to name the bands. Their names are always "Band 1", "Band 2", etc. I searched and found a function that should do the trick but failed (SetRasterCategoryNames). I hope that someone could help me how to name when writing multiple-layer rasters in python.

Following code is just an example of writing multiple bands to a raster.

        from osgeo import gdal
    #Read projection information from original raster
    path = '/home/12101008/MCD43A1.A2007001.h26v06.006.2016111212159.hdf'
    GRID_NAME = 'MOD_Grid_BRDF'
    DATAFIELD_NAME = 'BRDF_Albedo_Band_Mandatory_Quality_Band1'    
    gname = 'HDF4_EOS:EOS_GRID:"{0}":{1}:{2}'.format(path, GRID_NAME, DATAFIELD_NAME)    

    gdset = gdal.Open(gname)
    cols = gdset.RasterXSize
    rows = gdset.RasterYSize
    geoTransform = gdset.GetGeoTransform()
    proj = gdset.GetProjection() 

    #Read example layer to test  
    result1 = gdset.ReadAsArray()     
    #print geoTransform, proj, cols, rows

    #Setting driver         
    driver = gdal.GetDriverByName('NetCDF')
    driver.Register()

    #Create new file and set parameters from original raster
    path1 = '/home/12101008/Processed/test.nc'
    outDataset = driver.Create(path1, cols, rows, 6, gdal.GDT_Int16, ['COMPRESS=PACKBITS'])            
    outDataset.SetGeoTransform(geoTransform)
    outDataset.SetProjection(proj)       

    #Write 6 raster from "result" variable
    for i in range(0,6): 
        outBand = outDataset.GetRasterBand(i+1)
        #Attempted to set band name like this but failed
        outBand.SetRasterCategoryNames(['NDVI'])
        outBand.SetNoDataValue(-10000)
        outBand.WriteArray(result1, 0, 0) 

    gdset = None
    outBand = None
    outDataset = None 





Padmanabha
  • 1,384
  • 3
  • 9
  • 19
  • Did you figure out the issue? I think SetRasterCategoryNames does not set the band name actually? – Matifou Jun 22 '17 at 00:39
  • 4
    Here: https://stackoverflow.com/questions/32609570/how-to-set-the-band-description-option-tag-of-a-geotiff-file-using-gdal-gdalw they suggest: outBand.SetDescription(BandName) I've tried it without error, but still nothing changes when I visualise my geotiff on QGIS. No updated band names. I would greatly appreciate it if someone knows a way (would prefer gdal and python) to change names in a way that QGIS recognises. There are some suggestions here: https://lists.osgeo.org/pipermail/gdal-dev/2013-October/037321.html about editing a metadata file (which I currently don't have and am not quite – Dr. E Sep 07 '18 at 14:24
  • 1
    Using the SetDescription() method as recommended by @Dr.E is what I do and it works fine for me when I visualize it in ArcGIS. Maybe there is a bug with QGIS that is worth reporting. – toferkey Dec 17 '20 at 21:39

1 Answers1

2

Please refer to the following post - How to edit the metadata for individual bands of a multiband raster, preferably with GDAL?

Call the script like this to set your band names:

    call python3 gdal_set_band_desc.py input_raster.tif 1 "Desired band 1 name" 2 "Desired band 2 name" 3 "Desired band 3 name"

The band names show up correctly in QGIS (tested with version 3.20).

Brent Edwards
  • 1,849
  • 15
  • 21
  • I've tried this solution, but, as per the name of the function, it only changes the band description, and not the band name. I can also testify to the fact that this is not a QGIS error, as I am uploading the rasters as assets to GEE and there the band name also stays the same as the original. I've also tried with QGIS plugin Rename Bands, but that also updates only the description. Does someone know how to change the band name? – Momchill Mar 15 '24 at 15:52