Select to view content in your preferred language

Insert Cursor to insert entire layer rows

501
1
11-18-2022 09:55 AM
2Quiker
Frequent Contributor

I need to transfer an entire layers features to another layer. I am trying to use the SearchCursor and InsertCursor to do this( I know I can use append, but I am trying to this way). Both Features have the same fields and same type of geometry, both are polygons.

in_features = "FeatureClass"
out_path = = "OtherFeatureClass"

dsc = arcpy.Describe(in_features)
fields = dsc.fields

# List all field names except the OID field
out_fields = [dsc.OIDFieldName, dsc.lengthFieldName, dsc.areaFieldName]
fieldnames = [field.name if field.name != 'Shape' else 'SHAPE@' for field in fields if field.name not in out_fields]
#out_fields = [dsc.OIDFieldName]
#fieldnames = [fields.name for fields in fields if fields.name not in out_fields]
# Create cursors and insert new rows
#
with arcpy.da.SearchCursor(in_features,fieldnames) as sCur:
    with arcpy.da.InsertCursor(out_path,fieldnames) as iCur:
        for row in sCur:
            iCur.insertRow(row)
del sCur

 

I get the following error.

iCur.insertRow(row)
RuntimeError: Number of parts in shape is incorrect for its geometry type

0 Kudos
1 Reply
Brian_Wilson
Honored Contributor

Is the source layer multipart polygons and the destination is not? If so you'd have to explode first.

Well you'd have to use "explode" not you personally. Don't tear out your hair either way.

https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/multipart-to-singlepart.htm

arcpy.management.MultipartToSinglepart(in_features, out_feature_class)

 Or make sure the destination is also multipart.