Arcpy da Editor confusing

10708
26
Jump to solution
07-30-2014 01:43 AM
Geography
New Contributor

Hi all,

I'm still new to arcpy and python. PLEASE can somebody explain why the first two lines of code create a runtime error, but the last two lines run fine?

>>> with arcpy.da.Editor(workspace) as edit:

...     edit.startEditing(False, True)

...    

Runtime error

Traceback (most recent call last):

  File "<string>", line 2, in <module>

AttributeError: 'Workspace Operation object' object has no attribute 'startEditing'

>>> edit = arcpy.da.Editor(workspace)

>>> edit.startEditing(False, True)

THX!!

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
RiyasDeen
Occasional Contributor III

May be this helps.

import arcpy

import os

fc  = str(sys.argv[1])

templine = fc + "_line_temp"

workspace = os.path.dirname(fc)

fields = []

for f in arcpy.ListFields(fc):

  if f.type != "OID":

  fields.append(f.name)

arcpy.SplitLine_management(fc, templine)

arcpy.DeleteFeatures_management(fc)

# if feature class is not in feature dataset then use arcpy.da.Editor(workspace)

with arcpy.da.Editor(os.path.dirname(workspace)) as edit:

  edit.startEditing(False, False)

  edit.startOperation()

  with arcpy.da.SearchCursor(templine,["Shape@"]) as sc:

  with arcpy.da.InsertCursor(fc, ["Shape@"]) as ic:

  for row in sc:

  ic.insertRow(row)

  del ic

  del sc

  edit.stopOperation()

  edit.stopEditing(True)

arcpy.Delete_management(templine)

View solution in original post

0 Kudos
26 Replies
DanPatterson_Retired
MVP Emeritus

From my reading, the with statement handles the appropriate start operations so you second line is superfluous as shown in Example 1

0 Kudos
Geography
New Contributor

Thanks for the quick reply Dan!! I though so, too. However, if I want to run the code (cf. below) I get another runtime error:

  1. >>> with arcpy.da.Editor(workspace) as edit:
  2. ...     with arcpy.da.InsertCursor(workspace+fc, ["Shape@","LineType"]) as ic:
  3. ...         for f in fl:
  4. ...             ic.insertRow(f)
  5. ...            
  6. Runtime error
  7. Traceback (most recent call last):
  8.   File "<string>", line 4, in <module>
  9. RuntimeError: An edit operation is required.

using the with statement together with the edit.startOperation() also fails with the following runtime error - so I'm confused.

  1. >>> with arcpy.da.Editor(workspace) as edit:
  2. ...     with arcpy.da.InsertCursor(workspace+fc, ["Shape@","LineType"]) as ic:
  3. ...         for f in fl:
  4. ...             edit.startOperation()
  5. ...             ic.insertRow(f)
  6. ...             edit.stopOperation()
  7. ...            
  8. Runtime error
  9. Traceback (most recent call last):
  10.   File "<string>", line 4, in <module>
  11. AttributeError: 'Workspace Operation object' object has no attribute 'startOperation'

btw. Sorry but how do I format code in the answer?

0 Kudos
DanPatterson_Retired
MVP Emeritus

You have a thing about with statements check the syntax in the arcpy.da.insertcursor of the help file for using insertcursors

0 Kudos
Geography
New Contributor

Please don't get mad at me, but I did cross check with the help file before, and the insertcursor seems to be okay, because it works nicely when I use it without the with edit statement. So PLEASE what am I missing? (FYI: I need to open an edit session, because I want to edit a feature class which is part of a topology feature data set.)

0 Kudos
DanPatterson_Retired
MVP Emeritus

can't help you with the latter part...thought you were editing simple features

0 Kudos
Geography
New Contributor

Never mind thanks anyway!! I think I'll post the entire code tomorrow.

0 Kudos
ToddUlery
Occasional Contributor

Could you post the part of your code where you set the environement/workspace? I would like to look at that part.
Just to get more clarity on the situation.
The error is telling me that there is an issue there. An issue that says the object you have set does not work in an edit session.

0 Kudos
Geography
New Contributor

Basically what I want to archive: Create single lines out of one selected polyline with many vertex points. I do a cut, copy, and paste process as follows and it does the trick.

# (cut)

arcpy.SplitLine_management("FGK_t0_Line","line_tmpvar")

arcpy.DeleteFeatures_management("FGK_t0_Line")

env = EnvVar()

cmd = Commands()

# container for all lines (copy)

fl=[]

with arcpy.da.SearchCursor("line_tmpvar",["Shape@","LineType"]) as sc:

   for row in sc:

        fl.append(row)

# start edit session to insert (paste), because of topograpy relationship/feature dataset

cmd.clsAllSelections()

edit = arcpy.da.Editor(env.wd)

edit.startEditing(False, True)

edit.startOperation()

with arcpy.da.InsertCursor(env.wd+r"\\TopoTestSet\\FGK_t0_Line", ["Shape@","LineType"]) as ic:

    for f in fl:

        ic.insertRow(f)

edit.stopOperation()

edit.stopEditing(True)

However, I would like to use the with edit command instead of the lines 13, 14, 15, 19 and 20. Therefore, I was trying to use it as follows:

# (cut)

arcpy.SplitLine_management("FGK_t0_Line","line_tmpvar")

arcpy.DeleteFeatures_management("FGK_t0_Line")

env = EnvVar()

cmd = Commands()

# container for all lines (copy)

fl=[]

with arcpy.da.SearchCursor("line_tmpvar",["Shape@","LineType"]) as sc:

   for row in sc:

        fl.append(row)

# start edit session to insert (paste), because of topograpy relationship/feature dataset

cmd.clsAllSelections()

with arcpy.da.Editor(env.wd) as edit:

    with arcpy.da.InsertCursor(env.wd+r"\\TopoTestSet\\FGK_t0_Line", ["Shape@","LineType"]) as ic:

        for f in fl:

            edit.startOperation()

            ic.insertRow(f)

            edit.stopOperation()

It always fails with the runtime error:

Runtime error

Traceback (most recent call last):

  File "<string>", line 16, in <module>

AttributeError: 'Workspace Operation object' object has no attribute 'startOperation'

As you can see I have all my environment settings saved in an object called "env", which worked in the first part. So what am I doing wrong?

>>> print env.wd

C:\Data\DigitestV02.gdb

0 Kudos
RiyasDeen
Occasional Contributor III

Edit operations must be controlled within the context of an edit session, and edit operations cannot be nested within other edit operations.

Refer: ArcGIS Help 10.1

try calling edit.startEditing @ line 14.

0 Kudos