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
SetRasterCategoryNamesdoes not set the band name actually? – Matifou Jun 22 '17 at 00:39