How to automatically add a field directly in a .vrt?

Question:

I have a VRT file for which I would like to automatically add a color table.
I vould like to add (in python) a field ColorTablea the correct location in the file.
To be clear: I have the following lines:

<VRTDataset rasterXSize="40320" rasterYSize="14560">
  <SRS>GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]</SRS>
  <GeoTransform> -1.8000446428500001e+02,  8.9285700000000003e-03,  0.0000000000000000e+00,  6.5004464284999997e+01,  0.0000000000000000e+00, -8.9285700000000003e-03</GeoTransform>
  <Metadata>
    <MDI key="AREA_OR_POINT">Area</MDI>
  </Metadata>
  <VRTRasterBand dataType="Byte" band="1">
    <Metadata>
      <MDI key="STATISTICS_MAXIMUM">4</MDI>
      <MDI key="STATISTICS_MEAN">1.9570865680717</MDI>
      <MDI key="STATISTICS_MINIMUM">0</MDI>
      <MDI key="STATISTICS_STDDEV">1.9845596274822</MDI>
    </Metadata>
    <NoDataValue>2.55000000000000E+02</NoDataValue>
    <ColorInterp>Gray</ColorInterp>
    <SimpleSource>
      <SourceFilename relativeToVRT="1">DISCR___MC10GWW_20131221_1KM_WB_MODIS__MC10GWW_20131221_1KM_WB_V23.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="40320" RasterYSize="14560" DataType="Byte" BlockXSize="40320" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="40320" ySize="14560" />
      <DstRect xOff="0" yOff="0" xSize="40320" ySize="14560" />
    </SimpleSource>
  </VRTRasterBand>
</VRTDataset>

and I would like to have the following:

    <VRTDataset rasterXSize="40320" rasterYSize="14560">
      <SRS>GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]</SRS>
      <GeoTransform> -1.8000446428500001e+02,  8.9285700000000003e-03,  0.0000000000000000e+00,  6.5004464284999997e+01,  0.0000000000000000e+00, -8.9285700000000003e-03</GeoTransform>
      <Metadata>
        <MDI key="AREA_OR_POINT">Area</MDI>
      </Metadata>
      <VRTRasterBand dataType="Byte" band="1">
        <Metadata>
          <MDI key="STATISTICS_MAXIMUM">4</MDI>
          <MDI key="STATISTICS_MEAN">1.9570865680717</MDI>
          <MDI key="STATISTICS_MINIMUM">0</MDI>
          <MDI key="STATISTICS_STDDEV">1.9845596274822</MDI>
        </Metadata>
        <NoDataValue>2.55000000000000E+02</NoDataValue>
 <ColorInterp>Palette</ColorInterp>
     <ColorTable>
      <Entry c1="255" c2="255" c3="255" c4="255"/>
      <Entry c1="0" c2="0" c3="255" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="223" c2="115" c3="255" c4="255"/>
       <Entry c1="255" c2="255" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
    </ColorTable>
 <SimpleSource>
          <SourceFilename relativeToVRT="1">DISCR___MC10GWW_20131221_1KM_WB_MODIS__MC10GWW_20131221_1KM_WB_V23.tif</SourceFilename>
          <SourceBand>1</SourceBand>
          <SourceProperties RasterXSize="40320" RasterYSize="14560" DataType="Byte" BlockXSize="40320" BlockYSize="1" />
          <SrcRect xOff="0" yOff="0" xSize="40320" ySize="14560" />
          <DstRect xOff="0" yOff="0" xSize="40320" ySize="14560" />
        </SimpleSource>
      </VRTRasterBand>
    </VRTDataset>
Asked By: Wraf

||

Answers:

Have you tried the VRT format tutorial at this link <Link>? Or looked at the VRT Raster Band documentation at this link <Link>? I think you should be able to use AddColorTable

Answered By: khafen

I finally found the solution with the following function:

def Modify( filename , Color , Category ):
        dataset = osgeo.gdal.Open( filename , GA_Update )
        if dataset is None:
            print 'ERROR: gdal.Open( %(filename)s )' % var
            sys.exit( 1 )
        band = dataset.GetRasterBand( 1 )
        band.SetRasterColorInterpretation( GCI_PaletteIndex )
        band.SetRasterColorTable( Color )
        band.SetRasterCategoryNames( Category )
Answered By: Wraf
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.