[gdal-dev] Modify virtual file attributes

Luca Fasano lucafasano at interfree.it
Wed May 19 05:20:20 EDT 2010

Hi list,
I'm encountering some problems using Python Gdal API to modify *virtual*
file attributes.

In fact if I change the "Description" and "CategoryNames" attributes of
a virtual band with the Python API, the Python object is updated
correctly, but *not* the virtual file that mantains old values.
In addition if I try to change the "DataType" attribute of virtual band
the Python object (and obviously the virtual file) is *not* updated.
Are these considerable as bugs?

I also want to underline that the Python API misses some methods to
modify some virtual file attributes, for example "UnitType", "Offset"
and "Scale " of virtual bands or a set of methods to build virtual file
"sources" (SimpleSource, KernelFilteredSource, ...) without modifying
*manually* their XML code before adding them to bands using the specific
gdal.Band.SetMetadata() call.

Here is a simple example showing the difficult I encountered to modify
virtual band attributes:

from osgeo import gdal

realfname = 'image.tiff'
vrtfilename = 'vrtdataset.vrt'

#open real dataset and create copy as a vrt file
vrt_driver = gdal.GetDriverByName('VRT')
gtiff_driver = gdal.GetDriverByName('GTIFF')

realdataset = gtiff_driver.Create(
            'image.tiff', 100, 100, 1,
vrtdataset = vrt_driver.CreateCopy(vrtfilename, realdataset)
vrtband = vrtdataset.GetRasterBand(1)

#try to modify DataType
print 'datatype before modify:', vrtband.DataType
newdatatype_code = vrtband.DataType = gdal.GetDataTypeByName('Float32')
print 'new datatype code should be:', newdatatype_code
vrtband.DataType = newdatatype_code
print '...but new datatype code retrieved from band is:',

#try to modify Description
print 'Band description before change is:', vrtband.GetDescription()
vrtband.SetDescription('New Description for band')
print 'Band description after change is:', vrtband.GetDescription()

#try to modify CategoryNames
print 'Band category names before change are:',
vrtband.SetRasterCategoryNames(['Category A', 'Category B'])
print 'Band category names after change are:',

del vrtband, vrtdataset, realdataset


The output is:
fasano at fasano-debian:~/Desktop/test$ python gdalvrt.py 
datatype before modify: 1
new datatype code should be: 6
...but new datatype code retrieved from band is: 1

Band description before change is: 
Band description after change is: New Description for band

Band category names before change are: None
Band category names after change are: ['Category A', 'Category B']

The virtual file after the Python script execution results as following:

fasano at fasano-debian:~/Desktop/test$ cat vrtdataset.vrt 
<VRTDataset rasterXSize="100" rasterYSize="100">
  <VRTRasterBand dataType="Byte" band="1">
      <SourceFilename relativeToVRT="1">image.tiff</SourceFilename>
      <SourceProperties RasterXSize="100" RasterYSize="100"
DataType="Byte" BlockXSize="100" BlockYSize="81"/>
      <SrcRect xOff="0" yOff="0" xSize="100" ySize="100"/>
      <DstRect xOff="0" yOff="0" xSize="100" ySize="100"/>

Note that are not present neither "Description" nor "CategoryNames"
attributes, and "DataType" has the initial value (Byte=1) even after the
Python script execution.

More information about the gdal-dev mailing list