I am trying to create a Python script that automate the update of roads source used in a network dataset stored in a Enterprise Geodatabase (SDE). In short, I want to truncate the roads feature class and append the new roads data into it without have to recreate the network dataset.
When I try to edit the roads feature class, I encounter an error because arcpy can't edit a feature class used by a network dataset outside an edit session.
I tried using an update cursor in an edit session but I get RuntimeError: workspace already in transaction mode
import arcpy, os, time
time1 = time.time()
ROADS_INPUT = os.path.join(r"\\dd734dd", "geo", "Geobase", "GEO03_ADDRESS.gdb", "GEO03_ADDRESS", "GEO03E08_ROADS")
WORKSPACE = os.path.join(r"\\dd734dd", "geo", "ArcGIS Server", "Connections", "Enterprise.sde")
ROADS_NA = os.path.join(r"\\dd734dd", "geo", "ArcGIS Server", "Connections", "Enterprise.sde", "NA", "ROADS_2018_04")
editSession = arcpy.da.Editor(WORKSPACE)
editSession.startEditing(False, True)
editSession.startOperation()
#truncating the NA roads table
with arcpy.da.UpdateCursor(ROADS_NA, field_names="*") as u_cursor:
for row in u_cursor:
cursor.deleteRow()
#inserting rows from input to NA roads
with arcpy.da.SearchCursor(ROADS_INPUT, field_names="*") as s_cursor:
with arcpy.da.InsertCursor(ROADS_NA, field_names="*") as i_cursor:
for row in s_cursor:
i_cursor.insertRow(row)
editSession.stopOperation()
editSession.stopEditing(True)
time2 = time.time()
print "script took {:.3f} ms".format((time2-time1)*1000.0)
Is there a strategy I could try to achieve what I am trying to do?
Note: The network dataset is stored in Enterprise Geodatabase (SDE)
EDIT:
Using arcpy.DeleteFeatures_management()
and arcpy.Append_management()
instead of cursors results in error 99999 Objects in this class cannot be updated outside and edit session [Enterprise.ROADS_2018_04]
import arcpy, os, time
time1 = time.time()
ROADS_INPUT = os.path.join(r"\\dd734dd", "geo", "Geobase", "GEO03_ADDRESS.gdb", "GEO03_ADDRESS", "GEO03E08_ROADS")
WORKSPACE = os.path.join(r"\\dd734dd", "geo", "ArcGIS Server", "Connections", "Enterprise.sde")
ROADS_NA = os.path.join(r"\\dd734dd", "geo", "ArcGIS Server", "Connections", "Enterprise.sde", "NA", "ROADS_2018_04")
editSession = arcpy.da.Editor(WORKSPACE)
editSession.startEditing(False, True)
editSession.startOperation()
arcpy.DeleteFeatures_management(ROADS_NA)
arcpy.Append_management(ROADS_INPUT, ROADS_NA)
editSession.stopOperation()
editSession.stopEditing(True)
time2 = time.time()
print "script took {:.3f} ms".format((time2-time1)*1000.0)