AnsweredAssumed Answered

Issues with feature layers and python

Question asked by Carusot42 on Dec 31, 2014
Latest reply on Jan 6, 2015 by Carusot42

Hey folks,


Pretty new to GIS and scripting (in any language) and I'm trying to write a program that will, basically:


1) look at a point that represents an unverified well location

2) search for all centroids within a certain distance and select them

3) compare the last name of the well owner with the last name of all selected centroid owners

4) if there is a match between any, put "yes" in a "match" column


Here are the issues I'm having:

1) the arcpy.SelectLayerByLocation function requires a feature layer input. I added a line in my script that makes my centroid data into a feature layer, which actually creates a layer in my table of contents, and when I fix an error and try to run the script again, it stops almost immediately saying that the feature layer already exists. If I take it out of the script, then the feature layer is undefined for later. My supervisor (who has a good amount of GIS experience) says something is happening weirdly there and is unsure why.

2) lock issues. Every time I try to run it and it gets past the feature layer part, I get this error:


Runtime error  Traceback (most recent call last):   File "<string>", line 17, in <module>   File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\", line 6618, in SelectLayerByLocation     raise e ExecuteError: ERROR 999999: Error executing function. Cannot acquire a lock. Cannot acquire a lock. Item not found in this collection. Failed to execute (SelectLayerByLocation). 


Can anyone point me in the right direction here?


Here is the code I've written


import arcpy
# define a workspace
arcpy.env.workspace = r"C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb"
# Define input files
orps = r"C:\Users\tmc18\Desktop\comp_orps\centroids\madirps_point1.shp"
wells = r"C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb\Madison_DEC_Well_Logs_3_14_14_MASTER_COPY1"
arcpy.MakeFeatureLayer_management(orps, "orpsFL")
# Create well update cursor, will compare the "owner_last" field value of the well used to select nearby centroids to the "owner_last"
# field of centroid data. 
with arcpy.da.UpdateCursor(wells, ["match", "owner_last"]) as cursor:
        for row in cursor:
                # select by location
                arcpy.SelectLayerByLocation_management("orpsFL", "WITHIN_A_DISTANCE", wells, "0.5 kilometers", "new_selection")
                # create orps name cursor
                orps_match = arcpy.da.SearchCursor("orpsFL", ["owner_last"])
                # if the uppercase string value of the well owner's last name != the centroid, then the orps_match cursor will move to the next
                # centroid name and try that one.
                if str([row[1] for row in cursor]).upper != str([row[0] for row in orps_match]).upper:
                # but if they do match, we get a "yes" in the match column
                        row[0] = "YES"


Thanks in advance!