[GIS] Debugging Select By Attribute tool within loop giving ERROR 000358: Invalid expression Cannot acquire a lock

arcpyerror-000358lockselect-by-attributeselect-by-location

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:

count = int(arcpy.GetCount_management(ds).getCount(0))

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:

# treatment_maps_ds - this is the name of your input polygon dataset
treatment_area_list = [row.map_name for row in arcpy.SearchCursor(treatment_maps_ds)]
# point_ds - this is the name of your input point dataset
arcpy.MakeFeatureLayer_management(point_ds, "ptslyr")
arcpy.MakeFeatureLayer_management(treatment_map_ds, "treatmentlyr")

# Loops through each treatment 'map_name'
treatdict = {}
for treatmap in treatment_area_list:
    arcpy.SelectLayerByAttribute_management("treatmentlyr", "NEW_SELECTION", "map_name = '" + str(treatmap) + "'")
    arcpy.SelectLayerByLocation_management("pntlyr", "INTERSECTS", "", "treatmentlyr")
    cnt = int(arcpy.GetCount_management("pntlyr").getOutput(0))
    treatdict[treatmap] = cnt

cur,row = None, None
cur = arcpy.UpdateCursor(treatment_maps_ds)
for row in cur:
    cnt = treatdict[row.map_name]
    row.DI_Count = cnt
    cur.updateRow(row)
Related Question