I have seen several similar posts related to this technique but I am still having issues. The following script is intended to select all points from one shapefile that are within the boundaries of a second polygon shapefile, then edit the number of points selected into one of the polygon fields. I have not been able to get the where clause statement to function properly although from the error message it appears to be correct. Both shapefiles are within a file geodatabase and feature layers where created for both in order to use the selection functions.
Here is the code:
arcpy.MakeFeatureLayer_management("treatment_maps",'maps')
arcpy.MakeFeatureLayer_management("points_2014",'points')
cursor=arcpy.UpdateCursor("treatment_maps")
for row in cursor:
map=row.getValue('map_name')
where='"map_name"' + '=' + '\'' + str(map) + '\''
arcpy.SelectLayerByAttribute_management("maps","NEW_SELECTION",where)
arcpy.SelectLayerByLocation_management("points","COMPLETELY_WITHIN","maps")
count=arcpy.GetCount_management("points")
row.setValue('DI_Count', count)
cursor.updateRow(row)
Here is the error statement:
Executing: SelectLayerByAttribute maps NEW_SELECTION "map_name"='Airport'
Start Time: Wed Jan 15 13:00:56 2014
ERROR 000358: Invalid expression
Cannot acquire a lock.
Cannot acquire a lock.
Failed to execute (SelectLayerByAttribute).
Failed at Wed Jan 15 13:00:56 2014 (Elapsed Time: 0.00 seconds)
The treatment_maps features class contains polygons and counts (DI_Count) for points located within each polygon, which need to be updated. I'm hoping to get this script to work properly as I have around 100 polygons to update.
Best Answer
Give this a try:
where="map_name = '%s'" % (map)
Also on your get count line, if
DI_Count
is an Integer field - change it to something like this:I looks like you are wanting to select all the points within each treatment area and get a count and populate that treatment area with the point count? correct? If this is true, try the following: