AnsweredAssumed Answered

Script is deleting rows in attribute table, no idea why

Question asked by Carusot42 on Jan 7, 2015
Latest reply on Jan 7, 2015 by Carusot42

Hello folks,


So I've actually got a couple issues here, and some help would be greatly appreciated as I've been working on this for a few days now and it needs to be wrapping up. I'm still learning python and am admittedly not very good yet.


So basically, this is a script that should:

1) Look at a point that represents an unverified well location on a map

2) Select all centroids within a given distance of that point

3) Compare the last names of centroid owners and well owners to see if any of them match

4) If there is a match, write "YES" in a column called match

5) Move on to the next well and repeat


Some issues I've run into:

1) I was having trouble making the "select by location" function work. I had a search cursor and the arcpy.SelectLayerByLocation_management acting on the row in the cursor, but it would instead select all centroids nearest all wells, and then iterate to the next well record and do the same again. I got it to select from one well at a time using advice on this forum but then it started deleting rows in my attribute tables, which leads to..

2) The title problem.


Can any kind soul lend me a hand?




While I haven't figured out why things were being deleted, I did determine that if I "select all" in the wells1 feature layer and then save as a new featureclass, the attribute tables in all of my layers (wells, wells1 and the new output) all remain intact. I'm not sure why this works, but I'll take it for now. My only remaining issue is that my for loops with the cursors exit after two iterations, one "if" and one "else"; because I do get a single YES in my match column, and the "print owner, orpsowner" is only called once before the script selects and saves.


Here is my full, updated script:


import arcpy
import datetime
current_time =

print current_time

arcpy.env.overwriteOutput = 1

# define a workspace
arcpy.env.workspace = r"C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb"

# Define input files
orps09 = 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"

# Make a feature layer
arcpy.MakeFeatureLayer_management(orps09, "orps09_FL")
arcpy.MakeFeatureLayer_management(wells, "wells1")
wells1 = "wells1"

# Create dictionary of last names of all well owners
well_owners = {}  
rows = arcpy.da.SearchCursor(wells1, ["OBJECTID", "OWNER_L"])  
for row in rows:  
    well_owners[row[0]] = str(row[1])   
del rows

# Create search cursor which will iterate through wells       
with arcpy.da.SearchCursor(wells1, ["OBJECTID"]) as cursor:
        for row in cursor:
            # set well owner name for this record
            record = row[0]
            owner = well_owners[record]
            # select by attribute  
            arcpy.SelectLayerByAttribute_management(wells1,"NEW_SELECTION","OBJECTID = {}".format(row[0]))
            # select by location
            arcpy.SelectLayerByLocation_management("orps09_FL", "WITHIN_A_DISTANCE", wells1, "0.5 kilometers", "NEW_SELECTION")

            # create search cursor which will iterate through selected orps owners
            with arcpy.da.SearchCursor("orps09_FL", ["OWNER_L"]) as orpscur:
                for row in orpscur:
                    # set orps owner name
                    orpsowner = row[0]
                    # compare owner's names    
                    if owner != orpsowner:
                        print owner, orpsowner
                        print "YES"
                        # select the row that the main well cursor is currently on
                        arcpy.SelectLayerByAttribute_management(wells1,"NEW_SELECTION","OBJECTID = {}".format(record))
                        # update the match field
                        with arcpy.da.UpdateCursor(wells1, ["match"]) as cur:
                            for row in cur:
                                row[0] = "YES"
                            del cur
                        # clear selection    
        # if the well cursor has exhausted the list and the .next() method returns a stop iteration...        
            error =
        except StopIteration:
            # select all features in the FL and save to a new featureclass
            arcpy.SelectLayerByAttribute_management(wells1,"NEW_SELECTION","OBJECTID >= 1")
            arcpy.CopyFeatures_management(wells1, "Madison_well_logs_match")
            print current_time