InsertCursor

概要

InsertCursor は、フィーチャクラスまたはテーブルに書き込みカーソルを設定します。 InsertCursor は、新しい行の追加に使用できます。

ディスカッション

ポイント フィーチャクラスに InsertCursor を使用し、PointGeometry を作成して SHAPE@ に設定する場合、トークンは相対的にコストがかかる操作になります。 代わりに、SHAPE@XYSHAPE@ZSHAPE@M などのトークンを使用してポイント フィーチャを定義すると、より高速で効率的なアクセスが実現します。

メモ:

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

構文

InsertCursor (in_table, field_names, datum_transformation)
パラメーター説明データ タイプ
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 (raster and 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@XYA tuple of the feature's centroid x,y coordinates.
  • SHAPE@XYZA tuple of the feature's centroid x,y,z coordinates.
  • SHAPE@TRUECENTROIDA tuple of the feature's centroid x,y coordinates. This returns the same value as SHAPE@XY.
  • SHAPE@XA double of the feature's x-coordinate.
  • SHAPE@YA double of the feature's y-coordinate.
  • SHAPE@ZA double of the feature's z-coordinate.
  • SHAPE@MA double of the feature's m-value.
  • SHAPE@JSON The Esri JSON string representing the geometry.
  • SHAPE@WKBThe well-known binary (WKB) representation for OGC geometry. It provides a portable representation of a geometry value as a contiguous stream of bytes.
  • SHAPE@WKTThe well-known text (WKT) representation for OGC geometry. It provides a portable representation of a geometry value as a text string.
  • SHAPE@A geometry object for the feature.

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

プロパティ

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

A tuple of field names used by the cursor.

The tuple will include all fields (and tokens) specified by the field_names argument. If the field_names argument is set to *, the fields property will include all fields used by the cursor. When using *, geometry values will be returned in a tuple of the x,y-coordinates (equivalent to the SHAPE@XY token).

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

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 を使用して、テーブルに新しい行を挿入します。

import arcpy
import datetime

# Create an insert cursor for a table specifying the fields that will
# have values provided
fields = ['rowid', 'distance', 'CFCC', 'DateInsp']
cursor = arcpy.da.InsertCursor('D:/data/base.gdb/roads_maint', fields)

# 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()))

# Delete cursor object
del cursor

InsertCursorSHAPE@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
cursor = arcpy.da.InsertCursor('C:/data/texas.gdb/counties',
                               ['NAME', 'SHAPE@XY'])

# 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)

# Delete cursor object
del cursor

InsertCursorSHAPE@ トークンを使用して、ジオメトリ オブジェクトを使用した新しいフィーチャを追加します。

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 and insert the new geometry
cursor = arcpy.da.InsertCursor('C:/data/texas.gdb/counties', ['SHAPE@'])
cursor.insertRow([polyline])

# Delete cursor object
del cursor

関連トピック