HOW TO
Adding features to a feature class can be automated using a Python script. The ArcPy function, InsertCursor() is used to insert a new row in an attribute table. The function can be used as an object to insert the values for the newly created rows.
Note: The InsertCursor() function can also be used in conjunction with the SearchCursor() function to replicate certain fields.
The following procedure demonstrates how to add a feature to a feature class using Python:
import arcpy
row_values = [('Anderson', (1409934.4442000017, 1076766.8192000017)), ('Andrews', (752000.2489000037, 1128929.8114))]
cursor = arcpy.da.InsertCursor("C://sample.gdb/test",("[row 1]", "[row 2]"))
for row in row_values: cursor.insertRow(row)
del cursor
The following code sample demonstrates a full working code:
import arcpy # A list of values that will be used to construct new rows row_values = [('Atascosa', (1409934.4442000017, 1076766.8192000017)), ('Bosque', (752000.2489000037, 1128929.8114))] # Open an InsertCursor. # Specify the location for the desired feature. The sample inserts the values into the field, NAME and SHAPE. cursor = arcpy.da.InsertCursor("C:/data/texas.gdb/counties", ("NAME", "SHAPE@XY")) # Insert new rows that include the county name and the x and y coordinate # pair that represents the county center for row in row_values: cursor.insertRow(row) # Delete cursor object del cursor
To replicate data from another feature class, the SearchCursor() function must be used in conjunction with the InsertCursor() function. The following code sample demonstrates how to use the SearchCursor() and the InsertCursor() functions.
sourceCursor = arcpy.da.SearchCursor([Feature_Name], [Field_Name]) inputCursor = arcpy.da.InsertCursor([New_Feature_Name], [Field_Name]) for row in sourceCursor: inputCursor.insertRow(row)
Unterstützung durch ArcGIS-Experten anfordern
Esri Support App herunterladen