Issues with feature layers and python

4869
2
Jump to solution
12-31-2014 09:45 AM
ThomasCaruso
New Contributor III

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\management.py", 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:
                        orps_match.next
                        
                # but if they do match, we get a "yes" in the match column
                else:
                        row[0] = "YES"
                        cursor.updateRow(row)
                        continue

Thanks in advance!

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Thomas,

You can add the following line below you 'import arcpy' statement to overwrite any output layers, in this case your Feature Layer.

arcpy.env.overwriteOutput = 1

See if that will help with the lock issue you are receiving as well.

View solution in original post

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Thomas,

You can add the following line below you 'import arcpy' statement to overwrite any output layers, in this case your Feature Layer.

arcpy.env.overwriteOutput = 1

See if that will help with the lock issue you are receiving as well.

0 Kudos
ThomasCaruso
New Contributor III

Hi Jake,

Thank you, that solved that little hangup. The lock issue was my own fault, it was because I made my cursor an update cursor which locked the table I was trying to select features from. Thanks again for your help!

0 Kudos