Select to view content in your preferred language

Insert.cursor Error While Copying Geometries

1669
6
Jump to solution
03-24-2022 04:41 PM
Jefcoat_Nathan
Regular Contributor

Hi,

I'm trying to insert/create geometries using "feature A" geometries  into feature B, which has null geometries .  It seems simple enough but I'm not well acquainted with programming.  This is what I have:

 

import arcpy

#Setup environment and variables
arcpy.env.workspace = r'C:\\Users\\xxxx\\OneDrive - xxxxxxx, BUREAU OF TECH. SERVICES\\Documents\\ArcGIS\\Projects\\Migration\\ Migration.gdb'
A = 'Easement_for_Treat'
idfieldA = 'IDFieldA'
B = "Work_Area_Migration_Merge"
idfieldB = 'IDFieldB'

#Build a dictionary with IDs as keys and geometries as value
geometries = {key:value for (key,value) in arcpy.da.SearchCursor(A, [idfieldA, 'SHAPE@'])}

#Empty list to store ids in B not found in A
notfound = []

#Insert B with geometries from A where ID:s match
with arcpy.da.InsertCursor(B, [idfieldB, 'SHAPE@']) as cursor:
    for row in cursor:
        try:
            row[1] = geometries[row[0]]
            insert.updateRow(row)
        except:
            notfound.append(row[0])
            

 

I get thrown this exception:

 

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
In  [2]:
Line 18:    for row in cursor:

TypeError: 'da.InsertCursor' object is not iterable
---------------------------------------------------------------------------

 Any guidance would be greatly appreciated!  

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

As the others have said, you can't iterate an InsertCursor.

You want to update existing features in class B, so you need an UpdateCursor.

Starting from line 17 in your code:

 

with arcpy.da.UpdateCursor(B, [idfieldB, 'SHAPE@']) as cursor:
    for key, shape in cursor:  #iterate through the existing features in B
        try:
            new_shape = geometries[key]
            cursor.updateRow([key, new_shape])
        except KeyError:
            notfound.append(key)

 


Have a great day!
Johannes

View solution in original post

6 Replies
DanPatterson
MVP Esteemed Contributor

an updatecursor would be a better bet if you are altering existing data

UpdateCursor—ArcGIS Pro | Documentation

it supports "with" syntax


... sort of retired...
by Anonymous User
Not applicable

You don't iterate an InsertCursor (for row in cursor) since you're just adding rows. try something like this

iCur = arcpy.da.InsertCursor(table, ["ID", "SHAPE@X", "SHAPE@Y"])
for k, v in geometries.items():
    row = (k, v[0], v[1])
    iCur.insertRow(row)

I've never tried to insert a complete shape object which is why I split it out in the example. that may be unnecessary.

also, if you're trying to update an existing records with geometries, you'd want to use an UpdateCursor.

JohannesLindner
MVP Frequent Contributor

As the others have said, you can't iterate an InsertCursor.

You want to update existing features in class B, so you need an UpdateCursor.

Starting from line 17 in your code:

 

with arcpy.da.UpdateCursor(B, [idfieldB, 'SHAPE@']) as cursor:
    for key, shape in cursor:  #iterate through the existing features in B
        try:
            new_shape = geometries[key]
            cursor.updateRow([key, new_shape])
        except KeyError:
            notfound.append(key)

 


Have a great day!
Johannes
Jefcoat_Nathan
Regular Contributor

Thanks for the help. I've updated the code but I'm getting thrown a NameError on insert.updateRow

 

#Setup environment and variables
arcpy.env.workspace = r'C:\\Users\\xxx\\OneDrive - CITY OF xxxx, BUREAU OF TECH. SERVICES\\Documents\\ArcGIS\\Projects\\xxxxxxxx\\xxxxxxx Migration.gdb'
A = 'Easement_for_Treat'
idfieldA = 'IDFieldA'
B = "Work_Area_Migration_Merge"
idfieldB = 'IDFieldB'

#Build a dictionary with IDs as keys and geometries as value
geometries = {key:value for (key,value) in arcpy.da.SearchCursor(A, [idfieldA, 'SHAPE@Area'])}

#Empty list to store ids in B not found in A
notfound = []

#Insert B with geometries from A where ID:s match
with arcpy.da.UpdateCursor(B, [idfieldB, 'SHAPE@']) as cursor:
    for key, shape in cursor:  #iterate through the existing features in B
        try:
            new_shape = geometries[key]
            insert.updateRow([key, new_shape])
        except KeyError:
            notfound.append(key)

 

--------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
In  [5]:
Line 19:    insert.UpdateRow([key, new_shape])

NameError: name 'insert' is not defined
---------------------------------------------------------------------------

 Do I have the syntax wrong?

0 Kudos
Jefcoat_Nathan
Regular Contributor

I changed it to cursor.updateRow and it worked. Thank You!

0 Kudos
JohannesLindner
MVP Frequent Contributor

Oops, I didn't change all of the variable names... Fixed it in my answer.


Have a great day!
Johannes
0 Kudos