How to Insert data into an Existing feature class?

2332
4
07-27-2012 08:21 AM
MatthewGerbrandt
New Contributor II
Does anyone here know how to insert rows of data into an existing feature class from either a geodatabase table or a .lyr file?

All of the code I can find wants to copy data into a new feature class instead of populating an existing one.

All help is tremendously appreciated. Thanks in advance!
Tags (2)
0 Kudos
4 Replies
ChristopherThompson
Occasional Contributor III
if you have two datasets with the same data structure (i.e. field names, sizes, types) then what you are looking for is the Append function (arpy.Append_management) I think.  You can use Append when the two feature classes have the same data but differently named fields too but then you have to deal with field mappings which is a bit of a PITA done outside a GUI.
0 Kudos
MatthewGerbrandt
New Contributor II
Thank you, Christopher. Unfortunatley, I'm not working with the same data types. I need to go into a FeatureClass from either a geodatabase table or a layer (.lyr) file.
0 Kudos
SolomonPulapkura
Occasional Contributor III
Roughly, you have to do something like this where you are reading from one table and writing to the other using search and insert cursors. You must have same number of records in both tables. If it is a feature class OID's should match. Never tried table to table though. This is just an example. You might have to tweak it to make it work for your situation -

# Open a search cursor on a feature class / table to read from
    scur = arcpy.SearchCursor(read_fc)
    # Open an insert cursor on the fc / table to write to
    icur = arcpy.InsertCursor(write_fc)

    for srow in scur:
            irow = icur.newRow()
            irow.shape = srow.shape
            irow.setValue(to_field1, srow.getValue(from_field1))
            irow.setValue(to_field2, srow.getValue(from_field2))
            icur.insertRow(irow)
    del irow, icur, srow, scur
0 Kudos
ChristopherThompson
Occasional Contributor III
Thank you, Christopher. Unfortunatley, I'm not working with the same data types. I need to go into a FeatureClass from either a geodatabase table or a layer (.lyr) file.


You need to clarify this a little bit - what is your 'source' data that you are using and what is the 'target' that you want to put those features into?  The term 'featureclass' is sort of a generic reference to a data type that hold spatial data - this can be either a shapefile or a featureclass within a geodatabase.  Also, a .lyr file is simply an on disk representation of a gdb featureclass or a shapefile, it doesn't actually contain data, rather it holds all the properties needed for arcmap to render the data it refrences cartographically.  I still think 'append' is the tool you need.
0 Kudos