<?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: raster clipping with overlapping polygons in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/raster-clipping-with-overlapping-polygons/m-p/763632#M59013</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Further to what Christopher said, you want to extract your feature class data of interest using your cursor, and then access that outside the cursor. Here is an example using a dictionary.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
# set environments
arcpy.env.workspace = "Path"
arcpy.env.overwriteOutput = True
#set parameters
featureTable="Individual_catchments"
baseFeature="elevation"
temp_featureTable = r"in_memory\temp1"
temp_baseFeature = r"in_memory\temp2"
temp_featureClass = r"in_memory\temp_fc"
temp_layer = r"in_memory\temp"

arcpy.MakeFeatureLayer_management(featureTable, temp_featureTable)
arcpy.MakeFeatureLayer_management(baseFeature, temp_baseFeature)
arcpy.MakeFeatureLayer_management(featureClass, temp_featureClass)

# Get OID field name
f_list = arcpy.ListFields(temp_featureClass)
for f in f_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if f.type == "OID":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f_oid = f.name

# Set Dictionary
feat_dict = {}

#set cursor
rows = arcpy.SearchCursor(temp_featureClass)

# loop
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; feat_dict[row.getValue(f_oid)] = row.getValue("Local")

for id in feat_dict:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(temp_featureClass, temp_layer, "%s = %s" % (f_oid, id))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Clip_management(temp_baseFeature, "#", "catch%s.tif" % (feat_dict[id]), temp_layer, "0", "ClippingGeometry")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 08:27:15 GMT</pubDate>
    <dc:creator>MathewCoyle</dc:creator>
    <dc:date>2021-12-12T08:27:15Z</dc:date>
    <item>
      <title>raster clipping with overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/raster-clipping-with-overlapping-polygons/m-p/763630#M59011</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am quite new to using python, but...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a set of overlapping polygons that I would like to use to clip several raster layers&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have the following code thus far&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy # set environments arcpy.env.workspace = "Path" arcpy.env.overwriteOutput = True #set parameters featureTable="Individual_catchments" baseFeature="elevation" #set cursor rows = arcpy.SearchCursor(featureClass) # loop&amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #here I select the geometry&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; feat = row.Shape &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Clip_management(baseFeature, "#", "catch"+str(row.Local)+".tif",feat, "0", "ClippingGeometry")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;however, when I try to run I get an error stating "ERROR 999999: Error executing function. Raster dataset already exists Failed to execute (Clip). "&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thank you for your time and help&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;cheers&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mat&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Aug 2012 11:04:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/raster-clipping-with-overlapping-polygons/m-p/763630#M59011</guid>
      <dc:creator>matseymour</dc:creator>
      <dc:date>2012-08-16T11:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: raster clipping with overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/raster-clipping-with-overlapping-polygons/m-p/763631#M59012</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;A couple of things that come to mind but i haven't tested:&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;try building your output raster name outside of the tool, and pass it in as a variable, putting those sorts of expressions in a tool can be confusing to read and lead to syntax errors&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;If the row.Local field contains duplicate values, you could indeed be trying to overwrite an existing raster that has been created, so you might want to double check that.&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;you're passing in the geometry of a polygon as the 'feat' variable - if you read the help on this tool i think it wants a feature layer or another raster layer instead of the geometry.&amp;nbsp; So create a feature layer based on the current row you are processing like this:&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;feat_id = row.FID #or however you want to uniquely identify a given polygon)
qry = """ "FID" = """ + feat_id
feat = arcpy.MakeFeatureLayer(featureClass,'feat_lyr',qry)
out_rast = "catch" + str(row.Local) + ".tif"
arcpy.Clip_management(baseFeature, "#", out_rast,feat, "0", "ClippingGeometry")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The MakeFeatureLayer makes an in-memory layer with a query definition based on the current row's FID and then is passed in as an argument to the Clip tool.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:27:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/raster-clipping-with-overlapping-polygons/m-p/763631#M59012</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2021-12-12T08:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: raster clipping with overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/raster-clipping-with-overlapping-polygons/m-p/763632#M59013</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Further to what Christopher said, you want to extract your feature class data of interest using your cursor, and then access that outside the cursor. Here is an example using a dictionary.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
# set environments
arcpy.env.workspace = "Path"
arcpy.env.overwriteOutput = True
#set parameters
featureTable="Individual_catchments"
baseFeature="elevation"
temp_featureTable = r"in_memory\temp1"
temp_baseFeature = r"in_memory\temp2"
temp_featureClass = r"in_memory\temp_fc"
temp_layer = r"in_memory\temp"

arcpy.MakeFeatureLayer_management(featureTable, temp_featureTable)
arcpy.MakeFeatureLayer_management(baseFeature, temp_baseFeature)
arcpy.MakeFeatureLayer_management(featureClass, temp_featureClass)

# Get OID field name
f_list = arcpy.ListFields(temp_featureClass)
for f in f_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if f.type == "OID":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f_oid = f.name

# Set Dictionary
feat_dict = {}

#set cursor
rows = arcpy.SearchCursor(temp_featureClass)

# loop
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; feat_dict[row.getValue(f_oid)] = row.getValue("Local")

for id in feat_dict:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(temp_featureClass, temp_layer, "%s = %s" % (f_oid, id))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Clip_management(temp_baseFeature, "#", "catch%s.tif" % (feat_dict[id]), temp_layer, "0", "ClippingGeometry")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:27:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/raster-clipping-with-overlapping-polygons/m-p/763632#M59013</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-12T08:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: raster clipping with overlapping polygons</title>
      <link>https://community.esri.com/t5/python-questions/raster-clipping-with-overlapping-polygons/m-p/763633#M59014</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the input folks. After several instances of trial and error I managed to get the below script to produce the output I needed, and feel much more confident in using python to retrieve data. I am sure the code could be simplified significantly, but it is enough for my simple mind. Of course any suggestions would be welcomed. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;cheers &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mat&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;###this first part creates individual raster files (from as single larger raster file) based on overlapping polygons import math import os import arcpy # set the workspace environment&amp;nbsp; arcpy.env.workspace = "D:/path.mdb" arcpy.env.overwriteOutput = True # set the cliping regions featureClass="Individual_catchments" # set the base feature baseFeature="elevation_swiss" #create list to store catchment elevations catchmentList = [] #create table to store catchment id and average elevation CreateTable_management ("D:/path.mdb", "Average_elevations")&amp;nbsp;&amp;nbsp; # need to use a cursor argument since we are looking at a single table # this one assigns a search cursor to the Individual catchments table rows = arcpy.SearchCursor(featureClass)&amp;nbsp;&amp;nbsp; # loop to create all of my individual files &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #here we only want the geometry (shape) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; feat = row.Shape &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output="c"+str(row.LOCALITYID) &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Clip_management(baseFeature, "#", output,feat, "0", "ClippingGeometry") # delete the cursor del rows&amp;nbsp; ################################################################################################# ####### second part of script to take individual catchments and calculate some statistics ####### # i created this using a separate script from the first so the environments are retyped #################################################################################################&amp;nbsp; # make sure to put the module you are working with before the function (or it will spit back 'not defined') - one my biggest pitfalls ###here i want to update the catchemnt feature with additional information based on other layers import math import os import arcpy # set the workspace environment&amp;nbsp; arcpy.env.workspace = "D:/path.mdb"&amp;nbsp; # Overwrite pre-existing files arcpy.env.overwriteOutput = True&amp;nbsp; #parameters path = "D:/path.mdb" arcpy.Delete_management("SumTable") arcpy.CreateTable_management (path,"SumTable") arcpy.AddField_management ("SumTable", "NameID", "TEXT") arcpy.AddField_management ("SumTable", "TotalCount", "LONG") arcpy.AddField_management ("SumTable", "TotalElv", "FLOAT",'',10) arcpy.AddField_management ("SumTable", "AVG_ELV", "FLOAT",'',10) SummaryT= "D:/path.mdb/SumTable"&amp;nbsp;&amp;nbsp; statsFields = [["COUNT", "SUM"],["ELEVC","SUM"]]&amp;nbsp;&amp;nbsp; #create list to point to catchment elevations catchmentList = arcpy.ListRasters ("c*")&amp;nbsp;&amp;nbsp; for i in catchmentList: &amp;nbsp;&amp;nbsp;&amp;nbsp; #make sure each raster has its own attribute table &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.BuildRasterAttributeTable_management(i,"Overwrite") &amp;nbsp;&amp;nbsp;&amp;nbsp; #create a new field with elevation times number of occurances (COUNT * VALUE) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management (i, "ELEVC", "LONG") &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(i,"ELEVC","!Value! * !Count!","PYTHON_9.3","#") &amp;nbsp;&amp;nbsp;&amp;nbsp; temp_outtable = "outtable" &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Statistics_analysis (i, temp_outtable , statsFields) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #python's tedious way to get data from a table &amp;nbsp;&amp;nbsp;&amp;nbsp; rows=arcpy.SearchCursor(temp_outtable) #this 'points' to the first row&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; row=rows.next() #this 'points' to the field &amp;nbsp;&amp;nbsp;&amp;nbsp; print row.SUM_COUNT,"_",row.SUM_ELEVC&amp;nbsp; # Create the insert cursor and a new empty row &amp;nbsp;&amp;nbsp;&amp;nbsp; rowInserter = arcpy.InsertCursor(SummaryT) &amp;nbsp;&amp;nbsp;&amp;nbsp; newIncident = rowInserter.newRow()&amp;nbsp; # Populate attributes of new row &amp;nbsp;&amp;nbsp;&amp;nbsp; newIncident.NameID = str(i[0:7]) &amp;nbsp;&amp;nbsp;&amp;nbsp; newIncident.TotalCount = row.SUM_COUNT &amp;nbsp;&amp;nbsp;&amp;nbsp; newIncident.TotalElv = row.SUM_ELEVC &amp;nbsp;&amp;nbsp;&amp;nbsp; newIncident.AVG_ELV = row.SUM_ELEVC / row.SUM_COUNT&amp;nbsp; # Insert the new row into the shapefile &amp;nbsp;&amp;nbsp;&amp;nbsp; rowInserter.insertRow(newIncident) &amp;nbsp;&amp;nbsp;&amp;nbsp; # delete catchment specific search cursor and temp sum file &amp;nbsp;&amp;nbsp;&amp;nbsp; del row &amp;nbsp;&amp;nbsp;&amp;nbsp; del rows &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(temp_outtable)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # delete the insert cursor del rowInserter&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Aug 2012 09:31:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/raster-clipping-with-overlapping-polygons/m-p/763633#M59014</guid>
      <dc:creator>matseymour</dc:creator>
      <dc:date>2012-08-22T09:31:31Z</dc:date>
    </item>
  </channel>
</rss>

