2

I created a multiband raster using the gdal_merge.py utility where each band represents a measurement from a different year:

gdal_merge.py -o stack.tif -ot float32 -separate -a_nodata -9999 DEM_2012.tif DEM_2013.tif DEM_2015.tif DEM_2016.tif DEM_2017.tif DEM_2018.tif

However, the information about which year corresponds to which band is lost in the resulting multiband raster. Is there away to retain this information during the creation of the multiband raster? If not, how can I simply access and edit the individual band metadata after the raster is created?

4

You can use a simple python script that uses the Band.SetDescription method to set the band names:

"""
Set Band descriptions
Usage:
    python set_band_desc.py /path/to/file.ext band desc [band desc...]
Where:
    band = band number to set (starting from 1)
    desc = band description string (enclose in "double quotes" if it contains spaces)
Example:
    python set_band_desc.py /path/to/dem.tif 1 "Band 1 desc"  2 "Band 2 desc"  3 "Band 3 desc"

"""
import sys
from osgeo import gdal

def set_band_descriptions(filepath, bands):
    """
    filepath: path/virtual path/uri to raster
    bands:    ((band, description), (band, description),...)
    """
    ds = gdal.Open(filepath, gdal.GA_Update)
    for band, desc in bands:
        rb = ds.GetRasterBand(band)
        rb.SetDescription(desc)
    del ds

if __name__ == '__main__':
    filepath = sys.argv[1]
    bands = [int(i) for i in sys.argv[2::2]]
    names = sys.argv[3::2]
    set_band_descriptions(filepath, zip(bands, names))

This functionality should probably be in gdal_edit, but isn't.

For example:

gdalbuildvrt -separate test.vrt test2015.tif test2016.tif test2017.tif
    0...10...20...30...40...50...60...70...80...90...100 - done.

gdalinfo test.vrt
    Driver: VRT/Virtual Raster
    <snip...>
    Band 1 Block=128x128 Type=Byte, ColorInterp=Undefined
      Min=1.000 Max=1.000
      NoData Value=0
    Band 2 Block=128x128 Type=Byte, ColorInterp=Undefined
      Min=1.000 Max=1.000
      NoData Value=0
    Band 3 Block=128x128 Type=Byte, ColorInterp=Undefined
      Min=1.000 Max=1.000
      NoData Value=0

python set_band_desc.py test.vrt 1 2015 2 2016 3 2017

gdalinfo test.vrt
    Driver: VRT/Virtual Raster
    <snip...>Band 1 Block=128x128 Type=Byte, ColorInterp=Undefined
----> Description = 2015
      Min=1.000 Max=1.000
      NoData Value=0
    Band 2 Block=128x128 Type=Byte, ColorInterp=Undefined
----> Description = 2016
      Min=1.000 Max=1.000
      NoData Value=0
    Band 3 Block=128x128 Type=Byte, ColorInterp=Undefined
----> Description = 2017
      Min=1.000 Max=1.000
      NoData Value=0
  • I looked at the gdal_edit.py and gdalinfo docs looking for this functionality. I imagine this is a common need I'll keep this script in my toolbox, cheers. – Charlie Parr Jul 27 '18 at 20:51

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.