I am pretty new to coding.
I have to execute zonal statistics on many shapefiles (~20) for many rasters (~300). Essentially, the first shapefile is used to process all zonal statistics for all rasters and merge it to one table. This is then repeated for the second shapefile and so on. The code also creates two new fields (year and date) and populates them from the raster name. The rasters are .tif file formats.
I keep running into error 000865: Parameters are not valid. Input value raster does not exist. Failed to execute (ZonalStatisticsAsTable).
My code so far looks like:
# Import system modules
import arcpy, sys, os, string, glob
print "imported modules"
# Check out licenses
arcpy.CheckOutExtension("spatial")
# Overwrite
arcpy.env.overwriteOutput = True
def zonal_SWE(rasters, shapes):
arcpy.env.workspace = rasters
rasterlist = arcpy.ListRasters()
arcpy.env.workspace = shapes
shplist = arcpy.ListFeatureClasses()
for raster in rasterlist:
for i in shplist:
out_tbl = tbl + "\\" + raster + "_ZStats"
print 'Zonal Statistics based on SWE : ' + raster[9:13]
# Preform zonal statistics on the polygon file within the variable 'znlyr'
z = arcpy.gp.ZonalStatisticsAsTable_sa(i, InZnValFld, raster, out_tbl, "DATA", "MIN_MAX_MEAN")
# Create a new field to add the filename information
arcpy.AddField_management(z, fieldname1, "DOUBLE")
# Create a new field to add the filename information
arcpy.AddField_management(z, fieldname2, "DOUBLE")
# Add the Year value from the filename to the new field
arcpy.CalculateField_management(z, fieldname1,raster[9:13])
# Add the Month value from the filename to the new field
arcpy.CalculateField_management(z, fieldname2,raster[13:15])
arcpy.Append_management(z, Template, "","","")
#----------------variables--------------
znshp = r"C:\Users\Documents\Projects\shapefile_folder"
grd = r"C:\Users\Documents\Projects\raster_folder"
tbl = r"C:\Users\Documents\Projects\Table_folder"
InZnValFld = "OBJECTID"
fieldname1= "Year"
fieldname2= "Month"
Template = r"C:\Users\Documents\Projects\Tables\Template.dbf"
#---------------------------------------
print "starting zonal SWE"
zonal_SWE(grd, znshp)
print "done"