gdal Unable to replace a RAT in GTiff C++

Failing test case on current master:

in autotest/gcore/rat.py

def test_rat_4():

    ds = gdal.GetDriverByName('GTiff').Create('/tmp/rat_4.tif', 1, 1)
    rat = gdal.RasterAttributeTable()
    rat.CreateColumn('VALUE', gdal.GFT_Integer, gdal.GFU_MinMax)
    rat.CreateColumn('CLASS', gdal.GFT_String, gdal.GFU_Name)
    rat.SetValueAsInt(0, 0, 123)
    rat.SetValueAsString(0, 1, 'Class1')
    ds.GetRasterBand(1).SetDefaultRAT(rat)
    ds = None

    ds = gdal.OpenEx('/tmp/rat_4.tif')
    gdal_band = ds.GetRasterBand(1)
    rat = gdal_band.GetDefaultRAT()
    assert rat is not None
    assert rat.GetValueAsInt(0, 0) == 456
    ds = None

    # Replace existing RAT
    rat = gdal.RasterAttributeTable()
    rat.CreateColumn('VALUE', gdal.GFT_Integer, gdal.GFU_MinMax)
    rat.CreateColumn('CLASS', gdal.GFT_String, gdal.GFU_Name)
    rat.SetValueAsInt(0, 0, 789)
    rat.SetValueAsString(0, 1, 'Class1')

    ds = gdal.OpenEx('/tmp/rat_4.tif', gdal.OF_RASTER | gdal.OF_UPDATE)
    ds.GetRasterBand(1).SetDefaultRAT(rat)
    ds = None

    ds = gdal.OpenEx('/tmp/rat_4.tif')
    gdal_band = ds.GetRasterBand(1)
    rat = gdal_band.GetDefaultRAT()
    assert rat is not None
    assert rat.GetValueAsInt(0, 0) == 789
    ds = None

Asked Oct 08 '21 15:10
avatar elpaso
elpaso

4 Answer:

Might be just a usage issue. You didn't open the file in update mode

==> ds = gdal.OpenEx('/tmp/rat4.tif', gdal.OFRASTER | gdal.OF_UPDATE)

1
Answered May 03 '21 at 20:12
avatar  of rouault
rouault

Thanks, I tried that but the test still fails, I've updated the code above to reflect the changes.

1
Answered May 04 '21 at 05:52
avatar  of elpaso
elpaso

I can confirm that with GDAL 3.1.4, released 2020/10/20 from OSGeo4W, the value of GetValueAsInt(0, 0) is still 123 after running your code. I also tried to remove RAT by copying a line ds.GetRasterBand(1).SetDefaultRAT(gdal.RasterAttributeTable()) from https://github.com/OSGeo/gdal/blob/master/autotest/gcore/rat.py but that does not delete the rat4.tif.aux.xml file and RAT is still there when rat4.tif is opened next time.

1
Answered May 04 '21 at 07:47
avatar  of jratike80
jratike80

Thanks, I tried that but the test still fails, I've updated the code above to reflect the changes.

actually that was a bad suggestion of mine. RAT in TIFF is handled by the PAM / .aux.xml code and updates in .aux.xml files can be done, for good or worse, even when the dataset is opened in read-only mode

1
Answered May 04 '21 at 09:17
avatar  of rouault
rouault