Select to view content in your preferred language

Batch processing Zonal Statistics As table

2028
2
10-09-2012 07:32 AM
StacyVentresca
Emerging Contributor
I have been able to get a stand alone script to work and output a table for each of the rasters used in the ZonalStatisticsAsTable tool. However when I made it into a geoprocessing tool so that it would accept input from a user (as opposed to the hard-coded values that were in the stand alone script) it only outputs a single INFO table from the last raster that it processed. I have not been able to figure out how to make the script tool create an output table for each of the rasters. Any help is greatly appreciated!


import arcpy, os, sys, string
from arcpy import env
from arcpy.sa import*

arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

# Get input parameters for the Zonal Statistics As Table tool
inPath = arcpy.GetParameterAsText(0)
zoneField = arcpy.GetParameterAsText(1)
rasterList = arcpy.GetParameterAsText(2).split(';')
outPath = arcpy.GetParameterAsText(3)

try:
    for raster in rasterList:    

        ZonalStatisticsAsTable(inPath, zoneField, raster, outPath)         
        arcpy.AddField_management(outPath, "RasName", "TEXT")      
        
        arcpy.CalculateField_management(outPath, "RasName", '"'+os.path.splitext(os.path.basename(raster))[0]+'"')
       
 except:
    print arcpy.GetMessages(2)
Tags (2)
0 Kudos
2 Replies
ChristopherThompson
Frequent Contributor
in your script the table you create is named according to the value in 'outPath' which is set outside the for loop. 'outPath' is given as input from the user only once and the result is that the for loop writes its output to the same table each time the loop runs, essentially overwriting the results of the last loop that was executed.

If you want to automatically create a uniquely named table then you have to provide that name inside your loop somehow. One solution might be using the value you use to populate 'rasname' in the field calculate tool (os.path.splitext(os.path.basename(raster))[0]) and use that to create the table name. You might have something like this instead (not tested, so this is just the concept for you to tinker with (for instance you might need to flesh out how the various quotations marks that delimit a text string work inside your code):
for raster in rasterList:
    tbl_name = '"'+os.path.splitext(os.path.basename(raster))[0]+'"'
    stat_table = ZonalStatisticsAsTable(inPath, zoneField, raster, outPath + '\\' + tbl_name)         
    arcpy.AddField_management(stat_table, "RasName", "TEXT")      
    arcpy.CalculateField_management(stat_table, "RasName", tbl_name)
0 Kudos
StacyVentresca
Emerging Contributor
Thanks for your reply! I have tried your suggestion but I think I'm just completely missing something. As I was looking around for a solution I found several mentions of using arcpy.SetParameterAsText() along with output parameter settings in the tool of "Derived" type.
I have been playing with this and I just don't have any idea how to use this SetParameterAsText. Any suggestions? I would love to see an entire script that illustrates how this can be used if anyone knows of one. Thanks in advance!
0 Kudos