GSCUser85

Reading a Raster Attribute table

Discussion created by GSCUser85 Champion on Dec 1, 2013
Latest reply on Dec 2, 2013 by GSCUser85
Last week I answered a query about Null / Not Null counts in a raster.
As an aside there I asked about accessing the RAT from a raster.
After a bit of searching I have found the solution, its not quite what I expected.
Import the modules, navigate to my gdb containing the rasters, create a raster object "Ras1" and validate that it does indeed have a RAT.
>>> import sys, os, arcpy
>>> from arcpy import env
>>> env.workspace = "c:/Data/Python/NumPyArrays/ZimDem.gdb"
>>> arcpy.ListRasters()
[u'DemFill', u'FlowDir', u'DemFill_hs', u'Ras2', u'Ras3', u'Dem', u'Dem_hs', u'FlowD', u'Ras', u'FlowD_Temp', u'Dem_MaxUpstreamElevation', u'Temp', u'Dem_DD', u'CreateConsta1', u'XMapFinal', u'YMapFinal']
>>> arcpy.ListTables()
[]
>>> 
>>> Ras1 = arcpy.Raster("FlowD")
>>> Ras1.hasRAT
True


Try to use da.SearchCursor to list the RAT.
for r in arcpy.da.SearchCursor(Ras1, ["VALUE", "COUNT"]):
 print r[0], r[1]

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    for r in arcpy.da.SearchCursor(Ras1, ["VALUE", "COUNT"]):
RuntimeError: 'in_table' is not a table or a featureclass

So the da.SearchCursor method, errors out because Ras1 is a raster object, not a table. Fair enough you might think.
However, then try the "old" SearchCursor...
rows = arcpy.SearchCursor(Ras1, "", "", "VALUE; COUNT")
>>> for r in rows:
 v = r.getValue("VALUE")
 c = r.getValue("COUNT")
 print v, c

 
2 4254.0
4 4093.0
8 1572.0
16 3615.0
32 4112.0
64 9199.0
128 6344.0
>>> 

So the old SearchCursor doesn't mind that the input is a raster object, and lists the Value; Count list no probs...
How odd is that?
Cheers,
Neil

Outcomes