UpdateCursor

概要

UpdateCursor フィーチャクラスまたはテーブルから返されるレコードに対して読み取り/書き込みアクセスを設定します。

リストの反復子を返します。 リストの値の順番は、field_names 引数で指定されたフィールドの順番と一致します。

ディスカッション

更新カーソルは、for ループを使用して反復処理できます。 更新カーソルは、反復処理をリセットしてロックの削除に役立つ with ステートメントもサポートしています。 ただし、ロックが起こるあらゆる事態を防ぐために、del ステートメントを使用してオブジェクトを削除するか、関数内でカーソルを折り返してカーソル オブジェクトを範囲外に移動することを検討してください。

Python 2 では、UpdateCursor はループ外の次の行を取得する反復子の next メソッドをサポートしています。 Python 3 では、Python の組み込み next 関数を使用して同等の処理を実行できます。

メモ:

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

メモ:

バージョン対応データに UpdateCursor を使用するには、編集セッションの開始が必要です。

メモ:

[フィールド演算 (Calculate Field)] および [フィールド演算 (複数) (Calculate Fields)] ツールを使用してフィールド値を更新することもできます。

構文

UpdateCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})
パラメーター説明データ タイプ
in_table

フィーチャクラス、レイヤー、テーブル、またはテーブル ビュー。

String
field_names
[field_names,...]

フィールド名のリスト (またはタプル)。 単一フィールドの場合、文字列のリストの代わりに文字列を使用できます。

入力テーブルのすべてのフィールド (ラスターおよび BLOB フィールドは除きます) にアクセスする場合、フィールドのリストの代わりにアスタリスク (*) を使用します。 ただし、パフォーマンスとフィールドの順序の信頼性を高めるために、フィールドのリストは実際に必要なフィールドのみに絞り込むことをお勧めします。

ラスター フィールドはサポートされていません。

その他の情報には、フィールド名の代わりにトークン (OID@ など) を使用してアクセスできます。

  • SHAPE@XYフィーチャの重心を表す X 座標と Y 座標の組み合わせ
  • SHAPE@XYZフィーチャの重心を表す XYZ 座標の組み合わせ このトークンは、ジオメトリが Z 対応である場合にのみサポートされます。
  • 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) 表現。ジオメトリ値の汎用的な表現が、連続的なバイト ストリームとして提供されます。
  • SHAPE@WKTOGC ジオメトリの WKT (Well-Known Text) 表現。ジオメトリ値の汎用的な表現が、テキスト文字列として提供されます。
  • SHAPE@フィーチャのジオメトリ オブジェクト
  • SHAPE@AREAフィーチャの面積 (Double)
  • SHAPE@LENGTHフィーチャの長さ (Double)
  • OID@ObjectID フィールドの値
String
where_clause

返されるレコードを制限するオプションの条件式。 WHERE 句と SQL ステートメントの詳細については、クエリ式で使用されるエレメントの SQL リファレンスをご参照ください。

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

String
spatial_reference

The spatial reference of the feature class can be specified with either a SpatialReference object or string equivalent.

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

SpatialReference
explode_to_points

フィーチャを個々のポイントまたは頂点に分解します。 explode_to_pointsTrue に設定されている場合、たとえば 5 つのポイントがあるマルチポイント フィーチャは 5 つの行で表現されます。

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

Boolean
sql_clause

リストまたはタプルに編成された SQL の接頭辞句と接尾辞句のオプションのペア。

An SQL prefix clause supports None, DISTINCT, and TOP. An SQL postfix clause supports None, ORDER BY, and GROUP BY.

SQL の接頭辞句は、最初の位置に配置され、SELECT キーワードと SELECT COLUMN LIST の間に挿入されます。 SQL の接頭辞句は、DISTINCT または ALL などの句に最もよく使用されます。

SQL の接尾辞句は、2 番目の位置に配置され、WHERE 句に続く SELECT ステートメントに追加されます。 SQL の接尾辞句は、ORDER BY などの句に最もよく使用されます。

メモ:

DISTINCT、ORDER BY、および ALL は、データベースを操作する場合のみサポートされています。 他のデータ ソース (dBASE や INFO テーブルなど) ではサポートされていません。

TOP は、SQL Server データベースでのみサポートされています。

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

tuple

プロパティ

プロパティ説明データ タイプ
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

手法の概要

手法説明
deleteRow ()

Deletes the current row.

reset ()

Resets the cursor back to the first row.

updateRow (row)

Updates the current row in the table.

手法

deleteRow ()
reset ()
updateRow (row)
パラメーター説明データ タイプ
row

A list or tuple of values. The order of values should be in the same order as the fields.

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

コードのサンプル

UpdateCursor の例 1

UpdateCursor を使用して、他のフィールドの値を評価することでフィールド値を更新します。

import arcpy

fc = 'c:/data/base.gdb/well'
fields = ['WELL_YIELD', 'WELL_CLASS']

# Create update cursor for feature class 
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    # For each row, evaluate the WELL_YIELD value (index position 
    # of 0), and update WELL_CLASS (index position of 1)
    for row in cursor:
        if (row[0] >= 0 and row[0] <= 10):
            row[1] = 1
        elif (row[0] > 10 and row[0] <= 20):
            row[1] = 2
        elif (row[0] > 20 and row[0] <= 30):
            row[1] = 3
        elif (row[0] > 30):
            row[1] = 4

        # Update the cursor with the updated list
        cursor.updateRow(row)
UpdateCursor の例 2

UpdateCursor を使用して、[バッファー (Buffer)] ツールで使用するバッファー距離のフィールドを更新します。

import arcpy

arcpy.env.workspace = 'c:/data/output.gdb'
fc = 'c:/data/base.gdb/roads'
fields = ['ROAD_TYPE', 'BUFFER_DISTANCE']

# Create update cursor for feature class 
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    # Update the field used in Buffer so the distance is based on road 
    # type. Road type is either 1, 2, 3, or 4. Distance is in meters. 
    for row in cursor:
        # Update the BUFFER_DISTANCE field to be 100 times the 
        # ROAD_TYPE field.
        row[1] = row[0] * 100
        cursor.updateRow(row) 

# Buffer feature class using updated field values
arcpy.Buffer_analysis(fc, 'roads_buffer', 'BUFFER_DISTANCE')

関連トピック