More

Batch Zonal Statistics as table?

Batch Zonal Statistics as table?


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:SheyenneAtmospherically Corrected LandsatIndicesMainNDWImain_master' #zones to calculate stats on inpoly=r'F:SheyenneSNG_sections.shp' #where to save output files destination_path=r'F:SheyenneAtmospherically Corrected LandsatIndicesMainNDWIzonal_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 "", line 1, in  runfile('F:/python codes/zonal_stats.py', wdir="F:/python codes") File "C:UsersspotterAppDataLocalContinuumAnacondalibsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:UsersspotterAppDataLocalContinuumAnacondalibsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "F:/python codes/zonal_stats.py", line 21, in  arcpy.gp.ZonalStatisticsAsTable_sa(inpoly, "OBJECTID", raster, destination_raster,"DATA","ALL") File "C:Program Files (x86)ArcGISDesktop10.2arcpyarcpygeoprocessing\_base.py", line 498, in  return lambda *args: val(*gp_fixargs(args, True)) ExecuteError: ERROR 999999: Error executing function. Create output table failed Failed to execute (ZonalStatisticsAsTable).

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.


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.


Watch the video: zonal statistics example