2

This question is similar to Copy a polygon geometry in Arcpy, except within the context of using Arcpy.da instead.

I want to copy the geometry from one feature class to another using ArcPy.da (I'm not concerned about attributes of the feature class right now).

I have a Python script that reads features from one feature class based upon a given WHERE clause using a search cursor and then inserts the features from the search cursor into another feature class:

import arceditor
import arcpy


inputfeatureclass = arcpy.GetParameterAsText(0)
insert_field = arcpy.GetParameterAsText(1)
where_field = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
insertfeatureclass = arcpy.GetParameterAsText(4)


def get_specified_rows(inputfeatureclass, insert_field, where_field, expression):

    searchcursor = arcpy.da.SearchCursor(inputfeatureclass, (insert_field, where_field), where_clause=expression) 

    return searchcursor

def insert_specified_rows(searchcursor, insertfeatureclass):

    insertcursor = arcpy.da.InsertCursor(insertfeatureclass, ("SHAPE@"))
    for row in searchcursor:
        arcpy.AddMessage(row[0])
        insertcursor.insertRow(row[0])

searchcursor = get_specified_rows(inputfeatureclass, insert_field, where_field, expression)
insert_specified_rows(searchcursor, insertfeatureclass)

The polygon Shape field is the value passed for my insert_field parameter.

The program fails on insertcursor.insertRow(row[0]) with the following error: sequence size must match size of the row.

I'm getting the following message on arcpy.AddMessage(row[0]): [3088680.146493972, 10159411.210196389].

This does not look like a polygon array, perhaps I am reading the polygon incorrectly using my search cursor?

1 Answer 1

7

I would restructure to avoid defining functions. I'm not sure they play nicely with arcpy.da cursors. Here's my suggestion:

import arceditor
import arcpy


inputfeatureclass = arcpy.GetParameterAsText(0)
expression = arcpy.GetParameterAsText(1)
insertfeatureclass = arcpy.GetParameterAsText(2)


with arcpy.da.InsertCursor(insertfeatureclass, ("SHAPE@")) as i_cursor:
    with arcpy.da.SearchCursor(inputfeatureclass, ("SHAPE@"), where_clause=expression) as s_cursor:
        for s_row in s_cursor:
            i_cursor.insertRow(s_row)

I removed the search cursor's reference to insert_field and where_field because I can't see their relevance to simply copying the geometry.

2
  • 1
    Works perfect. And you're right - those two parameters are not relevant to simply copying geometry. I had placed them in anticipation for other things outside of the scope of this question and forgot to take them out before I posted. You had one extra parenthesis in your syntax, I've edited to remove it and all extraneous information for a nice clear and concise answer.
    – Conor
    Commented Feb 19, 2014 at 22:32
  • Also I'll add that the error I was getting hints that these references to insert_field and where_field were likely the issue. Thanks!
    – Conor
    Commented Feb 19, 2014 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.