1

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.

3

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)
  • I tried this and recieved the following error, very similar to the original one:Executing: SelectLayerByAttribute maps NEW_SELECTION "map_name = 'Airport'" Start Time: Tue Jan 21 07:48:15 2014 ERROR 000358: Invalid expression Cannot acquire a lock. Cannot acquire a lock. Failed to execute (SelectLayerByAttribute). Failed at Tue Jan 21 07:48:15 2014 (Elapsed Time: 0.00 seconds) – user3174452 Jan 21 '14 at 15:49
  • Not sure - that expression seems to be correct. Although it says you cannot acquire a lock - which tells me that perhaps it is locked by another program. I would suggest closing all ESRI programs before running your script. – dklassen Jan 21 '14 at 17:41
  • In my tests - on a string field, this expression works fine. What is the feature class you are quering? SDE/File GDB/ Shapefile? – dklassen Jan 21 '14 at 17:51
  • I am looking at your question again and see that the "treatment maps" which you are cursoring through and the "maps" which you are selecting by attribute on - are the same layer - this cannot be done - because you are locking the file with the cursor - and looping through. I think the data processing methodology needs to be revisited. I will edit my answer to this regard. – dklassen Jan 21 '14 at 18:12
  • You are correct and thank you for your answer. I figured out a workaround by creating a dummy shapefile with the field names (map names) I was looking for and used that as my selecting layer while keeping the cursor on the original layer. It worked perfectly but if you have any suggestions on efficiency I wouldn't mind hearing them. Thanks again. – user3174452 Jan 22 '14 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.