AnsweredAssumed Answered

Which is the better way of using edit session and insert cursors?

Question asked by bogibrynjar on Sep 13, 2016
Latest reply on Sep 14, 2016 by bixb0012

Greetings!

 

I would like to pose a question about two snippets of Python code that basically take in a list of events, each of which contain a latitude, longitude and depth like so eventslist = [[ lat1, lng1, depth1], [lat2,lng2, depth2] ... [latN, lngN, depthN]] and input these as points into two separate Geodatabase Featureclasses. I have verified that both these code snippets work but I would like to ask, which snippet in your opinion is the more efficient/correct one in regards to where to start/end the edit operation (edit.startOperation()) and creation/deletion of the insert cursors.

 

Also if you think another solution is even more preferable then please don't hesitate to suggest, thanks in advance!

 

SNIPPET 1

#Start an edit session to insert new events. Need to have edit session because of using two insert cursors at the same time. 
edit = arcpy.da.Editor(env.workspace)
edit.startEditing(False, False)

#Iterate over the list of new events, each event contains lat, lng and depth
    for event in newevents: 
        edit.startOperation()

        cursor = arcpy.da.InsertCursor(inFc, ["SHAPE@", "lat", "lng", "depth"])
        cursor2 = arcpy.da.InsertCursor(archiveFc, ["SHAPE@", "lat", "lng", "depth"])

        lat,lng,Z = (event[0], event[1], event[2])
        location = arcpy.Point(lng, lat, Z) #Create the 3-D point object
        pointGeom = arcpy.PointGeometry(location) #Create the shape geometry as pointGeometry

        row = [pointGeom, lat, lng, depth]
        row2 = [pointGeom, lat, lng, depth]

        cursor.insertRow(row) #Insert the new events into longterm class using insert cursor
        cursor2.insertRow(row2) #Insert the new events into archive class using insert cursor2

        del row, row2
        del cursor, cursor2
  
        edit.stopOperation()

edit.stopEditing(True)

 

SNIPPET 2

#Start an edit session to insert new events. Need to have edit session because of using two insert cursors at the same time. 
edit = arcpy.da.Editor(env.workspace)
edit.startEditing(False, False)
edit.startOperation()

cursor = arcpy.da.InsertCursor(inFc, ["SHAPE@", "lat", "lng", "depth"])
cursor2 = arcpy.da.InsertCursor(archiveFc, ["SHAPE@", "lat", "lng", "depth"])

#Iterate over the list of new events, each event is a list that contains lat, lng and depth
for event in newevents:
 
    lat,lng,Z = (event[0], event[1], event[2])
    location = arcpy.Point(lng, lat, Z) #Create the 3-D point object
    pointGeom = arcpy.PointGeometry(location) #Create the shape geometry as pointGeometry

    row = [pointGeom, lat, lng, depth]
    row2 = [pointGeom, lat, lng, depth]

    cursor.insertRow(row) #Insert the new events into longterm class using insert cursor
    cursor2.insertRow(row2) #Insert the new events into archive class using insert cursor2

    del row, row2

edit.stopOperation()

del cursor, cursor2 

edit.stopEditing(True)

Outcomes