フィールド演算(Calculate Field)ツールの使用

The Calculate Field tool is located in the Data Management toolbox in the Fields toolset. This is the same tool that is called when you click the Field Calculator command from the field shortcut menu of an attribute table. When performing field calculations, it is important to know what type of data you are using and in what context it is going to be used in the future. The syntax that must be used in a calculation expression differs depending on the data source and scripting language.

The following includes a number of important tips and best practices for using the Calculate Field tool.

フィールド演算(Calculate Field)ツールの使用に関するヒントとベスト プラクティス

フィーチャクラス、フィーチャ レイヤ、ラスタ カタログのフィールドの値を計算します。

Expressions can be created using VBScript or a standard Python format. The formatting style of the string used for the expression should be appropriate for the environment (type).

Python 式は、ジオメトリ オブジェクトのプロパティ(typeextentcentroidfirstPointlastPointarealengthisMultipart、および partCount)を使用して作成できます。

Python 式では、ジオメトリの area プロパティや length プロパティに対して単位を指定し、その単位での面積や長さに換算できます(たとえば、!shape.length@kilometers!)。データが地理座標系で格納され、距離単位(たとえば、マイル)が指定されている場合、長さは測地線アルゴリズムを使用して計算されます。地理データで面積単位を使用すると、グローブ全体で度(10 進)が一定でないため、正確な結果になりません。

!shape.area@acres!

[フィーチャ レイヤの作成(Make Feature Layer)][属性検索(Select Layer By Attribute)] でクエリから作成されたフィーチャなど、一連の選択されているフィーチャに対してこのツールを使用すると、選択されているレコードのみが更新されます。

1 回の操作につき、1 つのフィールドにのみ演算を適用できます。

Fields are always enclosed in brackets [ ] for VBScript.

Python の演算の場合、フィールド名は感嘆符で囲まれている必要があります(!fieldname!)。

テキスト フィールドまたは文字フィールドの文字列の演算を行うには、ダイアログ ボックスでは、文字列が二重引用符で囲まれている必要があり(「"string"」)、スクリプトでは、二重引用符で囲まれた文字列がさらに一重引用符で囲まれている必要があります(「'"string"'」)。

このツールは、文字項目を更新するために使用することもできます。文字列を使用する式は、一重引用符で囲む必要があります([CHARITEM] = 'NEW STRING')。ただし、文字列の一部に一重引用符が使用されている場合は、文字列を二重引用符で囲みます([CHARITEM] = "TYPE'A'")。

数値を指定するフィールドの演算を行うには、[条件式] パラメータに数値を入力します。値を引用符で囲む必要はありません。

The arcgis.rand() function is supported by the Calculate Field tool, and the expression type must be Python. The arcgis.rand() function was created for ArcGIS tools and should not be confused with the Python Rand() function.

式とコード ブロックは接続されています。コード ブロックの結果が式に渡される必要があるため、コード ブロックが式に関連付けられている必要もあります。

[コード ブロック] パラメータでは、Python の数学モジュールおよび数学形式を使用できます。追加のモジュールをインポートすることもできます。数学モジュールは、数論的関数と表現関数、べき関数と対数関数、三角関数、角度変換関数、双曲線関数、および数学定数を提供します。数学モジュールの詳細については、Python のヘルプをご参照ください。

以前のバージョンの ArcGIS で保存された VB の *.cal ファイルをそのまま、または最小限の修正を加えて使用できます。ArcObjects を使用する以前のリリースの VBA コードがある場合は、10.0 で利用できるようにするために計算を修正する必要があります。

結合したデータの演算を行う際、結合された列の演算を直接行うことはできません。元のテーブルの列の演算を直接行うことはできます。結合されたデータの演算を行うには、まず、結合されたテーブルまたはレイヤを ArcMap に追加する必要があります。そうすると、このデータの演算を単体で行うことができるようになります。これらの変更は結合された列に反映されます。

The expression type must be Python when running Calculate Field with ArcGIS Engine or ArcGIS Server. Only use Python as the expression type whenever the tool is included in a model that will be published to ArcGIS Server.

フィールド演算(Calculate Field)ツールを使用したコード ブロックの例

CalculateField(フィールド演算)の例:重心の演算

CalculateField(フィールド演算)を使用して、新しいフィールドに重心の値を割り当てます。

# Name: CalculateField_Centroids.py
# Description: Use CalculateField to assign centroid values to new fields

 
# Import system modules
import arcpy
from arcpy import env

try: 
    # Set environment settings
    env.workspace = "C:/data/airport.gdb"
 
    # Set local variables
    inFeatures = "parcels"
    fieldName1 = "xCentroid"
    fieldName2 = "yCentroid"
    fieldPrecision = 18
    fieldScale = 11
    # Expressions are calculated using the Shape Field's geometry property
    expression1 = "float(!SHAPE.CENTROID!.split()[0])"
    expression2 = "float(!SHAPE.CENTROID!.split()[1])"
 
    # Execute AddField
    arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE", 
                              fieldPrecision, fieldScale)
    arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE", 
                              fieldPrecision, fieldScale)
 
    # Execute CalculateField 
    arcpy.CalculateField_management(inFeatures, fieldName1, expression1,
                                    "PYTHON")
    arcpy.CalculateField_management(inFeatures, fieldName2, expression2,
                                    "PYTHON")
except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message

CalculateField(フィールド演算)の例:範囲の演算

CalculateField(フィールド演算)とコード ブロックを使用して、範囲に基づいて値の演算を行います。

# Name: CalculateField_Ranges.py
# Description: Use CalculateField with a codeblock to calculate values
#  based on ranges

 
# Import system modules
import arcpy
from arcpy import env
 
# Set environment settings
env.workspace = "C:/data/airport.gdb"
 
# Set local variables
inTable = "parcels"
fieldName = "areaclass"
expression = "getClass(float(!SHAPE.area!))"
codeblock = """def getClass(area):
    if area <= 1000:
        return 1
    if area > 1000 and area <= 10000:
        return 2
    else:
        return 3"""
 
# Execute AddField
arcpy.AddField_management(inTable, fieldName, "SHORT")
 
# Execute CalculateField 
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON", 
                                codeblock)

CalculateField(フィールド演算)の例:ランダム値の演算

CalculateField(フィールド演算)を使用して、新しいフィールドにランダム値を割り当てます。

# Name: CalculateField_Random.py
# Description: Use CalculateField to assign random values to a new field
 
  
# Import system modules
import arcpy
from arcpy import env
 
# Set environment settings
env.workspace = "C:/data/airport.gdb"
  
# Set local variables
inFeatures = "parcels"
fieldName = "RndValue"
expression = "arcgis.rand('Integer 0 10')"
 
# Execute AddField
arcpy.AddField_management(inFeatures, fieldName, "LONG")
 
# Execute CalculateField 
arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON")

関連項目


7/10/2012