Branched from: Calculate zonal mean for multiple rasters using Python
I found a solution to the same problem. This solution will allow you to process all of the raster files in a selected folder instead of naming the raster files individually. I had 936 rasters I needed to analyze, so the ability to loop through all of the available rasters was a necessity for me.
The script below is my modificaiton of the discussion in the following post: arcpy - Python Script Zonal Stats as Table Loop Question - Geographic Information Systems Stack Exchange
The output from this script is an individual .dbf file (and associated files) for each of the rasters that you process.
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os<SPAN class="punctuation token">,</SPAN> arcinfo
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="keyword token">from</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>sa <SPAN class="keyword token">import</SPAN> <SPAN class="operator token">*</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Spatial"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Select the folder containing raster files. This script will use ALL of</SPAN>
<SPAN class="comment token"># the raster files in the selected folder. </SPAN>
env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN>
<SPAN class="string token">"C:\\Users\\davebetts\\GIS\\project\\rasters"</SPAN>
<SPAN class="comment token"># Select the shapefile containing the polygons to use as boundaries</SPAN>
<SPAN class="comment token"># for zonal statistics</SPAN>
watershedFeat <SPAN class="operator token">=</SPAN>
<SPAN class="string token">"C:\\Users\\davebetts\\GIS\\project\\zones_polygons.shp"</SPAN>
<SPAN class="comment token"># Select output folder for saving the output - zonal tables (.dbf files)</SPAN>
outDir <SPAN class="operator token">=</SPAN>
<SPAN class="string token">"C:\\Users\\davebetts\\GIS\\project\\rasters\\tables\\"</SPAN>
<SPAN class="comment token"># Something goes wrong with this script during use, perhaps with the</SPAN>
<SPAN class="comment token"># temporary files. No error messages are given. The "print" statements</SPAN>
<SPAN class="comment token"># inserted within the script keep track of where to restart. Replace the '0'</SPAN>
<SPAN class="comment token"># in the "for" statement with the most recently printed integer (printing of</SPAN>
<SPAN class="comment token"># the variable 'ndx').</SPAN>
x <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># the "0" can be replaced by the most recent result of</SPAN>
<SPAN class="comment token"># "print ndx" in order to restart where the code stopped</SPAN>
<SPAN class="keyword token">for</SPAN> raster <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> raster
ndx <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">.</SPAN>index<SPAN class="punctuation token">(</SPAN>raster<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> ndx
outTable <SPAN class="operator token">=</SPAN> outDir <SPAN class="operator token">+</SPAN> raster <SPAN class="operator token">+</SPAN> <SPAN class="string token">".dbf"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>gp<SPAN class="punctuation token">.</SPAN>ZonalStatisticsAsTable_sa<SPAN class="punctuation token">(</SPAN>watershedFeat<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"FID"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="comment token"># Select an attribute in the shape file to identify polygons</SPAN>
raster<SPAN class="punctuation token">,</SPAN>
outTable<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"NODATA"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"MEAN"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CheckInExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Spatial"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
!!! This script is still stopping several times partway through the total number of rasters I'm processing. I have to restart the process using the most recent results from the "prnt ndx" replacing the '0' in line 30. !!!
The script needs improvement. I would love to get rid of the pauses and having to restart.
I also want to combine this script with the scripts that I use before and after into a single script. The steps I follow and a link to the location of these scripts are as follows:
Kriging and zonal statistics
- Kriging of gridded climate data over multiple time steps. Individual rasters are produced for each time step.
- Zonal statistics of ALL rasters in the selected folder (produced by Step 1)
- Combine all .dbf files (zonal tables) in working directory (produced by Step 2) into a single table.
- Columns = zones
- Rows = time steps