7

Does anybody know how to change or set the "Description" option/tag of a GeoTIFF file using GDAL?

To specify what I mean, this is an example of gdalinfo return from a GeoTIFF file with set "Description":

 Band 1 Block=64x64 Type=UInt16, ColorInterp=Undefined
 Description = AVHRR Channel 1:  0.58  micrometers -- 0.68 micrometers
 Min=0.000 Max=814.000 
 Minimum=0.000, Maximum=814.000, Mean=113.177, StdDev=152.897
 Metadata:
    LAYER_TYPE=athematic
    STATISTICS_MAXIMUM=814
    STATISTICS_MEAN=113.17657236931
    STATISTICS_MINIMUM=0
    STATISTICS_STDDEV=152.89720574652

In the example you can see: Description = AVHRR Channel 1: 0.58 micrometers -- 0.68 micrometers

How do I set this parameter using GDAL?

Generic Wevers
  • 570
  • 6
  • 10

4 Answers4

9

In Python you can set the band description like this:

from osgeo import gdal, osr
import numpy

# Define output image name, size and projection info:
OutputImage = 'test.tif'
SizeX = 20
SizeY = 20
CellSize = 1
X_Min = 563220.0
Y_Max = 699110.0
N_Bands = 10
srs = osr.SpatialReference()
srs.ImportFromEPSG(2157)
srs = srs.ExportToWkt()
GeoTransform = (X_Min, CellSize, 0, Y_Max, 0, -CellSize)

# Create the output image:
Driver = gdal.GetDriverByName('GTiff')
Raster = Driver.Create(OutputImage, SizeX, SizeY, N_Bands, 2) # Datatype = 2 same as gdal.GDT_UInt16
Raster.SetProjection(srs)
Raster.SetGeoTransform(GeoTransform)

# Iterate over each band
for band in range(N_Bands):
    BandNumber = band + 1
    BandName = 'SomeBandName '+ str(BandNumber).zfill(3)
    RasterBand = Raster.GetRasterBand(BandNumber)
    RasterBand.SetNoDataValue(0)
    RasterBand.SetDescription(BandName) # This sets the band name!
    RasterBand.WriteArray(numpy.ones((SizeX, SizeY)))

# close the output image
Raster = None
print("Done.")

Unfortunately, I'm not sure if ArcGIS or QGIS are able to read the band descriptions. However, the band names are clearly visible in Tuiview: enter image description here

  • Is band specific metadata a GeoTiff extension, or part of Tiff regularly? – CMCDragonkai Mar 23 '18 at 05:53
  • It is included in GeoTiff metadata. If you use the command "gdalinfo -norat *.tif", it will return the description of each band in your image. I have tested this on a Landsat 8 image and it returns the band names that I have set (e.g. "Red", "Green", "Blue", "NIR") instead of just Band1, Band2, Band3, Band4...etc. –  Apr 14 '18 at 10:59
  • 1
    Thanks @Osian, that's the perfect solution. – Generic Wevers Feb 05 '20 at 11:01
2

GDAL includes a python application called gdal_edit.py which can be used to modify the metadata of a file in place. I am not familiar with the Description field you are referring to, but this tool should be the one to use.

Here is the man page: gdal_edit.py

Here is an example script using an ortho-image I downloaded from the USGS Earth-Explorer.

#!/bin/sh

#  Image to modify
IMAGE_PATH='11skd505395.tif'

#  Field to modify
IMAGE_FIELD='TIFFTAG_IMAGEDESCRIPTION'

# Print the tiff image description tag
gdalinfo $IMAGE_PATH | grep $IMAGE_FIELD

#  Change the Field
CMD="gdal_edit.py -mo ${IMAGE_FIELD}='Lake-Tahoe' $IMAGE_PATH"
echo $CMD
$CMD

#  Print the new field value
gdalinfo $IMAGE_PATH | grep $IMAGE_FIELD

Output

$ ./gdal-script.py 
TIFFTAG_IMAGEDESCRIPTION=OrthoVista
gdal_edit.py -mo TIFFTAG_IMAGEDESCRIPTION='Lake-Tahoe' 11skd505395.tif
TIFFTAG_IMAGEDESCRIPTION='Lake-Tahoe'

Here is another link that should provide useful info.

https://gis.stackexchange.com/questions/111610/how-to-overwrite-metadata-in-a-tif-file-with-gdal

Community
  • 1
  • 1
msmith81886
  • 2,286
  • 2
  • 20
  • 27
0

Here's a single purpose python commandline script to edit band description in place.

''' Set image band description to specified text'''
import os
import sys
from osgeo import gdal

gdal.UseExceptions()

if len(sys.argv) < 4:
    print(f"Usage: {sys.argv[0]} [in_file] [band#] [text]")
    sys.exit(1)

infile = sys.argv[1]        # source filename and path
inband = int(sys.argv[2])   # source band number
descrip = sys.argv[3]        # description text

data_in = gdal.Open(infile, gdal.GA_Update)
band_in = data_in.GetRasterBand(inband)
old_descrip = band_in.GetDescription()
band_in.SetDescription(descrip)
new_descrip = band_in.GetDescription()

# de-reference the datasets, which triggers gdal to save
data_in = None
data_out = None

print(f"Description was: {old_descrip}")
print(f"Description now: {new_descrip}")

In use:

$ python scripts\gdal-edit-band-desc.py test-edit.tif 1 "Red please"
Description was:
Description now: Red please

$ gdal-edit-band-desc test-edit.tif 1 "Red please also"

$ python t:\ENV.558\scripts\gdal-edit-band-desc.py test-edit.tif 1 "Red please also"
Description was: Red please
Description now: Red please also

Properly it should be added to gdal_edit.py but I don't know enough do feel safe adding it directly.

matt wilkie
  • 17,268
  • 24
  • 80
  • 115
-1

gdal_edit.py with the -mo flag can be used to edit the band descriptions, with the bands numbered starting from 1:

gdal_edit.py -mo BAND_1=AVHRR_Channel_1_p58_p68_um -mo BAND_2=AVHRR_Channel_2 avhrr.tif

I didn't try it with the special characters but that might work if you use the right quotes.

  • While this code may solve the question, [including an explanation](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion – Muhammad Dyas Yaskur Jan 30 '20 at 01:01
  • 4
    This does not achieve what I was asking for. It only adds additional general metadata with names defined by you but not adds descriptions to the single band properties. – Generic Wevers Feb 05 '20 at 11:03
  • This is misleading; it does not add metadata to the bands. – metasim Oct 25 '22 at 13:23