InsertCursor
Summary
The InsertCursor object establishes a write cursor on a feature class or table. InsertCursor can be used to add new rows.
Discussion
When using InsertCursor on a point feature class, creating a PointGeometry object and setting it to the SHAPE@ token is a computationally intensive operation. Instead, define the point feature using tokens such as SHAPE@XY, SHAPE@Z, and SHAPE@M for faster, more efficient access.
Insert cursors support with statements to aid in the removal of locks.
Opening simultaneous insert or update operations on the same workspace using different cursors requires the start of an edit session.
The following includes some dataset types that can only be edited within an edit session:
Feature classes participating in a topology
Feature classes participating in a geometric network
Feature classes participating in a network dataset
Versioned datasets in enterprise geodatabases
Some object and feature classes with class extensions
Note:
Using InsertCursor on a layer with a joined table is not supported.
When a field has a default value, a cursor applies the default value when the field is not specified or is set to None.
Syntax
InsertCursor(in_table, field_names, {datum_transformation}, {explicit}, {load_only})
| Name | Explanation | Data type |
|---|---|---|
|
in_table |
The feature class, layer, table, or table view. |
String |
|
field_names |
A list (or tuple) of field names. For a single field, you can use a string instead of a list of strings. Use an asterisk ( Raster fields are not supported. Additional information can be accessed using tokens (such as
Polygon, polyline, or multipoint features can only be created using the |
String |
|
datum_transformation (Optional) |
When features to be inserted have a different spatial reference than the target feature class, a projection will be performed automatically. If the two spatial references have a different datum, specify an appropriate transformation. The |
String |
|
explicit (Optional) |
If a field has a default value and the field is nullable, using a value of Apply the explicit rule to all fields:
The explicit rule can also be applied to individual fields using a list of Boolean values. The list of values must be the same length as the list of fields. Apply the explicit rule to only the first two fields specified:
The default value is False. |
Boolean |
|
load_only (Optional) |
Specifies whether the spatial index will be maintained. When set to When set to The default value is True. |
Boolean |
Properties
| Name | Explanation | Data type |
|---|---|---|
|
fields (Read only) |
A tuple of field names used by the cursor. The tuple will include all fields and tokens specified by the The order of the field names on the If the |
tuple |
Methods
insertRow(row[row,...])
Inserts a row into a table.
| Name | Explanation | Data type |
|---|---|---|
|
row[row,...] |
A list or tuple of values. The order of values must be in the same order as specified when creating the cursor. When updating fields, if the incoming values match the type of field, the values will be cast as necessary. For example, a value of |
tuple |
Return value
| Data type | Explanation |
|---|---|
|
Integer |
|
Code sample
Use InsertCursor to insert new rows into a table.
import arcpy
import datetime
# Create an insert cursor for a table specifying the fields that will
# have values provided
fields = ['rowid', 'distance', 'CFCC', 'DateInsp']
with arcpy.da.InsertCursor('D:/data/base.gdb/roads_maint', fields) as cursor:
# Create 25 new rows. Set default values on distance and CFCC code
for x in range(0, 25):
cursor.insertRow((x, 100, 'A10', datetime.datetime.now()))
Use InsertCursor with the SHAPE@XY token to add point features to a point feature class.
import arcpy
# A list of values that will be used to construct new rows
row_values = [('Anderson', (1409934.4442000017, 1076766.8192000017)),
('Andrews', (752000.2489000037, 1128929.8114))]
# Open an InsertCursor using a context manager
with arcpy.da.InsertCursor('C:/data/texas.gdb/counties', ['NAME', 'SHAPE@XY']) as cursor:
# Insert new rows that include the county name and a x,y coordinate
# pair that represents the county center
for row in row_values:
cursor.insertRow(row)
Use InsertCursor with the SHAPE@ token to add a new feature using a geometry object.
import arcpy
# Create a polyline geometry
array = arcpy.Array([arcpy.Point(459111.6681, 5010433.1285),
arcpy.Point(472516.3818, 5001431.0808),
arcpy.Point(477710.8185, 4986587.1063)])
spatial_ref = arcpy.SpatialReference(26911) # NAD 1983 UTM Zone 11N
polyline = arcpy.Polyline(array, spatial_ref)
# Open an InsertCursor using a context manager and insert the new geometry
with arcpy.da.InsertCursor('C:/data/gdb.gdb/lines', ['SHAPE@']) as cursor:
cursor.insertRow([polyline])