<?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 Trouble combining SelectLayerByLocation with an UpdateCursor. Please help! in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/trouble-combining-selectlayerbylocation-with-an/m-p/311355#M24232</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;First of all, I have been using ArcGIS 10 for a long time but I am a novice to using Python scripting. So please pardon my probably rather clunky code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;What I would like to do for each feature is to update a field with the mean value of a different field (in the same feature class) of all features that fall within a certain radius. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In my case, I have the point shapefile "FL20C.shp" which contains 20 features each with a series of attributes including "ZBUSH" and "MEAN01". I would like to update "MEAN01" with the value calculated by taking the mean/average of all the features in a 150 km radius of it (including the feature to be updated itself). The layer "counties_lyr" is the Feature Layer I created to be able to run the SelectLayerByLocation. (So that step has already been made and is left out of the script.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have tested the different blocks of code and they seem to work: the script iterates successfully through the SelectLayerByLocation and the steps of calculating the mean. I think the problem lies in establishing the connection between the UpdateCursor and the SearchCursor used in the SelectLayerByLocation. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have structured the code in different ways, hitting road blocks in different locations. With this first attempt I get the following error: "Runtime error &amp;lt;class 'arcgisscripting.ExecuteError'&amp;gt;: ERROR 999999: Error executing function. Cannot acquire a lock. Cannot acquire a lock. [The table FL20C.shp is being written by another process.] Failed to execute (SelectLayerByLocation)." &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This error results from the following code: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
# Creating the UpdateCursor and its input 
counties = "C:/GISDATA2/FAUI/USAtests/FL20C.shp"
cursor01 = arcpy.UpdateCursor(counties)
# Setting up variables to be used in the Select Layer By Location 
selectionbase = arcpy.SearchCursor("counties_lyr")
desc = arcpy.Describe("counties_lyr")
selectionbaseShapeFieldName = desc.featureClass.shapeFieldName

# Starting the iteration through the input table that is to be updated 
for county in cursor01: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Starting the Select Layer By Location 
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_c = arcpy.SelectLayerByLocation_management("counties_lyr", "WITHIN_A_DISTANCE", row.getValue(selectionbaseShapeFieldName), "150000 Meter", "NEW_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Counting the number of selected features to be used in the calculation of the Mean 
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_nr = arcpy.GetCount_management(selected_c)
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_count = selected_nr.getOutput(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; count_sel = float(selected_count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculating the total of the ZBUSH value to be used in the Mean 
&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_selected = arcpy.SearchCursor(selected_c)
&amp;nbsp;&amp;nbsp;&amp;nbsp; list4total = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for co in curs_selected:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list4total.append(co.ZBUSH)
&amp;nbsp;&amp;nbsp;&amp;nbsp; total_v = sum(list4total)
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_mean = total_v / count_sel
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Update "MEAN01" with the new_mean value&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; county.MEAN01 = new_mean
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor01.updateRow(county)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # emptying variables before restarting loop 
&amp;nbsp;&amp;nbsp;&amp;nbsp; del count_sel
&amp;nbsp;&amp;nbsp;&amp;nbsp; del list4total
&amp;nbsp;&amp;nbsp;&amp;nbsp; del total_v
&amp;nbsp;&amp;nbsp;&amp;nbsp; del new_mean
&amp;nbsp;&amp;nbsp;&amp;nbsp; del cursor02
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Alternatively this code runs the Select Layer By Location and the Calculations successfully, but fails to update the Row. Instead I get: Runtime error &amp;lt;type 'exceptions.AttributeError'&amp;gt;: 'Cursor' object has no attribute '__exit__'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I think my mistake is in this code segment of the full code that follows below: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;with arcpy.UpdateCursor(counties) as cursor02:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for co in cursor02:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; co.MEAN01 = new_mean
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor02.updateRow(co)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt; Should I leave out the iteration? &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;How do I make sure that the shapefile-row being updated in this cursor corresponds to the row in the FeatureLayer used for the SelectLayerByLocation? If this is done through a SQL statement, then I don't know where to place it or how to state it. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
selectionbase = arcpy.SearchCursor("counties_lyr")
desc = arcpy.Describe("counties_lyr")
selectionbaseShapeFieldName = desc.featureClass.shapeFieldName

for row in selectionbase:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make the selection 
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_c = arcpy.SelectLayerByLocation_management("counties_lyr", "WITHIN_A_DISTANCE", row.getValue(selectionbaseShapeFieldName), "150000 Meter", "NEW_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Count the number of selected features&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_nr = arcpy.GetCount_management(selected_c)
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_count = selected_nr.getOutput(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; count_sel = float(selected_count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the total ZBUSH value for the selected features 
&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_selected = arcpy.SearchCursor(selected_c)
&amp;nbsp;&amp;nbsp;&amp;nbsp; list4total = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for county in curs_selected:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list4total.append(county.ZBUSH)
&amp;nbsp;&amp;nbsp;&amp;nbsp; total_v = sum(list4total)
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_mean = total_v / count_sel
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Write the mean-value to the feature&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.UpdateCursor(counties) as cursor02:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for co in cursor02:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; co.MEAN01 = new_mean
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor02.updateRow(co)&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; # Reset values at end of iteration
&amp;nbsp;&amp;nbsp;&amp;nbsp; del count_sel 
&amp;nbsp;&amp;nbsp;&amp;nbsp; del list4total
&amp;nbsp;&amp;nbsp;&amp;nbsp; del total_v&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; del new_mean 
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you in advance for your help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; Vincent&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 08 Nov 2013 11:44:12 GMT</pubDate>
    <dc:creator>Vincentvan_Exel</dc:creator>
    <dc:date>2013-11-08T11:44:12Z</dc:date>
    <item>
      <title>Trouble combining SelectLayerByLocation with an UpdateCursor. Please help!</title>
      <link>https://community.esri.com/t5/python-questions/trouble-combining-selectlayerbylocation-with-an/m-p/311355#M24232</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;First of all, I have been using ArcGIS 10 for a long time but I am a novice to using Python scripting. So please pardon my probably rather clunky code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;What I would like to do for each feature is to update a field with the mean value of a different field (in the same feature class) of all features that fall within a certain radius. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In my case, I have the point shapefile "FL20C.shp" which contains 20 features each with a series of attributes including "ZBUSH" and "MEAN01". I would like to update "MEAN01" with the value calculated by taking the mean/average of all the features in a 150 km radius of it (including the feature to be updated itself). The layer "counties_lyr" is the Feature Layer I created to be able to run the SelectLayerByLocation. (So that step has already been made and is left out of the script.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have tested the different blocks of code and they seem to work: the script iterates successfully through the SelectLayerByLocation and the steps of calculating the mean. I think the problem lies in establishing the connection between the UpdateCursor and the SearchCursor used in the SelectLayerByLocation. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have structured the code in different ways, hitting road blocks in different locations. With this first attempt I get the following error: "Runtime error &amp;lt;class 'arcgisscripting.ExecuteError'&amp;gt;: ERROR 999999: Error executing function. Cannot acquire a lock. Cannot acquire a lock. [The table FL20C.shp is being written by another process.] Failed to execute (SelectLayerByLocation)." &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This error results from the following code: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
# Creating the UpdateCursor and its input 
counties = "C:/GISDATA2/FAUI/USAtests/FL20C.shp"
cursor01 = arcpy.UpdateCursor(counties)
# Setting up variables to be used in the Select Layer By Location 
selectionbase = arcpy.SearchCursor("counties_lyr")
desc = arcpy.Describe("counties_lyr")
selectionbaseShapeFieldName = desc.featureClass.shapeFieldName

# Starting the iteration through the input table that is to be updated 
for county in cursor01: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Starting the Select Layer By Location 
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_c = arcpy.SelectLayerByLocation_management("counties_lyr", "WITHIN_A_DISTANCE", row.getValue(selectionbaseShapeFieldName), "150000 Meter", "NEW_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Counting the number of selected features to be used in the calculation of the Mean 
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_nr = arcpy.GetCount_management(selected_c)
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_count = selected_nr.getOutput(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; count_sel = float(selected_count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculating the total of the ZBUSH value to be used in the Mean 
&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_selected = arcpy.SearchCursor(selected_c)
&amp;nbsp;&amp;nbsp;&amp;nbsp; list4total = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for co in curs_selected:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list4total.append(co.ZBUSH)
&amp;nbsp;&amp;nbsp;&amp;nbsp; total_v = sum(list4total)
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_mean = total_v / count_sel
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Update "MEAN01" with the new_mean value&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; county.MEAN01 = new_mean
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor01.updateRow(county)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # emptying variables before restarting loop 
&amp;nbsp;&amp;nbsp;&amp;nbsp; del count_sel
&amp;nbsp;&amp;nbsp;&amp;nbsp; del list4total
&amp;nbsp;&amp;nbsp;&amp;nbsp; del total_v
&amp;nbsp;&amp;nbsp;&amp;nbsp; del new_mean
&amp;nbsp;&amp;nbsp;&amp;nbsp; del cursor02
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Alternatively this code runs the Select Layer By Location and the Calculations successfully, but fails to update the Row. Instead I get: Runtime error &amp;lt;type 'exceptions.AttributeError'&amp;gt;: 'Cursor' object has no attribute '__exit__'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I think my mistake is in this code segment of the full code that follows below: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;with arcpy.UpdateCursor(counties) as cursor02:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for co in cursor02:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; co.MEAN01 = new_mean
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor02.updateRow(co)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt; Should I leave out the iteration? &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;How do I make sure that the shapefile-row being updated in this cursor corresponds to the row in the FeatureLayer used for the SelectLayerByLocation? If this is done through a SQL statement, then I don't know where to place it or how to state it. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
selectionbase = arcpy.SearchCursor("counties_lyr")
desc = arcpy.Describe("counties_lyr")
selectionbaseShapeFieldName = desc.featureClass.shapeFieldName

for row in selectionbase:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make the selection 
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_c = arcpy.SelectLayerByLocation_management("counties_lyr", "WITHIN_A_DISTANCE", row.getValue(selectionbaseShapeFieldName), "150000 Meter", "NEW_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Count the number of selected features&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_nr = arcpy.GetCount_management(selected_c)
&amp;nbsp;&amp;nbsp;&amp;nbsp; selected_count = selected_nr.getOutput(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; count_sel = float(selected_count)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the total ZBUSH value for the selected features 
&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_selected = arcpy.SearchCursor(selected_c)
&amp;nbsp;&amp;nbsp;&amp;nbsp; list4total = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for county in curs_selected:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list4total.append(county.ZBUSH)
&amp;nbsp;&amp;nbsp;&amp;nbsp; total_v = sum(list4total)
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_mean = total_v / count_sel
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Write the mean-value to the feature&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.UpdateCursor(counties) as cursor02:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for co in cursor02:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; co.MEAN01 = new_mean
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor02.updateRow(co)&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; # Reset values at end of iteration
&amp;nbsp;&amp;nbsp;&amp;nbsp; del count_sel 
&amp;nbsp;&amp;nbsp;&amp;nbsp; del list4total
&amp;nbsp;&amp;nbsp;&amp;nbsp; del total_v&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; del new_mean 
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you in advance for your help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; Vincent&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Nov 2013 11:44:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/trouble-combining-selectlayerbylocation-with-an/m-p/311355#M24232</guid>
      <dc:creator>Vincentvan_Exel</dc:creator>
      <dc:date>2013-11-08T11:44:12Z</dc:date>
    </item>
  </channel>
</rss>

