Reading a Raster Attribute table

12-01-2013 09:54 PM
MVP Frequent Contributor
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

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?
0 Kudos
2 Replies
Esri Esteemed Contributor
Hi Neil,

I looked at your original post:

...and the fact that you can't access the RAT. This is strange. I did something similar in this thread:

... I used the da cursor to access the RAT of a list of rasters and it worked.

I tried to reproduce your error and in my case it also happened. Looking at the differences I noticed that in my case I don't supply a raster object to the cursor, but the path to the raster. In that case (apparently) arcpy has the possibility to interpret it as a "table" (at least when it's an integer raster with a RAT) ...

So change it to:

locRas1 = os.path.join(env.workspace,'FlowD')
for r in arcpy.da.SearchCursor(locRas1, ["VALUE", "COUNT"]):

Then it should work.

I think you should mention this to Esri Support, since it would be a good thing if you can use a raster object in a da cursor...

Kind regards,

0 Kudos
MVP Frequent Contributor
Thanks Xander.
My initial confusion about accessing the RAT table is functions like ListTables reveal no RATs. So its there but its not there sort of thing.
But having all the raster parameters available with a Raster object is also quite handy, without having to go through a Describe object.
Yes, I see that if the permanent Raster is supplied to the da.SearchCursor, it reads the RAT fine.
Just a slight bit of inconsistency revealed here.
0 Kudos