1

I'm trying to write a small script loop script which retrieves rasters from each folder, performs zonal statistics using watershed polygons and saves it as a table. I've written similar loop scripts, but this seems to be confusing me. Here's what I have so far:

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "F:/lu_crop_rast"
arcpy.env.overwriteoutput = 1

watershedFeat = "F:/watersheds.shp"
outDir = "F:/lu_crop_rast/tables/"

for raster in arcpy.ListRasters():
    outTable = outDir + raster + "_TBL.dbf"
    arcpy.sa.ZonalStatisticsAsTable(watershedFeat,"Name",raster,outTable,"NODATA","MEAN")

This is the error I'm getting:

Runtime error  Traceback (most recent call last):   
File "<string>", line 15, in <module>   File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Functions.py", line 6232, in ZonalStatisticsAsTable     statistics_type)   
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Utils.py", line 53, in swapper     result = wrapper(*args, **kwargs)   File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Functions.py", line 6224, in Wrapper     statistics_type)   
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\geoprocessing\_base.py", line 504, in <lambda>     return lambda *args: val(*gp_fixargs(args, True)) ExecuteError: ERROR 999999: Error executing function. Create output table failed Failed to execute (ZonalStatisticsAsTable). 

I've checked and my rasters and shapefile aren't corrupt. I've looked at this thread as well and made the following changes:

arcpy.gp.ZonalStatisticsAsTable_sa(watershedFeat,"Name",raster,outTable,"NODATA","MEAN")

But so far nothing seems to work.

4
  • Try fixing your indentations in the for loop first and see if you get the same error (need to dedent) Aug 10, 2016 at 20:19
  • I think the indentation was just a matter of copying/pasting. Besides, over-indenting is actually accepted by Python; it doesn't throw an error--and if it did, it wouldn't throw an error within the geoprocessing function.
    – Tom
    Aug 10, 2016 at 20:23
  • Depending on the data types of your input rasters, this could be failing because you are assigning a second extension.
    – Tom
    Aug 10, 2016 at 20:25
  • Yes, because I ended up manually performing zonal stats on some of the rasters to check if that was the issue. The output table had valid entries.
    – asigethi
    Aug 10, 2016 at 21:13

1 Answer 1

2

You need to dedent and modify your naming (thanks Tom). Try this:

import arcpy, os
arcpy.env.overwriteOutput = True

-

for raster in arcpy.ListRasters():
    raster_name = os.path.basename(raster).rstrip(os.path.splitext(raster)[1])
    outTable = outDir + raster_name + "_TBL.dbf"
    arcpy.gp.ZonalStatisticsAsTable(watershedFeat,"Name",raster,outTable,"NODATA","MEAN")
4
  • Yes, Tom is correct. The over-indenting is because of copying and pasting the code. It is now fixed.
    – asigethi
    Aug 10, 2016 at 20:32
  • I updated my answer - try the code above Aug 10, 2016 at 20:32
  • Thanks! This runs the loop successfully. It is likely an issue with the ".tif" file extension. Now, I'm running into another problem where zonal stats aren't being calculated for some of the individual watersheds. Would setting the extent and mask solve this issue?
    – asigethi
    Aug 10, 2016 at 20:44
  • It wouldn't hurt to set your environments, but I suspect that's not the problem. Are you sure there are raster data present for each watershed? Aug 10, 2016 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.