Arcpy da Editor confusing

10775
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
26 Replies
Geography
New Contributor

Thanks Riyas!! So you're suggesting I should avoid the insertcursor, because the help files talk about

Edit sessions and cursors

Cursors should be scoped to a single edit operation. This means a new cursor should be instantiated and released for each operation. This is very important when editing rows returned by a cursor because the rows are tied to a specific state of the geodatabase. Avoid using a cursor across multiple edit operations. Doing so can result in unexpected behavior and data loss.

when using them? If so, how could I insert multiple records then?

0 Kudos
DanPatterson_Retired
MVP Emeritus

once the da.*cursor is established, they use the for row in cursor syntax   See the da.updatecursor ArcGIS Help (10.2, 10.2.1, and 10.2.2)‌ example, they use "for row in cursor" syntax

0 Kudos
Geography
New Contributor

Right Dan. You can see from my code above that I'm using it already . My problem seems to be the nesting of the "with edit" and "with cursor" structure. Otherwise, are you suggesting I could do the whole "cut, copy, paste procedure" within a single updatecursor?

0 Kudos
DanPatterson_Retired
MVP Emeritus

You could give it a try if all the operations are being performed on a row by row basis

0 Kudos
RiyasDeen
Occasional Contributor III

try replacing your code line 13 through 18 with below, suspect this should work. Also make sure you are able to edit the feature class from arcmap independent of your script just in case.

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

    #  use False for unversioned data

    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)

0 Kudos
Geography
New Contributor

Again many thanks!

Riyas I tried your suggestion, unfortunately it stops with the same runtime error. I'm confident that I can access, and edit the data, because the 1st section of the above code works.

Dan I'll consider your suggestion more seriously - it might be an even better solution. But I've to admit that I don't know if  it's possible to update one single part feature (one row) into many single part features (many rows).

0 Kudos
DanPatterson_Retired
MVP Emeritus

See the deconstruct option within the da.updatecursor section

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

0 Kudos
RiyasDeen
Occasional Contributor III

Hi Mate,

managed to create a working sample for you. Attached folder contains a fgdb and toolbox with script tool.

Hope this helps you.

0 Kudos
ToddUlery
Occasional Contributor

fc  = str(sys.argv[1])

templine = fc + "_line_temp"

workspace = os.path.dirname(fc)

This was the part I was trying to get at. The object being used as the env for edits.

If you try to find attributes for the obj that you have set as the edit env, you wont.

The method above is setting the edit on a simple string path referencing the GDB. All of the work arounds have been successful using the edit with a string.

This method works great.

0 Kudos
Geography
New Contributor

Again many thanks Riyas! I took a look at your code, its good and I think it does solve the task. However, I was looking for code using the with statement together with edit. Something like:

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

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

best regards

0 Kudos