I am a beginner to python coding in GIS. I want to calculate the total number of counts in raster attribute table. In order to do that:

  1. I would like to build a attribute table of raster data
  2. use a loop through attribute table using ARCPY.SearchCursor()

Therefore, I wrote a code to build an attribute table. However, I am getting an error while running the code and not able to figure out the error in the code.


import arcpy
# Create the geoprocessing object 
import arcgisscripting
gp = arcgisscripting.create()

#To overwrite output
arcpy.env.overwriteOutput = True

#Set environment settings
gp.workspace = "C:/Subhasis/Test/raster-read"

#Build Raster Attribute Table
gp.BuildRasterAttributeTable_management("sti-9", "Overwrite")

Error Message:
Traceback (most recent call last):
  File "C:/Subhasis/Test/raster-read/test.py", line 15, in <module>
    gp.BuildRasterAttributeTable_management("sti-9", "Overwrite")
ExecuteError: ERROR 000049: Failed to build attribute table
Failed to execute (BuildRasterAttributeTable).
  • I am using ArcGIS 10.2 – Inception Jul 14 '14 at 14:02
  • 1
    First of all avoid special characters like - or & or spaces in folder and file name. Secondly you can only build attribute tables for rasters that are INTEGER. So if you have a FLOAT raster this tool will fail – Hornbydd Jul 14 '14 at 14:06
  • Thank you very much1! I want ask yo stupid question. In order to check whether my raster is an INTEGER OR FLOAT, i looked at the layer properties and found that pixel type is unsigned integer. Is this the right way to check whether the raster is integer or float? – Inception Jul 14 '14 at 14:15
  • An unsigned integer is an INTEGER raster, so that sounds OK. I would suggest removing those special characters from your folder and file name. – Hornbydd Jul 14 '14 at 14:17
  • I checked the special character and ran the code and got the below message :Build Raster Attribute Table example failed. Executing: BuildRasterAttributeTable sti9 NONE. Thanks! – Inception Jul 14 '14 at 14:40

You appear to be mixing different geoprocessors, you do not need to use the arcgiscripting. Try this code, replacing the folder and file names as appropriate

import arcpy

#Set environment settings
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/Temp"

#Build Raster Attribute Table
arcpy.BuildRasterAttributeTable_management("myRaster", "Overwrite")
  • Thank you very much! Sorry for bothering you! I ran your code and got the message below:Traceback (most recent call last): File "C:/Subhasis/Test/raster-read/test.py", line 10, in <module> arcpy.BuildRasterAttributeTable_management("sti9", "Overwrite") File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 13214, in BuildRasterAttributeTable raise e ExecuteError: ERROR 000049: Failed to build attribute table Failed to execute (BuildRasterAttributeTable). – Inception Jul 14 '14 at 14:55
  • Hmmm... At this point the only idea I have left is to ask if it's file locked by another application or running process? As the code above works for me. – Hornbydd Jul 14 '14 at 15:28
  • Hi Just a quick question, I have the attribute table for raster and using the arcpy.searchcursor to read the attribute table. When I ran the program, I didn't get any error message. But I didn't see the "count" for each row in my "Python shell" window. Any thoughts..Providing code in the next comment: – Inception Jul 14 '14 at 17:16
  • import arcpy #To overwrite output arcpy.env.overwriteOutput = True #Set environment settings arcpy.env.workspace = "C:/Subhasis/Test/raster-read" #Build Raster Attribute Table arcpy.BuildRasterAttributeTable_management("sti-9", "Overwrite") # loop through attribute table Rows = arcpy.SearchCursor( "sti-9" ) for row in Rows: count = row.getValue( "COUNT" ) print count – Inception Jul 14 '14 at 17:16

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.