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 Answers 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.
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
-
1Yep. If you're doing it right, a little dialog should pop up on paste asking you which layer to target.obrl_soil– obrl_soil2016年06月27日 07:58:09 +00:00Commented Jun 27, 2016 at 7:58
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:
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:
As I already mentioned in my comments solution by @JamesLeversha is wrong.
-
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– JamesLeversha2016年06月27日 03:26:02 +00:00Commented 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 @PolyGeoFelixIP– FelixIP2016年06月27日 03:30:24 +00:00Commented 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.JamesLeversha– JamesLeversha2016年06月27日 03:40:10 +00:00Commented 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 beFelixIP– FelixIP2016年06月27日 04:54:04 +00:00Commented Jun 27, 2016 at 4:54
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
-
Your
fields
variable isn't defined.Vince– Vince2016年06月25日 11:18:20 +00:00Commented Jun 25, 2016 at 11:18 -
@Vince searchField = 'BLAH'? fields = searchFieldJamesLeversha– JamesLeversha2016年06月25日 11:24:47 +00:00Commented Jun 25, 2016 at 11:24
-
What is happening with Shape field? -1 btw, it is not just interested, it is wrongFelixIP– FelixIP2016年06月26日 09:32:21 +00:00Commented 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.JamesLeversha– JamesLeversha2016年06月26日 13:03:22 +00:00Commented 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.JamesLeversha– JamesLeversha2016年06月26日 13:48:57 +00:00Commented Jun 26, 2016 at 13:48