11

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 




    
3
  • Did you figure out the issue? I think SetRasterCategoryNames does not set the band name actually?
    – Matifou
    Jun 22 '17 at 0:39
  • 3
    Here: stackoverflow.com/questions/32609570/… 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: 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 7 '18 at 14:24
  • 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
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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