arcpy.da.InsertCursor only inserting last row in list of data

3326
18
Jump to solution
10-17-2017 01:40 PM
HannesZiegler2
Occasional Contributor II

Hello, I'm working on a script that gathers a bunch of line geometry (created using arcpy.Polyline function) into a list (called lines in the script), and then inserts this geometry into an empty polygon featureclass (called outln_m in the script). However, for some reason, only the last item in the list is there in the output polyline featureclass after the script runs, and I'm baffled. Can anyone shed some light on what might be causing this? I checked and all my cursor/row objects are being deleted before creating a new cursor. I tried wrapping the whole thing in an editor session but that did not change the result.

And the result of my debugging print statements:

You can see the list contains 25 items, but only one record is written to the output featureclass after running the list through a for-loop. That record happens to always be the last item in the list.

Thank you for any help! I'm sure I'm missing something obvious.. I just can't see it.

18 Replies
MitchHolley1
MVP Regular Contributor

I think you need to insert actual coordinate values and not geometry objects. 

0 Kudos
HannesZiegler2
Occasional Contributor II

But I do the same for a point featureclass, bt_points, and it works just fine using the exact same code (except it uses point geometry instead of Polyline geometry objects).

  1. ##Populate output bottom of dam point featureclass  
  2. cursor = arcpy.da.InsertCursor(outpt, ["SHAPE@", fid, "BottomOfDam_Elevation"])  
  3. for row in bt_points:  
  4.     cursor.insertRow(row)  
  5. del cursor, row  

0 Kudos
MitchHolley1
MVP Regular Contributor

Okay... are you trying to insert OBJECTID values?  The cursor has a field name of fid.  Or is that just a text type field?

0 Kudos
HannesZiegler2
Occasional Contributor II

The fid is just a text object with a unique id for each item - but not the object id. It's something like 'D 1403'. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

hmmm try adding a cursor.reset() in your code next time you run it... I have found a few sketchy situations where using cursors once results in either the last element being used the next time, or an empty value... I think it is a generator/list thing, but a reset to make sure it is at the beginning can't hurt. 

Sadly their cursor examples are lacking in those things that are 'code killers'

Examples of things that cause code failures would be more useful than just the success stories

0 Kudos
HannesZiegler2
Occasional Contributor II

Hi Dan, does that method work for da.InsertCursors? I am getting an error message, I can also not find any satisfactory documentation of this method.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Your original question says you're trying to insert polyline geometries into a polygon feature class. Is that correct?

0 Kudos
HannesZiegler2
Occasional Contributor II

Oh sorry, my mistake. No, it is a polyline featureclass.

0 Kudos
HannesZiegler2
Occasional Contributor II

Okay, I've found the problem, and it's not a bug or anything complicated, just an oversight. Earlier in the script, in order to minimize processing time I set a custom extent around each input point to work with only a small piece of the larger input raster. I forgot to reset the extent to arcpy.env.extent = "MAXOF" at the end of this processing, and so it was stuck on the extent of the last item processed. GetCount and InterpolateShape both honor the processing extent, while the InsertCursor (or rather the list of geometry objects I feed it) does not. Problem solved.

*EDIT 10/24/2017*

^ I was in a rush when I posted this, thank you all for the suggestions and help on cracking this!