with arcpy.da.SearchCursor(points, ('NEARBYADDRESS', 'BLOCK','STREET','SHAPE@X', 'SHAPE@Y', 'OID@')) as cursor: for row in cursor: #if row[5] < 100: thisoid = str(row[5]) point_info = getNearestAddress(row[3], row[4]) #returns a list like this ['Street Address','Block Description','Street Name'] or if no address found ['No Address Nearby'] if str(point_info) == "No Address Nearby": myinfo[thisoid] = {"address" : "No Address Nearby"} myinfo[thisoid]["block"] = "Unknown" myinfo[thisoid]["street"] = "Unknown" else: myinfo[thisoid] = {"address" : "".join(point_info[0])} #first value in the dictionary looks like this... create a key myinfo[thisoid]["block"] = "".join(point_info[1]) myinfo[thisoid]["street"] = "".join(point_info[2]) #the resulting dictionary of a couple of entries will look like this: #{123: {'address': "123 Main Street", 'block': "100 Block of Main Street", 'street': "Main Street"}, #456: {'address': "456 South Street", 'block': "400 Block of South Street", 'street': "South Street"}}
edit = arcpy.da.Editor(workspace) # Edit session is started without an undo/redo stack for versioned data # (for second argument, use False for unversioned data) edit.startEditing(False, False) # Start an edit operation edit.startOperation() # Insert a row into the table. with arcpy.da.UpdateCursor(points, ('NEARBYADDRESS','BLOCK','STREET','OID@')) as cursor: for row in cursor: print "Currently on... " + str(row[3]) if row[3] < 100: ##This will successfully save to the feature class! row[0] = "123 Dummy Address" row[1] = "100 Block of Dummy" row[2] = "Dummy" ##This will NOT save to the feature class!! row[0] = myinfo[str(row[3])]["address"] row[1] = myinfo[str(row[3])]["block"] row[2] = myinfo[str(row[3])]["street"] cursor.updateRow(row) totalpointsupdated +=1 # Stop the edit operation. THIS IS WHERE THE ERROR OCCURS edit.stopOperation() # Stop the edit session and save the changes edit.stopEditing(True)
try: ID_check = entry[0] print entry[0] intable = "in_memory\ID_Check" counter = 0 with da.SearchCursor(intable,["ID"]) as cursor: for row in cursor: if row[0] == ID_check: counter += 1 if counter >= 1: pythonaddins.MessageBox("This Feature's ID is a duplicate. Solve thi s error before continuing!","Error",0) raise Exception() elif counter == 0: print "All is good, this ID is not a duplicate." cursor = arcpy.da.InsertCursor(intable,["ID"]) cursor.insertRow([ID_check]) del cursor edit = da.Editor(workspace) edit.startEditing(False,True) edit.startOperation() with da.UpdateCursor(targetlayer,fields) as cursor: for row in cursor: row = entry print "row: {}".format(row) cursor.updateRow(row) del cursor del row edit.stopOperation() #edit.stopEditing(True) except Exception as e: print e.message print arcpy.GetMessages() arcpy.RefreshActiveView() print "Complete"
Your code looks sound except you forgot your populated 'myinfo' dictionary OID keys are integers...your 'lookup' update cursor code is attempting to use string keys. That creates an exception which isn't really handled - I won't go into that, but you have enough to know how to modify your code.
BTW, I may not know what the whole enchilada is on your OID handling -maybe you've got that adequately covered- but don't know how advisable it is to use the OID from one table to use in updating another table?
So, bottom line, your code should at least run with "myinfo[row[3]]['...']" statements.
Wayne
try: ID_check = entry[0] print entry[0] intable = "in_memory\ID_Check" counter = 0 with da.SearchCursor(intable,["ID"]) as cursor: for row in cursor: if row[0] == ID_check: counter += 1 if counter >= 1: pythonaddins.MessageBox("This Feature's ID is a duplicate. Solve thi s error before continuing!","Error",0) raise Exception() elif counter == 0: print "All is good, this ID is not a duplicate." cursor = arcpy.da.InsertCursor(intable,["ID"]) cursor.insertRow([ID_check]) del cursor edit = da.Editor(workspace) edit.startEditing(False,True) edit.startOperation() with da.UpdateCursor(targetlayer,fields) as cursor: for row in cursor: row = entry print "row: {}".format(row) cursor.updateRow(row) del cursor del row edit.stopOperation() #edit.stopEditing(True) except Exception as e: print e.message print arcpy.GetMessages() arcpy.RefreshActiveView() print "Complete"
When using "print edit.isEditing" after each edit method they are all true. When I uncomment the line edit.stopEditing(True) an exception is thrown before the print statement saying Start Edit Session. This happens in a versioned sde environment. When using the same code in a personal geodatabase the exception message states something like "unable to perform within an edit session."
So my solution for sde is to comment out the stop editing method. This works however if I create a feature then save edits. If I later change the attributes or geometry of that feature the state of the geodatabase changes and the edits cannot be saved. "Attempting to perform action on a closed state. Unable to save edits."
From my research into this the cursor is locking itself out due to the auto commit. Interestingly setting it to 0 doesn't work so I just set it stupid high and it has resolved in my use case.
arcpy.env.autoCommit = 1000000