サマリー
UpdateCursor establishes read-write access to records returned from a feature class or table.
Returns an iterator of lists. The order of values in the list matches the order of fields specified by the field_names argument.
説明
Update cursors can be iterated using a for loop. Update cursors also support with statements to reset iteration and aid in removal of locks. However, using a del statement to delete the object or wrapping the cursor in a function to have the cursor object go out of scope should be considered to guard against all locking cases.
異なるカーソルを使用して、同じワークスペースで同時に挿入または更新操作を開くには、編集セッションの開始が必要です。
編集セッション内でのみ編集できるデータセット タイプの一部を次に示します。
- トポロジに属しているフィーチャクラス
- ジオメトリック ネットワークに属しているフィーチャクラス
- ネットワーク データセットに属しているフィーチャクラス
- エンタープライズ ジオデータベース内のバージョン対応データセット
- クラス エクステンションを持つ一部のオブジェクトとフィーチャクラス
In Python 2, UpdateCursor supports the iterator next method to retrieve the next row outside of a loop. In Python 3, the equivalent is performed by using the Python built-in next function.
注意:
The Calculate Field and Calculate Fields tools can also be used to update field values.
注意:
Using an UpdateCursor on a layer with a joined table is not supported.
構文
UpdateCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause}, {datum_transformation}, {explicit}, {spatial_filter}, {spatial_relationship}, {search_order})
パラメーター | 説明 | データ タイプ |
in_table | フィーチャクラス、レイヤー、テーブル、またはテーブル ビュー。 | String |
field_names [field_names,...] | フィールド名のリスト (またはタプル)。 単一フィールドの場合、文字列のリストの代わりに文字列を使用できます。 フィールドのリストの代わりにアスタリスク (*) を使用すると、入力テーブルのすべてのフィールドにアクセスできます (BLOB フィールドは除きます)。 ただし、パフォーマンスとフィールドの順序の信頼性を高めるために、フィールドのリストは実際に必要なフィールドのみに絞り込むことをお勧めします。 ラスター フィールドはサポートされていません。 その他の情報には、フィールド名の代わりにトークン (OID@ など) を使用してアクセスできます。
| String |
where_clause | 返されるレコードを制限するオプションの式。 WHERE 句と SQL ステートメントの詳細については、「ArcGIS で使用されるクエリ式への SQL リファレンス」をご参照ください。 (デフォルト値は次のとおりです None) | String |
spatial_reference | フィーチャクラスの空間参照。 この引数を指定すると、フィーチャが入力の空間参照から投影 (または変換) されます。 指定しないと、入力フィーチャクラスの空間参照が使用されます。 この引数に有効な値は、SpatialReference オブジェクトまたはそれに相当する文字列です。 (デフォルト値は次のとおりです None) | SpatialReference |
explode_to_points | フィーチャを個々のポイントまたは頂点に分解します。 explode_to_points が True に設定されている場合、たとえば 5 つのポイントがあるマルチポイント フィーチャは 5 つの行で表現されます。 (デフォルト値は次のとおりです False) | Boolean |
sql_clause | リストまたはタプルに編成された、SQL の接頭辞句と接尾辞句のペア。 SQL 接頭辞句は None、DISTINCT、TOP をサポートしています。 SQL 接尾辞句は None、ORDER BY、GROUP BY をサポートしています。 Use DISTINCT in a prefix clause for a table located in a file geodatabase. DISTINCT is not honored in a relational database.
Use TOP in a prefix clause and ORDER BY in a postfix clause.
Use GROUP BY in a postfix clause.
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 |
datum_transformation | カーソルでフィーチャを 1 つの空間参照から別の空間参照に投影する際に、これらの空間参照が同じ測地基準系を共有していない場合は、適切な測地基準系変換を指定する必要があります。 An update cursor can perform a projection or transformation at two stages: when reading the features from the feature class on disk and when writing the updated features to the feature class. ListTransformations 関数を使用すると、2 つの空間参照の間で有効な測地基準系変換のリストを表示できます。 | String |
explicit [explicit,...] | フィールドにデフォルト値があり、フィールドで NULL 値が許可されている場合、True の値を使用すると、デフォルト値が明示的にオーバーライドされ、NULL 値がレコードに挿入されます。 値として False を使用すると、NULL の代わりにデフォルト値が挿入されます。 Apply the explicit rule to all fields: ブール値のリストを使用して、フィールドごとに明示的なルールを適用することもできます。 値のリストの長さはフィールドのリストと揃える必要があります。 Apply the explicit rule to only the first two fields: (デフォルト値は次のとおりです False) | Boolean |
spatial_filter | フィーチャを空間的にフィルター処理するために使用されるジオメトリ オブジェクト。 この引数を指定すると、カーソルは指定したジオメトリと [spatial_relationship] の値に基づいて、返されるフィーチャを制限します。 (デフォルト値は次のとおりです None) | Geometry |
spatial_relationship | spatial_filter 引数の入力ジオメトリとクエリ ジオメトリ間の空間リレーションシップ。 この引数は、spatial_filter 引数を指定した場合にのみ適用されます。
(デフォルト値は次のとおりです INTERSECTS) | String |
search_order | RDBMS によって空間検索が適用される順序。 このプロパティはエンタープライズ ジオデータベース データにのみ影響し、spatial_filter 引数を指定した場合にのみ適用されます。
(デフォルト値は次のとおりです ATTRIBUTEFIRST) | 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. 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 |
方法の概要
方法 | 説明 |
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 |
コードのサンプル
Use UpdateCursor to update a field value by evaluating the values of other fields.
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)
Use UpdateCursor to update a field of buffer distances for use with the Buffer function.
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.analysis.Buffer(fc, 'roads_buffer', 'BUFFER_DISTANCE')
Use UpdateCursor with a spatial filter to update field values.
import arcpy
arr = arcpy.Array(
[arcpy.Point(342917.4, 553980.8), arcpy.Point(366915.9, 594749.1)]
)
new_road = arcpy.Polyline(arr, spatial_reference=arcpy.SpatialReference(26971))
fc = r"C:\data\chicago.gdb\houses"
fields = ["OCCUPIED"]
with arcpy.da.UpdateCursor(
fc, fields, where_clause="OCCUPIED != 'Vacant'", spatial_filter=new_road
) as cursor:
for row in cursor:
row[0] = 'Vacant'
cursor.updateRow(row)