3

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.

enter image description here

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.

2
  • Your title has a missing "not". There are lots of examples of how to create an edit session if you look for them.
    – Vince
    Commented Dec 18, 2021 at 1:15
  • thanks. I tried to search for it but didn't find anything clear.
    – GeorgeC
    Commented Dec 18, 2021 at 5:00

2 Answers 2

4

Use the arcpy.da.Editor to start and stop editing.
https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/editor.htm

Code samples from esri:

import arcpy
import os

fc = 'Database Connections/Portland.sde/portland.jgp.schools'
workspace = os.path.dirname(fc)

# Start an edit session. Must provide the workspace.
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, True)

# Start an edit operation
edit.startOperation()

# Insert a row into the table.
with arcpy.da.InsertCursor(fc, ('SHAPE@', 'Name')) as icur:
    icur.insertRow([(7642471.100, 686465.725), 'New School'])

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)

Sample 2:

import arcpy

fc = 'C:/Portland/Portland.gdb/Land/Parks'
workspace = 'C:/Portland/Portland.gdb'
layer_name = 'Parks'

try:
    arcpy.MakeFeatureLayer_management(fc, layer_name)
    arcpy.SelectLayerByAttribute_management(
        layer_name, 'NEW_SELECTION',
        """CUSTODIAN = 'City of Portland'""")
    with arcpy.da.Editor(workspace) as edit:
        arcpy.CalculateField_management(
            layer_name, 'Usage', '"PUBLIC"', 'PYTHON')

except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))
4
  • Thanks - I didn't find this example before. The issue also seems to be with using a FC within the FD. I have workspace = arcpy.env.workspace,fc edit = arcpy.da.Editor(workspace) edit.startEditing(False,True) edit.startOperation() - I get RuntimeError: cannot open workspace This is where workspace is = D:\Projects\navigation_Net.gdb\aus_navigation_NetFD Turn_Restrictions as per the screen grab in the question. So there are 2 related issues - opening the fc (which worked earlier as I got the error saying it needs to be editable) and now this error saying it can't open the workspace.
    – GeorgeC
    Commented Dec 18, 2021 at 5:24
  • I have also tried the solution in gis.stackexchange.com/a/158627/2891
    – GeorgeC
    Commented Dec 18, 2021 at 7:11
  • try setting the workspace as the gdb instead of the feature set within the gdb. D:\Projects\navigation_Net.gdb
    – jbalk
    Commented Dec 18, 2021 at 23:56
  • I have tried but it still didn't allow me to insert rows into Turn_Restriction
    – GeorgeC
    Commented Dec 19, 2021 at 4:26
2

I believe when making edits to network dataset participants through python, you must access the nax (network analyst python extensions) as opposed to the da. See this page for examples of loading using either the load method, or the InsertCursor.

I think the "load" option will better suit you so that you don't have to format the geometry as you extract it from an FC to insert into a NetFC.

EDIT:. It looks like PolyGeo had a problem similar to this years ago on edge and junction features.

8
  • Thanks -it is confusing as I am trying to add to the Turn_Restrictions table and I don't see this listed. I have tried the following but get an error saying Turn_Restrictions is no defined - fields = ["UFI", "FROM_UFI", "TO_UFI","OBJECTID", "SHAPE@XY"] with arcpy.da.SearchCursor(os.path.join(gdbPath,shape),"*") as cursor: for row in cursor: with Turn_Restrictions.insertCursor(arcpy.nax.Turn_RestrictionsInputDataType.Turn_Restrictions, fields) as cur: cur.insertRow(row)
    – GeorgeC
    Commented Dec 19, 2021 at 4:23
  • Can you please update your question to show how this network dataset was initially created? It may be that you have to starry a new one, making sure that turn restrictions are enabled at creation time. Commented Dec 19, 2021 at 21:30
  • I have updated the q.
    – GeorgeC
    Commented Dec 19, 2021 at 21:58
  • I have updated the question and added some points. It would be great if you can help.
    – GeorgeC
    Commented Jan 16, 2022 at 20:12
  • Sorry for delays. Before I revise my answer, can you try two things and comment back? Can you 1.use the Network Dataset Properties Explorer to explore your ND and see if the Turns FC is included (ie shown in the GUI in the directions segment.) Then 2. See if you can revise the ND creation template to use your FC as input to the ND creation step? Commented Jan 17, 2022 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.