I want to write a raster file (using GDAL+Python) with multiply bands. However, I do not know how to named bands. Their names are always "Band 1", "Band 2", etc. I search 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 that 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')

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

        #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.WriteArray(result1, 0, 0) 

        gdset = None
        outBand = None
        outDataset = None 
  • Did you figure out the issue? I think SetRasterCategoryNames does not set the band name actually? – Matifou Jun 22 '17 at 0:39
  • 1
    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

Your Answer

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

Browse other questions tagged or ask your own question.