<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Kriging and Zonal Statistics for multiple rasters using Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398275#M31429</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What is the resolution of your raster data?&amp;nbsp; Sometimes when the polygons overlap&amp;nbsp;only portions of&amp;nbsp;the&amp;nbsp;underlying raster cells, the zonal statistics&amp;nbsp;isn't able to calculate&amp;nbsp;an average value.&amp;nbsp; That might produce the odd pattern that doesn't look like a pattern, because it's only every so often that there&amp;nbsp;is&amp;nbsp;enough of an offset between County_Grid.shp and your raster&amp;nbsp;cells that some of the 1x1mile grid cells only contains fractions of the underlying raster cells.&amp;nbsp; If that is the case, you might have to combine your grids into&amp;nbsp; 2x2mile, 3x3mile or greater.&amp;nbsp; That's my best guess so far without seeing your raster data, or additional information.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 27 Oct 2017 23:17:40 GMT</pubDate>
    <dc:creator>DavidBetts</dc:creator>
    <dc:date>2017-10-27T23:17:40Z</dc:date>
    <item>
      <title>Kriging and Zonal Statistics for multiple rasters using Python</title>
      <link>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398271#M31425</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;EM&gt;Branched from:&amp;nbsp;&lt;A href="https://community.esri.com/thread/200613" target="_blank"&gt;Calculate zonal mean for multiple rasters using Python&lt;/A&gt;&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I found a solution to the same problem.&amp;nbsp; This solution will allow you to process all of the raster files in a selected folder instead of naming the raster files individually.&amp;nbsp; I had 936 rasters I needed to analyze, so the ability to loop through all of the available rasters was a necessity for me.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The script below is&amp;nbsp;my modificaiton of the discussion in the following post:&amp;nbsp;&lt;A class="link-titled" href="https://gis.stackexchange.com/questions/206559/python-script-zonal-stats-as-table-loop-question" title="https://gis.stackexchange.com/questions/206559/python-script-zonal-stats-as-table-loop-question" rel="nofollow noopener noreferrer" target="_blank"&gt;arcpy - Python Script Zonal Stats as Table Loop Question - Geographic Information Systems Stack Exchange&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The output from this script is an individual .dbf file (and associated files) for each of the rasters that you process.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; os&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; arcinfo
&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; arcpy &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; env
&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;sa &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;*&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;overwriteOutput &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token boolean"&gt;True&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CheckOutExtension&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Spatial"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# Select the folder containing raster files.&amp;nbsp; This script will use ALL of&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# the raster files in the selected folder. &lt;/SPAN&gt;
env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; &lt;SPAN class="string token"&gt;"C:\\Users\\davebetts\\GIS\\project\\rasters"&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# Select the shapefile containing the polygons to use as boundaries&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# for zonal statistics&lt;/SPAN&gt;
watershedFeat &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; &lt;SPAN class="string token"&gt;"C:\\Users\\davebetts\\GIS\\project\\zones_polygons.shp"&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# Select output folder for saving the output - zonal tables (.dbf files)&lt;/SPAN&gt;
outDir &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; &lt;SPAN class="string token"&gt;"C:\\Users\\davebetts\\GIS\\project\\rasters\\tables\\"&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# Something goes wrong with this script during use, perhaps with the&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# temporary files.&amp;nbsp; No error messages are given.&amp;nbsp; The "print" statements&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# inserted within the script keep track of where to restart.&amp;nbsp; Replace the '0'&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# in the "for" statement with the most recently printed integer (printing of&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# the variable 'ndx').&lt;/SPAN&gt;

x &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListRasters&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# the "0" can be replaced by the most recent result of&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# "print ndx" in order to restart where the code stopped&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; raster &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListRasters&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; raster 
&amp;nbsp;&amp;nbsp;&amp;nbsp; ndx &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; x&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;index&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;raster&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; ndx
&amp;nbsp;&amp;nbsp;&amp;nbsp; outTable &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; outDir &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; raster &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;".dbf"&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;gp&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ZonalStatisticsAsTable_sa&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;watershedFeat&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="string token"&gt;"FID"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="comment token"&gt;# Select an attribute in the shape file to identify polygons&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raster&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outTable&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="string token"&gt;"NODATA"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"MEAN"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CheckInExtension&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Spatial"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍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&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;!!! This script is still stopping several times partway through the total number of rasters I'm processing.&amp;nbsp; I have to restart the process using the most recent results from the "prnt ndx" replacing the '0' in line 30. !!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The script&amp;nbsp;needs improvement.&amp;nbsp; I would love to&amp;nbsp;get rid of the pauses&amp;nbsp;and having to restart.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I also want to combine this script with the scripts that I use&amp;nbsp;before and after into a single script.&amp;nbsp; The steps I follow and a link to the location of these scripts are as follows:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/davebetts/davesdata" rel="nofollow noopener noreferrer" target="_blank"&gt;Kriging and zonal statistics&lt;/A&gt; &amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Kriging of gridded climate data over multiple time steps.&amp;nbsp; Individual rasters are produced for each time step.&lt;/LI&gt;&lt;LI&gt;Zonal statistics of ALL rasters in the selected folder (produced by Step 1)&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;Combine all .dbf files (zonal tables) in working directory (produced by Step 2) into a single table.&lt;UL&gt;&lt;LI&gt;Columns = zones&lt;/LI&gt;&lt;LI&gt;Rows = time steps&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:12:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398271#M31425</guid>
      <dc:creator>DavidBetts</dc:creator>
      <dc:date>2021-12-11T18:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: Kriging and Zonal Statistics for multiple rasters using Python</title>
      <link>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398272#M31426</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Xander for sharing this. I have tried the code below but it does not gives me the zonal mean statistics for all the S_R_T in the County_Grid shape file. But it does provide for some of the grids. I have no clue why it does not produce the zonal mean for all the S_R_Ts.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I appreciate if If some one can help me to figure this out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks a lot again.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy, os, arcinfo&lt;BR /&gt;from arcpy import env&lt;BR /&gt;from arcpy.sa import *&lt;BR /&gt;arcpy.env.overwriteOutput = True&lt;BR /&gt;arcpy.CheckOutExtension("Spatial")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;env.workspace = "H:/outRaster/Rasters"&lt;BR /&gt; &lt;BR /&gt;# Select the shapefile containing the polygons to use as boundaries&lt;BR /&gt;# for zonal statistics&lt;BR /&gt;watershedFeat = "H:/outRaster/Grid/County_Grid.shp"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Select output folder for saving the output - zonal tables (.dbf files)&lt;BR /&gt;outDir = "H:/Table/"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;x = arcpy.ListRasters()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for raster in arcpy.ListRasters(): #[0:]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;raster_name = os.path.basename(raster).rstrip(os.path.splitext(raster)[1])&lt;BR /&gt; outTable = outDir + raster_name + "_TBL.dbf"&lt;BR /&gt; arcpy.gp.ZonalStatisticsAsTable(watershedFeat,"S_R_T", raster, outTable,"NODATA","MEAN")&lt;/P&gt;&lt;P&gt;arcpy.CheckInExtension("Spatial")&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 26 Oct 2017 13:47:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398272#M31426</guid>
      <dc:creator>DuminduJayasekera</dc:creator>
      <dc:date>2017-10-26T13:47:57Z</dc:date>
    </item>
    <item>
      <title>Re: Kriging and Zonal Statistics for multiple rasters using Python</title>
      <link>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398273#M31427</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is there any pattern to which grids are left out?&amp;nbsp; Are there gaps in the&amp;nbsp;sequence along "S_R_T"? or does the script move through the series and stop before finishing?&amp;nbsp; Does the&amp;nbsp;attribute "S_R_T" have a unique value for each of the polygons in "County_Grid.shp"?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Oct 2017 00:42:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398273#M31427</guid>
      <dc:creator>DavidBetts</dc:creator>
      <dc:date>2017-10-27T00:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: Kriging and Zonal Statistics for multiple rasters using Python</title>
      <link>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398274#M31428</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for your reply David. No there is no pattern. I checked it by doing manually suing the "zonal statistics as Table" tool but still produced the same output identical to the output by the script. Then I stored the shape file (County_Grid.shp) and the raster in the geodatabase and ran the script but no luck. The grids the shapefile are in 1 mile x 1 mile. Instead, as a trial, I used a county shapfile across the state of Kansas, USA and used the FIPS codes as the ID and it worked by producing zonal means for all the counties for the KS state. But, I really want to make it run for the County_Grid.shp in which grids are in 1 mile x 1 mile resolution to obtain the zonal means for all S_R_T. Any idea?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Oct 2017 14:48:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398274#M31428</guid>
      <dc:creator>DuminduJayasekera</dc:creator>
      <dc:date>2017-10-27T14:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Kriging and Zonal Statistics for multiple rasters using Python</title>
      <link>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398275#M31429</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What is the resolution of your raster data?&amp;nbsp; Sometimes when the polygons overlap&amp;nbsp;only portions of&amp;nbsp;the&amp;nbsp;underlying raster cells, the zonal statistics&amp;nbsp;isn't able to calculate&amp;nbsp;an average value.&amp;nbsp; That might produce the odd pattern that doesn't look like a pattern, because it's only every so often that there&amp;nbsp;is&amp;nbsp;enough of an offset between County_Grid.shp and your raster&amp;nbsp;cells that some of the 1x1mile grid cells only contains fractions of the underlying raster cells.&amp;nbsp; If that is the case, you might have to combine your grids into&amp;nbsp; 2x2mile, 3x3mile or greater.&amp;nbsp; That's my best guess so far without seeing your raster data, or additional information.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Oct 2017 23:17:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/kriging-and-zonal-statistics-for-multiple-rasters/m-p/398275#M31429</guid>
      <dc:creator>DavidBetts</dc:creator>
      <dc:date>2017-10-27T23:17:40Z</dc:date>
    </item>
  </channel>
</rss>

