bhutaaa

ZonalStats as Table...

Discussion created by bhutaaa on Aug 23, 2011
Latest reply on Aug 4, 2012 by zarlashtm
Hello all,

I'm trying to work in Python (I'm a total newbie and I don't know what I'm doing).  I found some old Python code posted on a ArcGIS Script page and I modified it so I can use it in ArcGIS 9.3.1. 

I'm having some problems however and I can't get it to run right.  First, I can't get the code to read Any suggestions would be greatly appreciated.  I need to get this running as I have to apply it to about 4500 rasters that I generated.  The original code and modified codes are posted below:

The questions I have are:1) does that code to do the zonal stats as table in the "try:" section need to be modified to ArcGIS 9.3.1 "standards" so it will work and 2) is the code in the "parameters" right?

Original Code from David Coley 5/10/06
# Create the Geoprocessor object
import win32com.client, sys, os, string

gp = win32com.client.Dispatch("esriGeoprocessing.GPDispatch.1")
# Check out Spatial Analyst extension license
gp.CheckOutExtension("Spatial")
gp.CellSize = 10 #can set as argument
gp.Overwrite = 1

try:
    InputWorkspace = sys.argv[1]#gp.Workspace = "F:/coleyd/FloodZoneGDB" #can set as argumment
    InZonalLayer = sys.argv[2]
    InZonalValueField = sys.argv[3]#"PIN" #can set as argumment
    #set output workspace which holds tables in a gdb
    OutputWorkspace = sys.argv[4]

    #Get the raster datasets in the input workspace and loop through them from the start
    InputRasters = gp.ListRasters()
    InputRasters.reset()
    InputRaster = InputRasters.next()

    while InputRaster:

        outTables = OutputWorkspace + "\\" + InputRaster + "ZonalStats"       
       
        # Process: Zonal Statistics as Table

        gp.ZonalStatisticsAsTable_sa(InZonalLayer, InZonalValueField, InputRaster, outTables, "DATA")
        # print InputRaster
        InputRaster = InputRasters.next()
except:
    gp.AddMessage(gp.GetMessages(2))
    print gp.GetMessages(2)



My code 2011":

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = 1

# Parameters
#set the inputworkspace
gp.workspace = sys.argv[1]
gp.AddMessage ("input workspace: " + workspace)
#WorkSpace = "C:\test\ppt_study_counties"
OutputWorkspace = sys.argv[2]
gp.AddMessage ("output workspace: " + output_workspace)
#OutWorkSpace = "C:\test\zonal_stats_ppt_counties"
InZonalLayer = sys.argv[3]
#InZonalLayer = "C:\Bhuta_Dissertation\GIS\OUTPUT_DATA\COUNTIES\STUDY_COUNTIES.shp"
InZonalValueField = sys.argv[4]
#InZonalValueField = "COUNTY"

try:
    #Get the raster datasets in the input workspace and loop through them from the start
    InputRasters = gp.ListRasters()
    InputRasters.reset()
    InputRaster = InputRasters.next()

    while InputRaster:

        #Set the output table name
        outTables = OutputWorkspace + "\\" + InputRaster + "ZonalStats" 
        #outTables = OutWorkSpace + "C:\test\zonal_stats_ppt_counties" + InputRaster + "ZonalStats"       

        # Check out Spatial Analyst extension license
        gp.CheckOutExtension("Spatial")

        # Load required toolboxes.  You may need to alter the paths if you are using a 64 bit OS
        gp.AddToolbox("C:\Program Files (x86)\ArcGIS\ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx")

        # Process: Zonal Statistics as Table
        cellSize = 1
       
        gp.ZonalStatisticsAsTable_sa(InZonalLayer, InZonalValueField, InputRaster, outTables, "DATA")
        # print InputRaster
        InputRaster = InputRasters.next()
except:
    gp.AddMessage(gp.GetMessages(2))
    print gp.GetMessages(2)

Outcomes