InsertCursor

サマリー

InsertCursor establishes a write cursor on a feature class or table. InsertCursor can be used to add new rows.

Learn more about data access using cursors

説明

When using InsertCursor on a point feature class, creating a PointGeometry and setting it to the SHAPE@ token is a comparatively expensive operation. Instead, define the point feature using tokens such as SHAPE@XY, SHAPE@Z, and SHAPE@M for faster, more efficient access.

異なるカーソルを使用して、同じワークスペースで同時に挿入または更新操作を開くには、編集セッションの開始が必要です。

編集セッション内でのみ編集できるデータセット タイプの一部を次に示します。

  • トポロジに属しているフィーチャクラス
  • ジオメトリック ネットワークに属しているフィーチャクラス
  • ネットワーク データセットに属しているフィーチャクラス
  • エンタープライズ ジオデータベース内のバージョン対応データセット
  • クラス エクステンションを持つ一部のオブジェクトとフィーチャクラス
注意:

Using an 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.

構文

InsertCursor (in_table, field_names, {datum_transformation}, {explicit})
パラメーター説明データ タイプ
in_table

The feature class, layer, table, or table view.

String
field_names
[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 (*) instead of a list of fields to access all fields from the input table (BLOB fields are excluded). However, for faster performance and reliable field order, it is recommended that the list of fields be narrowed to only those that are actually needed.

Raster fields are not supported.

Additional information can be accessed using tokens (such as OID@) in place of field names:

  • SHAPE@XYフィーチャの重心を表す X 座標と Y 座標の組み合わせ
  • SHAPE@XYZフィーチャの重心を表す XYZ 座標の組み合わせ
  • SHAPE@TRUECENTROIDフィーチャの重心を表す X 座標と Y 座標の組み合わせ SHAPE@XY と同じ値が返されます。
  • SHAPE@Xフィーチャの X 座標 (Double)
  • SHAPE@Yフィーチャの Y 座標 (Double)
  • SHAPE@Zフィーチャの Z 座標 (Double)
  • SHAPE@Mフィーチャの M 値 (Double)
  • SHAPE@JSONジオメトリを表す Esri JSON 文字列
  • SHAPE@WKBOGC ジオメトリの WKB (Well-Known Binary) 表現。 ジオメトリ値の汎用的な表現が、連続的なバイト ストリームとして提供されます。bytearray または bytes オブジェクトとして値を追加できます。
  • SHAPE@WKTOGC ジオメトリの WKT (Well-Known Text) 表現。 ジオメトリ値の汎用的な表現が、テキスト文字列として提供されます。
  • SHAPE@フィーチャのジオメトリ オブジェクト
  • SUBTYPE@サブタイプ コードの整数。

Polygon, polyline, or multipoint features can only be created using the SHAPE@ token.

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, an appropriate transformation should be specified.

The ListTransformations function can be used to provide a list of valid datum transformations between two spatial references.

Learn more about datum transformations

String
explicit
[explicit,...]

If a field has a default value and the field is nullable, using a value of True will explicitly override the default value and insert null values into the record. When using a value of False, the default value will be inserted instead of null.

Apply the explicit rule to all fields:

with arcpy.da.InsertCursor(table, [field1, field2, field3], explicit=True) as cursor:
    ...

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:

with arcpy.da.InsertCursor(table, [field1, field2, field3], explicit=[True, True, False]) as cursor:
    # ...

(デフォルト値は次のとおりです False)

Boolean

プロパティ

プロパティ説明データ タイプ
fields
(読み取り専用)

A tuple of field names used by the cursor.

The tuple will include all fields and tokens specified by the field_names argument.

The order of the field names on the fields property will be the same as passed in with the field_names argument.

If the field_names argument is set to *, the fields property will include all fields used by the cursor. A value of * will return geometry in a tuple of x,y coordinates (equivalent to the SHAPE@XY token).

tuple

方法の概要

方法説明
insertRow (row)

Inserts a row into a table.

方法

insertRow (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 1.0 to a string field will be added as "1.0", and a value of "25" added to a float field will be added as 25.0.

tuple
戻り値
データ タイプ説明
Integer

insertRow returns the objectid of the new row.

コードのサンプル

InsertCursor example 1

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()))
InsertCursor example 2

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)
InsertCursor example 3

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)])
polyline = arcpy.Polyline(array)

# Open an InsertCursor using a context manager and insert the new geometry
with arcpy.da.InsertCursor('C:/data/texas.gdb/counties', ['SHAPE@']) as cursor:
    cursor.insertRow([polyline])

関連トピック