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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 25, 2016 at 10:20

4 Answers 4

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.

answered Jun 25, 2016 at 11:03
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

answered Jun 26, 2016 at 14:23
1
  • 1
    Yep. If you're doing it right, a little dialog should pop up on paste asking you which layer to target. Commented Jun 27, 2016 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.

answered Jun 26, 2016 at 20:49
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. Commented Jun 27, 2016 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 Commented Jun 27, 2016 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. Commented Jun 27, 2016 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 Commented Jun 27, 2016 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
answered Jun 25, 2016 at 10:52
6
  • Your fields variable isn't defined. Commented Jun 25, 2016 at 11:18
  • @Vince searchField = 'BLAH'? fields = searchField Commented Jun 25, 2016 at 11:24
  • What is happening with Shape field? -1 btw, it is not just interested, it is wrong Commented Jun 26, 2016 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. Commented Jun 26, 2016 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. Commented Jun 26, 2016 at 13:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.