I'm running an arcpy code in IDLE that update a field in shapefile. After updating it get the following error:

Traceback (most recent call last): File "C:/Users/10/Desktop/test.py", line 21, in cursor.updateRow(row_st) RuntimeError: Object invalid or no longer set.

And, here is the code:

from arcpy.sa import *
import arcpy
from arcpy import env
env.workspace = r"D:\Phd-Files\dataset.mdb"
env.overwriteOutput = True

fields_Street=["OBJECTID","SHAPE@","t_light_de", "st_dense", "bikeLn_den", "Buff_area", "len"]

cursor= arcpy.UpdateCursor("Boston_Street_Buff_10_Joint", fields_Street)
for row_st in cursor:
    geom = row_st.getValue('Shape')
    # calculate street density 
    arcpy.Buffer_analysis(geom, "geom_buff", "10 Meters")
    cursor_buff= arcpy.SearchCursor("geom_buff", ["ORIG_FID", "Shape_Area"])
    for row_buff in cursor_buff:
        _area_buff=_area_buff+ row_buff.getValue('Shape_Area')
        row_st.setValue ('st_dense' , row_st.getValue('Buff_area')/_area_buff)
    del cursor_buff, row_buff
    print ('street_ObjectID={0}, bike_lane density= {1}'.format(row_st.getValue('OBJECTID'), row_st.getValue('st_dense')))
  • 2
    Just a comment, I don't believe you need to delete the cursor using the del statement as you are using the cursors within a with statement which is supposed to guarantee release of resources, well that is my understanding – Hornbydd Oct 25 '18 at 19:25
  • 1
    It seems as the updateRow() method is fed with an invalid object. Try to examine the row_st before you are trying to update the row. Also, I would strongly suggest rewriting the code where you work with area_buff and row_buff. I've been trying to follow the code but all those index-based access with [-1] and [-4] makes it so difficult. namedtuple would be handy here. – Alex Tereshenkov Oct 25 '18 at 19:29
  • Totally agree with @AlexTereshenkov your use of index-based access of the row object is Very difficult to follow. – Hornbydd Oct 25 '18 at 19:32
  • @AlexTereshenkov your right. code is modified. – Reza Oct 25 '18 at 19:48
  • 1
    First, arcpy.UpdateCursor and arcpy.SearchCursor are deprecated in favor of the "data access" versions, arcpy.da.UpdateCursor and arcpy.da.SearchCursor. I would recommend using those instead. Secondly, geometry objects have a geom.buffer() method which would allow you to avoid running the Buffer_analysis inside of the loop and therefore avoid having a nested search cursor. They also have an area attribute. – mikewatt Oct 26 '18 at 0:18

I use shp file instead of mdb and it dosent get error. As it works on shp, i have to update Shape_Area at the buffer. i use expression1 = "{0}".format("!SHAPE.area@SQUERMETERS!") arcpy.CalculateField_management("geom_buff.shp", "Shape_Area", expression1, "PYTHON") and that works for me.

| improve this answer | |

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.