Hi,
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.
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.
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.)
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.
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 <class 'arcgisscripting.ExecuteError'>: 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)."
This error results from the following code:
Alternatively this code runs the Select Layer By Location and the Calculations successfully, but fails to update the Row. Instead I get: Runtime error <type 'exceptions.AttributeError'>: 'Cursor' object has no attribute '__exit__'
I think my mistake is in this code segment of the full code that follows below:
Should I leave out the iteration?
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.
Thank you in advance for your help!
Vincent
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.
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.
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.)
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.
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 <class 'arcgisscripting.ExecuteError'>: 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)."
This error results from the following code:
Code:
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:
# Starting the Select Layer By Location
selected_c = arcpy.SelectLayerByLocation_management("counties_lyr", "WITHIN_A_DISTANCE", row.getValue(selectionbaseShapeFieldName), "150000 Meter", "NEW_SELECTION")
# Counting the number of selected features to be used in the calculation of the Mean
selected_nr = arcpy.GetCount_management(selected_c)
selected_count = selected_nr.getOutput(0)
count_sel = float(selected_count)
# Calculating the total of the ZBUSH value to be used in the Mean
curs_selected = arcpy.SearchCursor(selected_c)
list4total = []
for co in curs_selected:
list4total.append(co.ZBUSH)
total_v = sum(list4total)
new_mean = total_v / count_sel
# Update "MEAN01" with the new_mean value
county.MEAN01 = new_mean
cursor01.updateRow(county)
# emptying variables before restarting loop
del count_sel
del list4total
del total_v
del new_mean
del cursor02
I think my mistake is in this code segment of the full code that follows below:
Code:
with arcpy.UpdateCursor(counties) as cursor02:
for co in cursor02:
co.MEAN01 = new_mean
cursor02.updateRow(co)
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.
Code:
import arcpy
selectionbase = arcpy.SearchCursor("counties_lyr")
desc = arcpy.Describe("counties_lyr")
selectionbaseShapeFieldName = desc.featureClass.shapeFieldName
for row in selectionbase:
# Make the selection
selected_c = arcpy.SelectLayerByLocation_management("counties_lyr", "WITHIN_A_DISTANCE", row.getValue(selectionbaseShapeFieldName), "150000 Meter", "NEW_SELECTION")
# Count the number of selected features
selected_nr = arcpy.GetCount_management(selected_c)
selected_count = selected_nr.getOutput(0)
count_sel = float(selected_count)
# Get the total ZBUSH value for the selected features
curs_selected = arcpy.SearchCursor(selected_c)
list4total = []
for county in curs_selected:
list4total.append(county.ZBUSH)
total_v = sum(list4total)
new_mean = total_v / count_sel
# Write the mean-value to the feature
with arcpy.UpdateCursor(counties) as cursor02:
for co in cursor02:
co.MEAN01 = new_mean
cursor02.updateRow(co)
# Reset values at end of iteration
del count_sel
del list4total
del total_v
del new_mean
Vincent