I am trying to copy features from a Feature Class in a GDB to a Feature Class within a Feature Dataset (Turn_Restrictions that is created by the Network Analyst process [i.e. arcpy.CreateNetworkDatasetFromTemplate_na]) in the same GDB and get
*** Remote Interpreter Reinitialized ***
2021-12-18 09:57:09.639000
[u'TR_Complex', u'TR_Simple']
[u'navigation_NetFD']
D:\Projects\navigation_Net.gdb\navigation_NetFD Turn_Restrictions
(1, (152.9809017500001, -27.267402999999973), 0.00019980302798811497, u'0000514c-4400-4600-0000-0000283baec4', u'8I', u'2', u'2103')
Traceback (most recent call last):
File "D:\Projects\Create_Network.py", line 282, in <module>
targetCursor.insertRow(row)
RuntimeError: Objects in this class cannot be updated outside an edit session [Turn_Restrictions]
I have looked at Featureclasses with attachments: objects cannot be updated outside edit session and similar. I have tried having the data editable in ArcMap while running the python code as well.
The two tables do have a different structure and I can copy/paste the records but need to use ArcPy to automate it.
The data that needs to be edited is
Data Type: File Geodatabase Feature Class
Database: D:\Projects\navigation_Net.gdb
Feature Dataset: navigation_NetFD
Feature Class: Turn_Restrictions
Feature Type: Simple
Geometry Type: Line
Coordinates have Z values: No
Coordinates have measures: No
Related code
gdbPath = r'D:\Projects\navigation_Net.gdb'
#List FC and FD in the gdb
#Ref https://gis.stackexchange.com/questions/210929/accessing-feature-class-from-file-geodatabase-for-intersect-in-arcpy
#Ref https://gis.stackexchange.com/questions/114067/listing-all-feature-datasets-and-feature-classes-from-single-geodatabase-into-cs
arcpy.env.workspace = gdbPath
shapes = arcpy.ListFeatureClasses()
print (shapes)
shapeList = list()
for shape in shapes:
print shape
#targetCursor = arcpy.da.InsertCursor(,"*")
datasetList = arcpy.ListDatasets('*','Feature')
print datasetList
for dataset in datasetList:
arcpy.env.workspace = dataset
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
if fc=="Turn_Restrictions":
print arcpy.env.workspace,fc
break
targetCursor = arcpy.da.InsertCursor(fc,"*")
# Setup a search cursor on our new data and interate the rows
with arcpy.da.SearchCursor(os.path.join(gdbPath,shape),"*") as cursor:
for row in cursor:
print (row)
#Insert into the target dataset (only Points or tabular data if using an asterisk)
targetCursor.insertRow(row)
How do I make this editable? Similar code for FC's works fine.
Is it something to do with the field_names and setting the SHAPE@ value as per https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/insertcursor-class.htm ?
EDITED CODE based on ESRI example -showing different methods I have tried.
workspace = os.path.join(arcpy.env.workspace,fc)
print workspace
edit = arcpy.da.Editor(out_gdb) #set gdb as edit space since FC didn't work
edit.startEditing(False,True)
edit.startOperation()
#targetCursor = arcpy.da.InsertCursor(fc,"*") #use new insert format outside search
# Setup a search cursor on our new data and interate the rows
with arcpy.da.SearchCursor(os.path.join(gdbPath,shape),"*") as cursor:
for row in cursor:
print (row)
#targetCursor.insertRow(row) #add row -works in gdb tables but not netork fc
editfc=arcpy.da.InsertCursor(fc,['SHAPE@XY']) #as per https://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/insertcursor-class.htm
editfc.insertRow(row)
## with arcpy.da.InsertCursor(fc, ('SHAPE@', 'Name')) as icur: # as https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/editor.htm
## icur.insertRow(row)
# Stop the edit operation.
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
Based on https://community.esri.com/t5/python-questions/runtimeerror-cannot-open-workspace/m-p/383027#M30172 I see that I can edit the gdb and fc's in it but not those in the Network dataset.
---UPDATE
The ND is created using
arcpy.CreateFileGDB_management(out_folder,out_gdb)
arcpy.CreateFeatureDataset_management(os.path.join(out_folder,out_gdb),out_fd,sr)
arcpy.CreateTurnFeatureClass_na(os.path.join(out_folder,out_gdb,out_fd),"Turn_Restrictions","5")
The structure created is explained in https://pro.arcgis.com/en/pro-app/latest/help/analysis/networks/turns-in-the-network-dataset.htm
The process I have is to then create the network from a template and use a vector layer with the turns to manually copy-paste the records from it, into the ND Turn_Restrictions file.
print ("Creating Network - may show no activity for ~10mins")
print(datetime.now())
arcpy.CheckOutExtension("Network")
arcpy.CreateNetworkDatasetFromTemplate_na("D:/Projects/cstm.xml", os.path.join(out_folder,out_gdb,out_fd))
print ('Added Network Dataset and Template')
arcpy.BuildNetwork_na(in_network_dataset=os.path.join(out_folder,out_gdb,out_fd+"_ND"))
print(datetime.now())
print ('Built network -manually copy paste TR records into Turn_Restrictions and rebuild')
print(datetime.now())
Even if we use the ArcGIS Tutorial Data - https://desktop.arcgis.com/en/arcmap/latest/extensions/network-analyst/about-the-network-analyst-tutorial-exercises.htm
I still can't access the equivalent 'RestrictedTurns' using arcpy.