InsertCursor
汇总
InsertCursor 对象可在要素类或表上建立写入游标。 可以使用 InsertCursor 来添加新行。
讨论
对点要素类使用 InsertCursor 时,创建 PointGeometry 对象并将其设置为 SHAPE@ 令牌操作的运算量很大。 此时,使用诸如 SHAPE@XY 、 SHAPE@Z 和 SHAPE@M 等令牌定义的点要素访问反而更为快速有效。
插入游标支持 with 语句以帮助移除锁。 但是,为了防止锁定所有内容,您也可以使用 del 语句来删除游标对象或将游标包含在函数中以使游标对象位于范围之外。
使用不同游标在同一个工作空间上开启同步插入或更新操作时,需要启动 编辑会话 。
以下列出了只能在编辑会话中编辑的一些数据集类型:
参与拓扑的要素类
参与几何网络的要素类
参与网络数据集的要素类
企业级地理数据库中的版本化数据集
一些带有类扩展的对象和要素类
注:
不支持在包含连接表的图层上使用 InsertCursor 。
当字段具有默认值时,如果该字段未指定或设置为 None ,则光标将应用该默认值。
语法
InsertCursor(in_table, field_names, {datum_transformation}, {explicit}, {load_only})
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
in_table |
要素类、图层、表或表视图。 |
String |
|
field_names |
字段名称列表(或组)。 对于单个字段,可以使用一个字符串,而不使用字符串列表。 要访问输入表中的所有字段(BLOB 字段除外),可以使用星号 ( 不支持栅格字段。 以令牌(如
Polygon, polyline, or multipoint features can only be created using the |
String |
|
datum_transformation (可选) |
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.
|
String |
|
explicit (可选) |
如果字段具有默认值并且该字段可为空,则使用 Apply the explicit rule to all fields:
显式规则也可以应用于使用布尔值列表的各个字段。 值列表的长度必须与字段列表的长度相同。 Apply the explicit rule to only the first two fields specified:
(默认值为 False) |
Boolean |
|
load_only (可选) |
Specifies whether the spatial index will be maintained. When set to When set to (默认值为 True) |
Boolean |
属性
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
fields (只读) |
游标使用的一组字段名称。 该组将包括由
如果 |
tuple |
方法
insertRow(row[row,...])
向表中插入一行。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
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 |
返回值
| 数据类型 | 说明 |
|---|---|
|
Integer |
|
代码示例
使用 InsertCursor 在表中插入新行。
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()))
使用 InsertCursor 和 SHAPE@XY 令牌将点要素添加到点要素类中。
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)
使用 InsertCursor 和 SHAPE@ 令牌添加一个使用几何对象的新要素。
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])