1

I have looked through the other topic similar to this and they just don't seem to have exactly what I am looking for. I have a feature class 'AGM' that I want to update a field in based on the AGMs location to an LSD (which is also a feature class). The LSD has an ID field that I want to use as the field in the AGM.

My script thus far will update the field with the ID of the next LSD, it keeps cycling through the LSD and just putting whichever ID it is on before I manually stop it in the field. (seems the select layer by location is not working the way I had hoped)

#insert a new field (LSD) into the AGM shapefile, locate which LSD the AGM belongs to and update the new LSD field with the LSD ID

#import modules

import arcpy

#set environment
#set up workspace variable as user input
wsp = arcpy.GetParameter (0)
#arcpy.env.overwrieOutput = True


#set up variables for the AGM location, the grid (LSD) location as user input
agm = arcpy.GetParameter (1)
grid = arcpy.GetParameter (2)

#make both shapefiles a layer
arcpy.MakeFeatureLayer_management (agm, "AGM")
arcpy.MakeFeatureLayer_management (grid, "LSD")

#create new field in AGM shapefile (LSD)
arcpy.AddField_management ("AGM", "LSD", "TEXT", field_length = 40)

#create search cursor for LSD
cursor_LSD = arcpy.da.SearchCursor ("LSD", ["ID"])
#for each row in the LSD layer search for the ID
for row_LSD in cursor_LSD:
#Select the LSD ID that contains an AGM
 arcpy.SelectLayerByLocation_management ("LSD", "CONTAINS", "AGM")
#create an update cursor to update the AM with the ID of the LSD that contains it
 cursor_AGM = arcpy.da.UpdateCursor ("AGM", ["LSD"])
 #for the rows in the AGM update the LSD field with the ID of the LSD that contains it
 for row_AGM in cursor_AGM:
     row_AGM[0] = row_LSD [0]
     cursor_AGM.updateRow(row_AGM)

 #delete any locks
 del row_AGM
 del cursor_AGM
1

Your code looks strange regarding the indentation. Avoid spaces before brackets and use tabs to indent instead of spaces. Also I would check if your selection uses the correct command. I don't know what geometries you're using, but maybe "INTERSECT" would work. Finally I would use a "CLEAR_SELECTION" line just to make sure that in the next cycle the selection is uninfluenced.

Yet another tip: use the cursor with a withstatement. This way you won't have the lock problems once it's finished, as it clears itself.

Check my updated code below. If it doesn't work, please provide some more information about your problem.

import arcpy

#set environment
#set up workspace variable as user input
wsp = arcpy.GetParameter(0)
#arcpy.env.overwrieOutput = True


#set up variables for the AGM location, the grid (LSD) location as user input
agm = arcpy.GetParameter(1)
grid = arcpy.GetParameter(2)

#make both shapefiles a layer
arcpy.MakeFeatureLayer_management(agm, "AGM")
arcpy.MakeFeatureLayer_management(grid, "LSD")

#create new field in AGM shapefile (LSD)
arcpy.AddField_management("AGM", "LSD", "TEXT", field_length = 40)

with arcpy.da.SearchCursor("LSD", ["ID"]) as cursor_LSD:
    for row_LSD in cursor_LSD:
        arcpy.SelectLayerByLocation_management("LSD", "CONTAINS", "AGM", "", "NEW_SELECTION")
        with arcpy.da.UpdateCursor("AGM", ["LSD"]) as cursor_AGM:
            for row_AGM in cursor_AGM:
                row_AGM[0] = row_LSD[0]
                cursor_AGM.updateRow(row_AGM)
        arcpy.SelectLayerByAttribute_management("LSD", "CLEAR_SELECTION")
  • Thank you for the help. The indents are fine in the actual script. I had a problem putting it in as a script on here. My first time posting. I will let you know if your improvements worked. Thanx again. – travel_pixie May 18 '17 at 21:07
  • Welcome. Just to clarify: I assume your "grid" isn't an actual raster, but a polygon or some sort of feature class, right? Also you're not using the added field "TEXT" in the rest of your code snippet. Are you aware of that? – dru87 May 19 '17 at 5:42
  • 1
    You are correct, "Grid" is the name of the feature class I am using. Also, "TEXT" is telling the AddField_management tool that my new field needs to be text. The add field works, its just the cursor part that I am having difficulties with. I am trying your suggestion today and will let you know how it goes. – travel_pixie May 19 '17 at 13:39

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.