arcpy.da.InsertCursor geometry

4722
5
Jump to solution
10-26-2020 06:03 PM
johnbrosowsky
Occasional Contributor II

I am attempting to insert a feature into feature class based on feature in another feature class (both have identical structure) like this:

input = arcpy.da.SearchCursor(fc, "*", where_clause=(expression))
output = arcpy.da.InsertCursor(tc,['risk','SHAPE@'])

for row in cursor:
   risk_factor = row[5]
   if risk_factor == "423123fasq":
     updateRows.insertRow([["high risk",row[1]])

... but I think I am not handling geometry correctly in insertRow as I get error "TypeError: cannot alter multipart geometry type".  They are polyline features.  Any ideas how to handle geometry correctly in this case?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

I noticed you are using a wildcard for your fields.  I would avoid this as the fields may be in a different order from what you are expecting.  Also, when you say the features both have the identical structure, you mean both field names/types match along with the geometry type and spatial reference?

If you are only interested in the geometry and risk field, you might try something like:

fc = 'inputFeature'  # feature for SearchCursor
tc = 'outputFeature' # feature for InsertCursor

expression = 'some expression'

# you should specify the fields for your search cursor (don't use wildcard)
# this way, you can be sure what they are when you reference them
# also, assuming that fc and tc use the same field 'risk'

fields = ['SHAPE@', 'risk'] 

# start an insert cursor
insertCursor = arcpy.da.InsertCursor(tc, fields)

with arcpy.da.SearchCursor(fc, fields, where_clause=expression) as cursor:
    for row in cursor:
        risk_factor = row[1]
        if risk_factor == "423123fasq":
            # insert feature only if risk_factor is "423123fasq"
            insertCursor.insertRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

cursor isn't defined. (row 3).  There are code examples here

InsertCursor—ArcGIS Pro | Documentation 


... sort of retired...
johnbrosowsky
Occasional Contributor II

Hi Dan -

Sorry that was just a typo (had cleaned up my code sample for the post, but missed that).  I am aware of the examples.  My code sample works if I remove SHAPE@ and row[1], i.e.:

input = arcpy.da.SearchCursor(fc, "*", where_clause=(expression))

output = arcpy.da.InsertCursor(tc,['risk'])

for row in input:
   risk_factor = row[5]
   if risk_factor == "423123fasq":
     output.insertRow(["high risk"])

.

... the above works, but produces output with no geometries of course.  When I try to add the geometries I get the crash.  Therefore I believe I am handling geometries incorrectly in some way.

.

0 Kudos
RandyBurton
MVP Alum

I noticed you are using a wildcard for your fields.  I would avoid this as the fields may be in a different order from what you are expecting.  Also, when you say the features both have the identical structure, you mean both field names/types match along with the geometry type and spatial reference?

If you are only interested in the geometry and risk field, you might try something like:

fc = 'inputFeature'  # feature for SearchCursor
tc = 'outputFeature' # feature for InsertCursor

expression = 'some expression'

# you should specify the fields for your search cursor (don't use wildcard)
# this way, you can be sure what they are when you reference them
# also, assuming that fc and tc use the same field 'risk'

fields = ['SHAPE@', 'risk'] 

# start an insert cursor
insertCursor = arcpy.da.InsertCursor(tc, fields)

with arcpy.da.SearchCursor(fc, fields, where_clause=expression) as cursor:
    for row in cursor:
        risk_factor = row[1]
        if risk_factor == "423123fasq":
            # insert feature only if risk_factor is "423123fasq"
            insertCursor.insertRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
johnbrosowsky
Occasional Contributor II

Hi Randy - 

Explicitly settings the "fields" property for both cursors did the trick.

Thanks!

JB

0 Kudos
johnbrosowsky
Occasional Contributor II

Hi Randy,

Yes input fc and output tc field names/types match along with the geometry type and spatial reference.  My tc geodatabase is a file copy of my fc geodatabse so they are identical. 

The example you provided only copies the field contents, whereas I need to alter field contents in the target based on some algorithm.

Thanks!

JB

  

0 Kudos