3

I am using ArcGIS Desktop.

How do I copy a line feature from a line feature class to another existing feature class without creating a new feature class?

The Copy Features tool creates the copied feature in a new feature class but what I want is to add the feature that will be copied to an existing feature class.

4

The Append tool:

Appends multiple input datasets into an existing target dataset. Input datasets can be point, line, or polygon feature classes, tables, rasters, raster catalogs, annotation feature classes, or dimensions feature classes.

For one ArcPy example its Help has:

import arcpy
arcpy.env.workspace = "C:/data/"
arcpy.Append_management(["north.shp", "south.shp", "east.shp", "west.shp"], "wholecity.shp", "TEST","","")

If you need only a subset of features from a feature class then precede Append with the Select tool.

0
3

Apart from the already cited "insertCursor" and "Append" tools, a simple way to copy one or more features into an existing feature class is to make a copy-paste while in edit mode.

Start an edit session for your target feature class

Select the features that you want to copy

CTRL+C

CTRL+V

save edit

1
  • 1
    Yep. If you're doing it right, a little dialog should pop up on paste asking you which layer to target. – obrl_soil Jun 27 '16 at 7:58
2

To help qualify the @JamesLevrsha solution I tested his script:

inFC = r'd:\scratch\centres.shp'
outFC = r'd:\scratch\centres_Copy.shp'

searchField = 'CODE'
desiredVal = 'PPA'

searchCur = arcpy.SearchCursor(inFC, fields = searchField)
inCur = arcpy.InsertCursor(outFC)

for sRow in searchCur:
    rowVal = sRow.getValue(searchField)
    print rowVal

    if rowVal == desiredVal:
        row = inCur.newRow()
        row.setValue(searchField, rowVal)
        inCur.insertRow(row)

del sRow,row,inCur,searchCur

on 2 shapefiles, with single field "CODE" populated by single value "PPA". Output feature class (outFC) shown as red points, input FC (inFC) shown as green points: enter image description here

I run the script and reduced the size of the symbol of green points, so that I can see appended points.

Result:

enter image description here

Why is this happening?

Because as expected the script creates records with NULL geometry. This is how red points table looks like after exporting to file geodatabase feature class and computing coordinates of the points:

enter image description here

As I already mentioned in my comments solution by @JamesLeversha is wrong.

4
  • But all the user needs to do is add the required fields? Ill add and update that includes the geometry field as well, in fact i just added all matching fields. – JamesLeversha Jun 27 '16 at 3:26
  • What he wants is append existing feature class by features from another. Matching etc not even a concern. See correct answer by @PolyGeo – FelixIP Jun 27 '16 at 3:30
  • Thanks, I understand that the append tool is the best option, however it doesnt make my solution wrong. I didnt explain that the original 'BLAH' field needed to be replaced by the user with the desired fields. My solution selects desired lines and then appends them to an existing feature class, which is what the OP wants to do. – JamesLeversha Jun 27 '16 at 3:40
  • First 2 oR even more solutions were wrong, as shown in my test. The latest might work, which is hard to tell because you keep changing original instead of updates. That is how it should be – FelixIP Jun 27 '16 at 4:54
-1

Using an insert cursor would do the trick. If you are using greater than 10.0 then use the da cursors but this code is for 10.0. Code isn't tested.

inFC = 'test.shp'
outFC = 'existing.shp'

inCur = arcpy.InsertCursor(outFC)
searchField = 'BLAH'

searchCur = arcpy.SearchCursor(inFC,fields = searchField)

for sRow in searchCur:
    rowVal = sRow.getValue('BLAH')
    if rowVal == 'DESIRED VALUE':
        row = inCur.newRow()
        row.setValue('BLAH', rowVal)
        inCur.insertRow(row)

del sRow
del row
del inCur
del searchCur

Update to include geometry, and all other fields that are matching in the output FC

import arcpy

inFC = r'test.shp'
outFC = r'testCopy.shp'


searchField = ''
desiredField = 'CODE'
desiredVal = 'PPA'


inOID = arcpy.Describe(inFC).OIDFieldName
outFields = [item.name for item in arcpy.ListFields(outFC)]
inFields = [item.name for item in arcpy.ListFields(inFC) if item.name != inOID]

finalFields = [item for item in inFields if item in outFields]

for item in finalFields:
    searchField += item + ';'
searchField = searchField[:-1]

searchCur = arcpy.SearchCursor(inFC, fields = searchField)
inCur = arcpy.InsertCursor(outFC)

row = None
cursorDict = {}
for sRow in searchCur:
    for item in finalFields:
        cursorDict[item] = sRow.getValue(item)
    if cursorDict[desiredField] == desiredVal:
        row = inCur.newRow()
        for item in finalFields:
            row.setValue(item, cursorDict[item])
        inCur.insertRow(row)

del sRow, row, inCur, searchCur
6
  • Your fields variable isn't defined. – Vince Jun 25 '16 at 11:18
  • @Vince searchField = 'BLAH'? fields = searchField – JamesLeversha Jun 25 '16 at 11:24
  • What is happening with Shape field? -1 btw, it is not just interested, it is wrong – FelixIP Jun 26 '16 at 9:32
  • @FelixIP I am unsure what you mean. I can see there is an extra r in the search cursor (sRrow) which I have fixed, but unsure what you mean by what is happening with the shape field. – JamesLeversha Jun 26 '16 at 13:03
  • @FelixIP I have just tested this on 10.3 and it worked so I am unsure what you mean. Unless 10.0 can't take the "fields = value" statement it isn't wrong. I should have however replaced all instances of 'BLAH' with searchField after I defined it. – JamesLeversha Jun 26 '16 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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