[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:

#BEGIN CODE
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,
eType=gdal.GetDataTypeByName('Byte'))
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:',
vrtband.DataType
print

#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()
print

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

del vrtband, vrtdataset, realdataset

#END CODE


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">
  <Metadata/>
  <VRTRasterBand dataType="Byte" band="1">
    <Metadata/>
    <ColorInterp>Gray</ColorInterp>
    <SimpleSource>
      <SourceFilename relativeToVRT="1">image.tiff</SourceFilename>
      <SourceBand>1</SourceBand>
      <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"/>
    </SimpleSource>
  </VRTRasterBand>
</VRTDataset>


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