0

I have the following problem. I am doing an analysis which creates a shapefile "A" with one or more point features. In the next step I need to insert the individual point features of the shapefile "A" into another point shapefile "B" which stores the results from multiple analyses. How do I insert (append) features from "A" into "B"? Both shapefiles have the same attributes (shapefile "B" has one additional attribute) and I need to transfer them as well.

Here is the code I think should work but it does not:

analysis_res = fcn1(inp1, inp2) # analysis results - shapefile "A"
cursorA = arcpy.da.SearchCursor(analysis_res)
cursorB = arcpy.da.InsertCursor(outFile, ['SHAPE@'])
for row in cursorA:
     cursorB.inserRow(row) # here I would like to insert the poit feature of "A" into "B"
del cursorA, cursorB
4
  • 2
    The shapefile schemas as not "the same" if one has more fields than the other.
    – Vince
    Commented Oct 21, 2016 at 10:53
  • They are the same except of the second one having an additional field. I wanted to emphasize that the rest of the fields are the same.
    – user21816
    Commented Oct 21, 2016 at 10:57
  • So they're the same except where they're different, but the difference is the part that creates the problem. uour Python doesn't work for many reasons, but not explicitly listing which columns is one of them.
    – Vince
    Commented Oct 21, 2016 at 11:01
  • @Vince is right. You need to specify which fields are part of your SearchCursor. cursorA = arcpy.da.SearchCursor(analysis_res, ['SHAPE@'])
    – Evan
    Commented Oct 21, 2016 at 14:09

2 Answers 2

2

Use the append tool with schema test disabled.

0

One of the ways how to do it is this one:

#result of the analysis
shpA = fcn1(inp1, inp2)

# get field names
fieldObj = arcpy.ListFields(shpA)
fieldList = []
for fld in fieldObj:
    fieldList.append(fld.name)
fields = tuple(list(fieldList[2:]) + list(["SHAPE@XY"]))

# create cursor objects
cursorA = arcpy.da.SearchCursor(shpA, fields)
cursorB = arcpy.da.InsertCursor(shpB, fields)

# insert rows
for row in cursorA:
    cursorB.inserRow(row)

# delete cursors
del cursorA, cursorB
1
  • 1
    It's better practice to use the with construct when possible (on the search cursor)
    – Vince
    Commented Oct 23, 2016 at 4:12

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.