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
# 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, "","","")

znshp = r"C:\Users\Documents\Projects\shapefile_folder"
grd = r"C:\Users\Documents\Projects\raster_folder"
tbl = r"C:\Users\Documents\Projects\Table_folder"
fieldname1= "Year"
fieldname2= "Month"
Template = r"C:\Users\Documents\Projects\Tables\Template.dbf"

print "starting zonal SWE"
zonal_SWE(grd, znshp)
print "done"
  • Welcome to GIS SE! As a new user be sure to take the Tour to learn about our focussed Q&A format. Have you reviewed gis.stackexchange.com/questions/tagged/error-000865?sort=votes ? – PolyGeo Jan 7 '18 at 22:20
  • My first impressions: You need to provide the full path to the raster as you've changed workspace to the shapefile workspace. Also, your out_tbl isn't likely to be valid, it lacks an extension and is therefore an INFO table which has a maximum file name length of 13 characters and cannot contain a '.' which is probable from your ListRasters. consider changing your output table to a .DBF; you aren't using your shape name in the output name so each iteration will overwrite the previous. You haven't parameter or global tbl, fieldname1, fieldname2 into your procedure as so you can't use them. – Michael Stimson Jan 8 '18 at 0:07

Your code is flawed, you get the list of rasters by setting then workspace, you then get the list of shapefiles by resetting the workspace to znshp. You then reference each raster in rasterlist. This list will hold just the name and not the full path. The ZonalStatisticsAsTable tool honours workspace but as you have pointed that to where the shapefiles are it's not surprising it cannot find the rasters!

I would recommend constructing the full path to the raster by concatenating rasters with raster and that will be a correct input parameter for the tool.

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.