0

I want to calculate zonal statistics as table for multiple rasters, but can't figure out how. What I am trying is:

import arcpy, os
arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\Indices\Main\NDWI\main_master'
#zones to calculate stats on
inpoly=r'F:\Sheyenne\SNG_sections.shp'
#where to save output files
destination_path=r'F:\Sheyenne\Atmospherically Corrected Landsat\Indices\Main\NDWI\zonal_stats'
 #list of rasters to calculate statistics on
rasters = arcpy.ListRasters("*water_.tif*")
#start loop
for raster in rasters:
    destination_path = os.path.join(destination_path, raster + ".dbf")
    arcpy.gp.ZonalStatisticsAsTable_sa(inpoly, "OBJECTID",raster, destination_path,"DATA","ALL")

The error returned is :

Traceback (most recent call last):

  File "<ipython-input-10-9522776a92dd>", line 1, in <module>
    runfile('F:/python codes/zonal_stats.py', wdir='F:/python codes')

  File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "F:/python codes/zonal_stats.py", line 21, in <module>
    arcpy.gp.ZonalStatisticsAsTable_sa(inpoly, "OBJECTID", raster, destination_raster,"DATA","ALL")

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 498, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))

ExecuteError: ERROR 999999: Error executing function.
Create output table failed
Failed to execute (ZonalStatisticsAsTable).
  • 1
    Does rasters contain any values or is it empty? "*water_.tif*" seems a little suspicious. I would expect something more like "*water_*.tif*" – Mintx Sep 24 '15 at 20:01
  • What version of ArcGIS are you running and on what version of Windows? I've seen plenty of weird version-dependent errors. Are you able to run the Zonal Statistics tool on the failing raster and with these zones in ArcMap? – nicksan Sep 24 '15 at 23:00
  • I am using ArcGIS 10.2 and Windows 7. – spotter Sep 25 '15 at 17:34
  • I have a similar issue: the zonal statistics as table in a for loop results in error ERROR 999999: Error executing function. This spatial reference object cannot be defined from the available information. I need to run a script for a bunch of fcs and rasters (so several rasters statistics for each fc). With the same script, it does run in some gdb and it doesn't in others with similar structure. Or runs for a while and then stops. Could I introduce the "if, else continue" or something of the like to at least keep the loop running for the fcs/rasters where it doesn't find that problem? I'm stuc – user110826 Dec 14 '17 at 13:18
1

The fourth parameter to ZonalStatisticsAsTable should be the output name - since you've put "DATA" there (which should be parameter 5), you're likely getting no output. You'll need to put your destination_path variable in that location.

But I'll also caution you that, as written, destination_path is going to keep growing since you're appending to it in each round of your loop. Try assigning to a new variable. Something like

for raster in rasters:
    destination_raster = os.path.join(destination_path, raster + ".dbf")
    arcpy.gp.ZonalStatisticsAsTable_sa(inpoly, "OBJECTID", raster,destination_raster,"DATA","ALL")

That style should preserve destination_path for each iteration and let you reuse a variable to provide to the Zonal Stats function call.

| improve this answer | |
  • I actually just noticed that, but some thing, code runs no output – spotter Sep 24 '15 at 19:51
  • I'd definitely recommend checking your inputs and your outputs, as mentioned in a few other comments then - add a "print raster" to the loop as well as a "print destination_raster" (or whatever you decided to name it) and make sure that both make sense. You could also try just running it for a single iteration and seeing what happens (effectively removing the loop) – nicksan Sep 24 '15 at 20:38
  • the rasters populate as expected, but you are right, when I run it using only 1 raster without loop it doesn't work either – spotter Sep 24 '15 at 21:02
1

for a single tool, you could also right-click on "zonal stat as table" and run it in batch.

Concerning your code, if it runs without errors nor results, you should first check the layer being processed ("print raster"in the loop), then make sure that there is no projection issue.

| improve this answer | |

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.