<?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 Zonal Statistic with multiple shapefiles only works on first file in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/zonal-statistic-with-multiple-shapefiles-only/m-p/1137992#M63600</link>
    <description>&lt;P&gt;I am attempting to calculate zonal statistics on a single raster for multiple shapefiles. When I load all the shapefiles into the contents pane the code runs (tested on 5 files), but I have thousands of shapefiles I need to conduct this calculation and cannot load them all into the contents pane. When I dont load the files to the contents pane only the zonal stat for the first file is calculated, after that an error is returned:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;ExecuteError&lt;/SPAN&gt;                              Traceback (most recent call last)
In  &lt;SPAN class=""&gt;[10]&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;23&lt;/SPAN&gt;:    outzone = ZonalStatistics(shape[i], &lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;presence&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;, Raster(&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;C:pathway to file/.tif&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;), &lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;MAXIMUM&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;NODATA&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;)

File &lt;SPAN class=""&gt;c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Functions.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;ZonalStatistics&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;6530&lt;/SPAN&gt;:  process_as_multidimensional)

File &lt;SPAN class=""&gt;c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Utils.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;swapper&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;53&lt;/SPAN&gt;:    result = wrapper(*args, **kwargs)

File &lt;SPAN class=""&gt;c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Functions.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;Wrapper&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;6522&lt;/SPAN&gt;:  process_as_multidimensional)

File &lt;SPAN class=""&gt;c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;&amp;lt;lambda&amp;gt;&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;511&lt;/SPAN&gt;:   &lt;SPAN class=""&gt;return&lt;/SPAN&gt; &lt;SPAN class=""&gt;lambda&lt;/SPAN&gt; *args: val(*gp_fixargs(args, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;))

&lt;SPAN class=""&gt;ExecuteError&lt;/SPAN&gt;: Failed to execute. Parameters are not valid.
ERROR 000865: Input raster or feature zone data: Abrawayaomys_chebezi.shp does not exist.
WARNING 001000: Zone field: Field presence does not exist
Failed to execute (ZonalStatistics).&lt;BR /&gt;&lt;BR /&gt;The full code is listed below:&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;import arcpy&lt;BR /&gt;from arcpy import env&lt;BR /&gt;from arcpy.sa import *&lt;/P&gt;&lt;P&gt;arcpy.CheckOutExtension("Spatial")&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;indir = "C:/pathway to folder"&lt;BR /&gt;outdir = "C:/pathway to output/"&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;env.workspace = indir&lt;/P&gt;&lt;P&gt;shape = arcpy.ListFiles("*.shp")&lt;BR /&gt;i = 0&lt;BR /&gt;while i &amp;lt; len(shape):&lt;BR /&gt;outzone = ZonalStatistics(shape[i], "presence", Raster("pathway to raster"), "MAXIMUM", "NODATA")&lt;BR /&gt;outzone.save(outdir+shape[i]+"_Jan_Max.tif")&lt;BR /&gt;del outzone&lt;BR /&gt;i += 1&lt;BR /&gt;print ("Sampling iteration #" + str(i))&lt;BR /&gt;print ("Complete")&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;the pathways were changed just for this question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;is there any suggestions for how to avoid this error?&lt;/P&gt;</description>
    <pubDate>Thu, 27 Jan 2022 18:30:50 GMT</pubDate>
    <dc:creator>MatthewWatson3</dc:creator>
    <dc:date>2022-01-27T18:30:50Z</dc:date>
    <item>
      <title>Zonal Statistic with multiple shapefiles only works on first file</title>
      <link>https://community.esri.com/t5/python-questions/zonal-statistic-with-multiple-shapefiles-only/m-p/1137992#M63600</link>
      <description>&lt;P&gt;I am attempting to calculate zonal statistics on a single raster for multiple shapefiles. When I load all the shapefiles into the contents pane the code runs (tested on 5 files), but I have thousands of shapefiles I need to conduct this calculation and cannot load them all into the contents pane. When I dont load the files to the contents pane only the zonal stat for the first file is calculated, after that an error is returned:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;ExecuteError&lt;/SPAN&gt;                              Traceback (most recent call last)
In  &lt;SPAN class=""&gt;[10]&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;23&lt;/SPAN&gt;:    outzone = ZonalStatistics(shape[i], &lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;presence&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;, Raster(&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;C:pathway to file/.tif&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;), &lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;MAXIMUM&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;NODATA&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;)

File &lt;SPAN class=""&gt;c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Functions.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;ZonalStatistics&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;6530&lt;/SPAN&gt;:  process_as_multidimensional)

File &lt;SPAN class=""&gt;c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Utils.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;swapper&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;53&lt;/SPAN&gt;:    result = wrapper(*args, **kwargs)

File &lt;SPAN class=""&gt;c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Functions.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;Wrapper&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;6522&lt;/SPAN&gt;:  process_as_multidimensional)

File &lt;SPAN class=""&gt;c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;&amp;lt;lambda&amp;gt;&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;511&lt;/SPAN&gt;:   &lt;SPAN class=""&gt;return&lt;/SPAN&gt; &lt;SPAN class=""&gt;lambda&lt;/SPAN&gt; *args: val(*gp_fixargs(args, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;))

&lt;SPAN class=""&gt;ExecuteError&lt;/SPAN&gt;: Failed to execute. Parameters are not valid.
ERROR 000865: Input raster or feature zone data: Abrawayaomys_chebezi.shp does not exist.
WARNING 001000: Zone field: Field presence does not exist
Failed to execute (ZonalStatistics).&lt;BR /&gt;&lt;BR /&gt;The full code is listed below:&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;import arcpy&lt;BR /&gt;from arcpy import env&lt;BR /&gt;from arcpy.sa import *&lt;/P&gt;&lt;P&gt;arcpy.CheckOutExtension("Spatial")&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;indir = "C:/pathway to folder"&lt;BR /&gt;outdir = "C:/pathway to output/"&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;env.workspace = indir&lt;/P&gt;&lt;P&gt;shape = arcpy.ListFiles("*.shp")&lt;BR /&gt;i = 0&lt;BR /&gt;while i &amp;lt; len(shape):&lt;BR /&gt;outzone = ZonalStatistics(shape[i], "presence", Raster("pathway to raster"), "MAXIMUM", "NODATA")&lt;BR /&gt;outzone.save(outdir+shape[i]+"_Jan_Max.tif")&lt;BR /&gt;del outzone&lt;BR /&gt;i += 1&lt;BR /&gt;print ("Sampling iteration #" + str(i))&lt;BR /&gt;print ("Complete")&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;the pathways were changed just for this question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;is there any suggestions for how to avoid this error?&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jan 2022 18:30:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/zonal-statistic-with-multiple-shapefiles-only/m-p/1137992#M63600</guid>
      <dc:creator>MatthewWatson3</dc:creator>
      <dc:date>2022-01-27T18:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: Zonal Statistic with multiple shapefiles only works on first file</title>
      <link>https://community.esri.com/t5/python-questions/zonal-statistic-with-multiple-shapefiles-only/m-p/1138174#M63607</link>
      <description>&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;to assist with proper indentation checks and to provide line numbers for reference&lt;/P&gt;&lt;P&gt;In any event, throw in some print statements to confirm that the concatenation process results in a correct filename.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;outdir = "C:/pathway to output/"
shape = "a.shp"  # -- I assume here that the path is removed
outdir+shape+"_Jan_Max.tif"  # -- wrong because *.shp not removed
'C:/pathway to output/a.shp_Jan_Max.tif'

outdir+shape[:-4]+"_Jan_Max.tif" # -- right if file extension is present
'C:/pathway to output/a_Jan_Max.tif'&lt;/LI-CODE&gt;&lt;P&gt;so make sure that the shapefile is stripped of path and file extension first.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jan 2022 22:51:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/zonal-statistic-with-multiple-shapefiles-only/m-p/1138174#M63607</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-01-27T22:51:10Z</dc:date>
    </item>
  </channel>
</rss>

