How do you insert both attributes and geometry with an InsertCursor?

3435
5
Jump to solution
03-04-2016 04:12 AM
DanEvans
Occasional Contributor II

I am trying to use an insert cursor to populate several fields with attributes and also the SHAPE field with a multi-part polyline, but I keep getting a "can only concatenate tuple (not "list") to tuple" error. I don't understand, because it works to insert just the SHAPE surrounded by square brackets, and my row from an outer SearchCursor works as a list everywhere else! (Converting to a tuple doesn't work either!).

Here is my code:

      # Insert the current superstring into the output feature class
      insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', dissolveFields + ["SHAPE@"])
      multipartPolyline = arcpy.Polyline(superstringPartsArray)
      insertCursor2.insertRow(row + [[multipartPolyline]])
      del insertCursor2

'dissolveFields' is the list of fields whose attributes I want to update with 'row', which is the current row of a table of unique combinations (from the summary statistics tool), which has the same fields as in the dissolveFields list.

Please help!

0 Kudos
1 Solution

Accepted Solutions
DanEvans
Occasional Contributor II

Thanks, managed to solve it though.

Turns out converting to a tuple did work, I just had to keep the square brackets around multipartPolyline - although actually what I ended up doing was instead converting 'row' to a list as I had a few other fields to add and it made sense to only do one conversion to list rather than multiple to tuple!

Now my code looks like this:

      # Insert the current superstring into the output feature class
      insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', dissolveFields + ["FREQUENCY", "SUM_SHAPE_Length", "SHAPE@"])
      multipartPolyline = arcpy.Polyline(superstringPartsArray)
      insertCursor2.insertRow(list(row) + [count] + [multipartPolyline.length] + [multipartPolyline])
      del insertCursor2

I just don't understand how 'row', which I treat like a list with no problems earlier on in the code, has suddenly become a tuple!

View solution in original post

0 Kudos
5 Replies
LukeWebb
Occasional Contributor III

EDIT: Ok I think I understand a bit better after rereading, I think what you are trying to do should work, however I have had issues doing it like that! My implementation wasnt how I wanted it to be, but went like this:

(Code may only work with standard InsertCursor, not the .da version!)

# Insert the current superstring into the output feature class
cursorFieldList = dissolveFields
cursorFieldList.append("SHAPE@")
insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', cursorFieldList)
#Create empty template row object
insertRow = insertCursor2.newRow()
multipartPolyline = arcpy.Polyline(superstringPartsArray)


#Set geometry
insertRow.shape = multipartPolyline


#Set other fields
insertRow.setValue(field1, row.getValue(field1))
insertRow.setValue(field2, row.getValue(field2))
insertCursor2.insertRow(insertRow)


del insertCursor2, insertRow
DanEvans
Occasional Contributor II

Thanks, managed to solve it though.

Turns out converting to a tuple did work, I just had to keep the square brackets around multipartPolyline - although actually what I ended up doing was instead converting 'row' to a list as I had a few other fields to add and it made sense to only do one conversion to list rather than multiple to tuple!

Now my code looks like this:

      # Insert the current superstring into the output feature class
      insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', dissolveFields + ["FREQUENCY", "SUM_SHAPE_Length", "SHAPE@"])
      multipartPolyline = arcpy.Polyline(superstringPartsArray)
      insertCursor2.insertRow(list(row) + [count] + [multipartPolyline.length] + [multipartPolyline])
      del insertCursor2

I just don't understand how 'row', which I treat like a list with no problems earlier on in the code, has suddenly become a tuple!

0 Kudos
WesMiller
Regular Contributor III

You are using a plus sign in your fields list which means it's trying to add them together. Change to

insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', [dissolveFields, "SHAPE@"])

DanEvans
Occasional Contributor II

Would that not add 'dissolveFields' as a list element within the list?

0 Kudos
WesMiller
Regular Contributor III

I hope this link works InsertCursor—Help | ArcGIS for Desktop please see the examples at the bottom of the page

EDIT:

You would create a field list like below:

field_list = [field1,field2,field3]

When you use the plus you are adding or concatenating:

1+1 =2

"value" + "othervalue" = "valueothervalue"

0 Kudos