Why does GDAL’s ‘GetDescription’ function return an empty string

bandgdalosgeoraster

I have used GDAL to add descriptions to my raster band. I have verified in QGIS that the bands are indeed correctly named. I use the following function for adding names as band descriptions, which has a .txt file as input with the names separated by a comma:

def set_band_descriptions(raster_fpath: str, bands_txt: str) -> None:
    """Sets descriptions of raster bands using a .txt file.

    Args:
        raster_fpath (str): Filepath of raster.
        bands_txt (str): Filepath of .txt file with band names.
    """
    # Open raster using GDAL
    ds = gdal.Open(raster_fpath, gdal.GA_Update)

    # Open .txt file
    band_file = open(bands_txt, "r")

    # Splitting the content by comma
    bands = band_file.read().split(sep = ",")

    # Loop through all bands
    for band in range(1, len(bands)+1):
        rb = ds.GetRasterBand(band)
        rb.SetDescription(bands[band - 1])

    # Delete dataset    
    del ds

In another part of my script, I need to read the raster band description. This is my, very straightforward, code for doing so:

from osgeo import gdal
    
dataset = gdal.Open(raster_fpath) 
band = dataset.GetRasterBand(1)
description = band.GetDescription()
print(description)

The print function returns an empty string. How do I correctly get the band description of a GeoTIFF file, preferably using GDAL?

Best Answer

As far as I see from the code, the problem might be in the way the raster dataset is closed in set_band_descriptions(). The GDAL Raster API tutorial suggests not to call the del operator on raster data sets:

Please keep in mind that GDALRasterBand objects are owned by their dataset, and they should never be destroyed with the C++ delete operator

Instead, the recommended way to close the raster data set is to set it to None. The following code excerpt presents 2 functions that, respectively, display and set a description for a given band of a raster file

def display_raster_band_description(ras_file=None, n_band=None):
    """  Open a raster file and get the description for n_band  """
    gdal_ras = gdal.Open(file, gdal.GA_ReadOnly)
    gdal_ras_band = gdal_ras.GetRasterBand(n_band)
    if not gdal_ras_band.GetDescription():
        print(f"Raster {ras_file} has no description for band {n_band}")
    else:
        print(f"Raster {ras_file} - band {n_band} description: {gdal_ras_band.GetDescription()}")
    gdal_ras = None


def set_raster_band_description(ras_file=None, n_band=None, s_description=None):
    """  Sets s_description to n_band of ras_file  """
    gdal_ras = gdal.Open(file, gdal.GA_Update)
    gdal_ras_band = gdal_ras.GetRasterBand(n_band)
    assert not gdal_ras_band.GetDescription(), f"The raster band {n_band} already has a description"
    gdal_ras_band.SetDescription(s_description)
    assert gdal_ras_band.GetDescription(), f"The description was not set on raster band {n_band}"
    gdal_ras = None

Executing the following code on an example file, which originally had no description in the raster band, gives the following output:

file = r"/.../N47E009_low_res_copy.tif"

display_raster_band_description(file, 1)
Raster /.../N47E009_low_res_copy.tif has no description for band 1

set_raster_band_description(file, 1, "This is a simple test")

display_raster_band_description(file, 1)
Raster /.../N47E009_low_res_copy.tif - band 1 description: This is a simple test
Related Question