This is related to the question at Different results from GDAL raster attribute table command on mac and windows

Now, I am not able to get the raster attribute table of the same file: https://www.dropbox.com/s/fmomfpro3j6dmz8/Shift_cult_past_current_prediction_20160107_gr1DD.tif?dl=0 on windows. I am using python 3.6 and gdal version 2.1.3. The code is as follows:

import gdal
dataset = gdal.Open(dataset_uri) # dataset_uri is path to .tif file
band = dataset.GetRasterBand(1)
rat = band.GetDefaultRAT()


how to get the raster attribute table usingpython 3.6 and gdal 2.1.3

  • What is the question? – Logan Byers Feb 5 '17 at 18:12
  • thanks @LoganByers, the question is how to get the raster attribute table usingpython 3.6 and gdal 2.1.3. Sorry, I should have been clearer – user1186 Feb 5 '17 at 19:29

try this:

# create a Raster Attribute Table(RAT), then create needed columns

rat = gdal.RasterAttributeTable()
rat.CreateColumn("Value", gdalconst.GFT_Integer, gdalconst.GFU_MinMax)
rat.CreateColumn("Count", gdalconst.GFT_Integer, dalconst.GFU_PixelCount)
rat.CreateColumn("Red", gdalconst.GFT_Integer, gdalconst.GFU_Red)
rat.CreateColumn("Green", gdalconst.GFT_Integer, gdalconst.GFU_Blue)
rat.CreateColumn("Blue", gdalconst.GFT_Integer, gdalconst.GFU_Red)
rat.CreateColumn("Class_Name", gdalconst.GFT_String, gdalconst.GFU_Name)

# populate the columns
for i in range(256):
    stringValue = str(i)
    intValue = int(i)
    bandhist2 = bandhist[3][intValue]

    rat.SetValueAsInt(intValue, 0, intValue)
    rat.SetValueAsInt(intValue, 1, bandhist2)
    rat.SetValueAsInt(intValue, 2, redDict[stringValue])
    rat.SetValueAsInt(intValue, 3, greenDict[stringValue])
    rat.SetValueAsInt(intValue, 4, blueDict[stringValue])
    rat.SetValueAsString(intValue, 5, classDict[stringValue])

# set the default Raster Attribute Table for src_ds band 1 to the newly modified rat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.