HOW TO

Add features to a feature class using ArcPy in ArcMap

Last Published: December 8, 2023

Summary

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.

Procedure

The following procedure demonstrates how to add a feature to a feature class using Python:

  1. Open the map with the features in ArcMap.
  2. Click Geoprocessing and select Python to open the Python console.
  3. In the Python console, insert the following script:
    1. Import the ArcPy module.
import arcpy
  1. Define a new array parameter to include the desired values in the new row.
row_values = [('Anderson', (1409934.4442000017, 1076766.8192000017)), ('Andrews', (752000.2489000037, 1128929.8114))]
  1. Create an object item to utilize the InsertCursor() function. The function requires inputs for the desired feature location and the row field names.
cursor = arcpy.da.InsertCursor("C://sample.gdb/test",("[row 1]", "[row 2]"))
  1. Start a loop to create and insert new rows to be filled in the attribute table with the predefined array values in Step 2.
for row in row_values:
    cursor.insertRow(row)
  1. Delete the cursor object to end the function.
del cursor

The following codeblock demonstrates the 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 add data from another feature class, the SearchCursor() function must be used with the InsertCursor() function. The following codeblock demonstrates the use of the SearchCursor() and the InsertCursor() functions.

import arcpy

sourceCursor = arcpy.da.SearchCursor(r"<feature_path>", ("<Field_Name>", "SHAPE@XYZ"))
inputCursor = arcpy.da.InsertCursor(r"<new_feature_path>", ("<Field_Name>", "SHAPE@XYZ"))

#example
#sourceCursor = arcpy.da.SearchCursor(r"C:\Users\User\Documents\ArcGIS\Projects\test\test.gdb\Point", ("URL", "SHAPE@XYZ"))
#inputCursor = arcpy.da.InsertCursor(r"C:\Users\User\Documents\ArcGIS\Projects\test\test.gdb\NewPoint", ("URL", "SHAPE@XYZ"))

for row in sourceCursor:
    inputCursor.insertRow(row)

del inputCursor
del sourceCursor

Article ID: 000018755

Software:
  • ArcMap

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options