ArcGIS for Desktop

  • ドキュメント
  • 価格
  • サポート

  • My Profile
  • ヘルプ
  • Sign Out
ArcGIS for Desktop

ArcGIS Online

組織のマッピング プラットフォーム

ArcGIS for Desktop

完全なプロ仕様の GIS

ArcGIS for Server

エンタープライズ GIS

ArcGIS for Developers

位置情報利用アプリの開発ツール

ArcGIS Solutions

各種業界向けの無料のテンプレート マップおよびテンプレート アプリケーション

ArcGIS Marketplace

組織で使えるアプリとデータを取得

  • ドキュメント
  • 価格
  • サポート
Esri
  • サイン イン
user
  • マイ プロフィール
  • サイン アウト

Help

  • ホーム
  • はじめに
  • マップ
  • 解析
  • データ管理
  • ツール
  • その他...

SearchCursor

  • サマリ
  • 説明
  • 構文
  • 特性
  • メソッドの概要
  • メソッド
  • コードのサンプル

サマリ

SearchCursor establishes read-only access to the records returned from a feature class or table.

Returns an iterator of tuples. The order of values in the tuple matches the order of fields specified by the field_names argument.

説明

Geometry properties can be accessed by specifying the token SHAPE@ in the list of fields.

Search cursors can be iterated using a For loop.

The records returned by SearchCursor can be constrained to match attribute criteria or spatial criteria.

Accessing full geometry with SHAPE@ is an expensive operation. If only simple geometry information is required, such as the x,y coordinate of a point, use tokens such as SHAPE@XY, SHAPE@Z, and SHAPE@M for faster, more efficient access.

注意:

arcpy.da.SearchCursor should not to be confused with the arcpy.SearchCursor.

構文

SearchCursor
 (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})
パラメータ説明データ タイプ
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 if you want 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@XY —フィーチャの重心を表す X 座標と Y 座標の組み合わせ
  • SHAPE@TRUECENTROID —フィーチャの真の重心を表す X 座標と Y 座標の組み合わせ
  • SHAPE@X —フィーチャの X 座標の 2 倍
  • SHAPE@Y —フィーチャの Y 座標の 2 倍
  • SHAPE@Z —フィーチャの Z 座標の 2 倍
  • SHAPE@M —フィーチャの M 値の 2 倍
  • SHAPE@JSON — ジオメトリを表す esri JSON 文字列
  • SHAPE@WKB —OGC ジオメトリの WKB (Well-Known Binary) 表現。ジオメトリ値の汎用的な表現が、連続的なバイト ストリームとして提供されます。
  • SHAPE@WKT —OGC ジオメトリの WKT (Well-Known Text) 表現。ジオメトリ値の汎用的な表現が、テキスト文字列として提供されます。
  • SHAPE@ —フィーチャのジオメトリ オブジェクト
  • SHAPE@AREA —フィーチャの面積の 2 倍
  • SHAPE@LENGTH —フィーチャの長さの 2 倍
  • OID@ —ObjectID フィールドの値
注意:

SHAPE@JSON、SHAPE@WKB、SHAPE@WKT トークンは、ArcGIS 10.1 Service Pack 1 で利用可能になりました。

String
where_clause

An optional expression that limits the records returned. For more information on WHERE clauses and SQL statements, see Building a query expression.

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

String
spatial_reference

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

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

SpatialReference
explode_to_points

Deconstruct a feature into its individual points or vertices. If explode_to_points is set to True, a multipoint feature with five points, for example, is represented by five rows.

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

Boolean
sql_clause

An optional pair of SQL prefix and postfix clauses organized in a list or tuple.

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

An SQL prefix clause is positioned in the first position and will be inserted between the SELECT keyword and the SELECT COLUMN LIST. The SQL prefix clause is most commonly used for clauses such as DISTINCT or ALL.

An SQL postfix clause is positioned in the second position and will be appended to the SELECT statement, following the where clause. The SQL postfix clause is most commonly used for clauses such as ORDER BY.

注意:

DISTINCT, ORDER BY, and ALL are only supported when working with databases. They are not supported by other data sources (such as dBASE or INFO tables).

TOP is only supported by SQL Server and MS Access databases.

(デフォルト値は次のとおりです (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).

tuple

メソッドの概要

メソッド説明
next ()

Returns the next row as a tuple. The order of fields will be returned in the order they were specified when creating the cursor.

reset ()

Resets the cursor back to the first row.

メソッド

next ()

戻り値

データ タイプ説明
tuple
reset ()

コードのサンプル

SearchCursor example 1

Use SearchCursor to step through a feature class and print specific field values and the x,y coordinates of the point.

import arcpy

fc = 'c:/data/base.gdb/well'
fields = ['WELL_ID', 'WELL_TYPE', 'SHAPE@XY']

# For each row print the WELL_ID and WELL_TYPE fields, and the
# the feature's x,y coordinates
with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        print('{0}, {1}, {2}'.format(row[0], row[1], row[2]))
SearchCursor example 2

Use SearchCursor to return a set of unique field values.

import arcpy

fc = 'c:/data/base.gdb/well'
field = 'Diameter'

# Use SearchCursor with list comprehension to return a
# unique set of values in the specified field
values = [row[0] for row in arcpy.da.SearchCursor(fc, field)]
uniqueValues = set(values)
print(uniqueValues)
SearchCursor example 3

Use SearchCursor to return attributes using tokens.

import arcpy

fc = 'c:/data/base.gdb/well'

# For each row print the Object ID field, and use the SHAPE@AREA
#  token to access geometry properties
with arcpy.da.SearchCursor(fc, ['OID@', 'SHAPE@AREA']) as cursor:
    for row in cursor:
        print('Feature {0} has an area of {1}'.format(row[0], row[1]))
SearchCursor example 4

Use SearchCursor with a where clause to identify features that meet specific criteria.

import arcpy

fc = 'c:/base/data.gdb/roads'
class_field = 'Road Class'
name_field = 'Name'

# Create an expression with proper delimiters
expression = arcpy.AddFieldDelimiters(fc, name_field) + ' = 2'

# Create a search cursor using an SQL expression
with arcpy.da.SearchCursor(fc, [class_field, name_field],
                           where_clause=expression) as cursor:
    for row in cursor:
        # Print the name of the residential road
        print(row[1])
SearchCursor example 5A

Use SearchCursor and Python's sorted method to sort rows.For additional sorting options, see Python's Sorting Mini-HOW TO.

import arcpy

fc = 'c:/data/base.gdb/well'
fields = ['WELL_ID', 'WELL_TYPE']

# Use Python's sorted method to sort rows
for row in sorted(arcpy.da.SearchCursor(fc, fields)):
    print('{0}, {1}'.format(row[0], row[1]))
SearchCursor example 5B

Alternatively, sort using sql_clause if the data supports SQL ORDER BY.

import arcpy

fc = 'c:/data/base.gdb/well'
fields = ['WELL_ID', 'WELL_TYPE']

# Use ORDER BY sql clause to sort field values
for row in arcpy.da.SearchCursor(
        fc, fields, sql_clause=(None, 'ORDER BY WELL_ID, WELL_TYPE')):
    print('{0}, {1}'.format(row[0], row[1]))
SearchCursor example 6

Use SQL TOP to limit the number of records to return.

import arcpy

fc = 'c:/data/base.mdb/well'
fields = ['WELL_ID', 'WELL_TYPE']

# Use SQL TOP to sort field values
for row in arcpy.da.SearchCursor(fc, fields, sql_clause=('TOP 3', None)):
    print('{0}, {1}'.format(row[0], row[1]))

関連トピック

  • UpdateCursor
  • InsertCursor
  • カーソルを使用したデータへのアクセス
このトピックへのフィードバック

ArcGIS for Desktop

  • ホーム
  • ドキュメント
  • 価格
  • サポート

ArcGIS プラットフォーム

  • ArcGIS Online
  • ArcGIS for Desktop
  • ArcGIS for Server
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

Esri について

  • 会社概要
  • 採用情報
  • スタッフ ブログ
  • ユーザ カンファレンス
  • デベロッパ サミット
Esri
© Copyright 2016 Environmental Systems Research Institute, Inc. | プライバシー | リーガル