入力フィールドから出力フィールドへのマッピング

一般的なジオプロセシング タスクとは、多数のデータセットを新しいデータセットまたは既存のデータセットにマージし、より広いエリアを網羅するデータセットや、多数のレコードを含むテーブルを作成することです。 マージ操作やアペンド操作で使用されるすべての入力では、属性 (またはフィールド) は同じであることがほとんどですが、それらが一致せず、名前とタイプが異なるフィールド間でリレーションシップをマッピングしなくてはならないことがあります。 フィールドのマッピングの一例として、[マージ (Merge)] ツールを使用すれば、データが適切な出力フィールドに正しい値で配置されるように、この関係のマッピングを容易に行うことができます。

FieldMap

FieldMap オブジェクトは、値を提供する一連のテーブルまたはフィーチャクラスから入力フィールドの定義とそれらのリストを提供します。

FieldMap オブジェクトのプロパティには、入力テキスト値の開始位置と終了位置が含まれるため、入力値の一部を使って出力値を作成することができます。FieldMap オブジェクトに同じテーブルまたはフィーチャクラスからの複数入力フィールドが含まれる場合、各レコードの値は mergeRule プロパティを使用してマージされます。これは、たとえば「国道」と「1 号」のように、道路タイプが含まれているフィールドと道路名が含まれているフィールドの値を結合するのに役立ちます。FieldMapjoinDelimiter プロパティは、mergeRule の値として Join が指定されている場合に使用されます。スペースなど、任意の文字のセットを区切り文字として使用できます。前記の例では、「国道 1 号」という値が作成されます。

FieldMappings

FieldMappings オブジェクトは FieldMap オブジェクトのコレクションで、[マージ (Merge)] などのフィールド マッピングを実行するツールのパラメーター値として使用されます。これらのオブジェクトを操作する最も簡単な方法は、まず FieldMappings オブジェクトを作成した後、結合対象の入力フィーチャクラスまたはテーブルを追加することにより、そのオブジェクトの FieldMap オブジェクトを初期化することです。すべての入力が指定された後、FieldMappings オブジェクトには、そのすべての入力の一意なフィールド名ごとに FieldMap オブジェクトまたは出力フィールドを 1 つずつ含まれます。このリストは、新しいフィールドを追加する、出力フィールドのプロパティまたは内容を変更する、必要のない出力フィールドを削除することにより、変更できます。

次の例では、米国の国勢調査データを含むフィーチャクラスをいくつかをマージし、新しいフィーチャクラスを作成します。 すべての入力に含まれる入力属性の 1 つに、数値フィールド STFID があります。 これは、米国のすべての国勢調査ブロックを表す 15 桁の一意識別子です。 この値は、4 つの構成要素に分類できます。 最初の 2 桁は州コード、次の 3 桁は郡、続く 6 桁は国勢調査区域、そして最後の 4 桁は国勢調査ブロックを表します。 360899912001006 という値は、ニューヨーク州 (36) セントローレンス郡 (089) の国勢調査区域 (991200) にある、ニューヨーク州ポツダムのニューヨーク州立大学が含まれている国勢調査ブロック (1006) を表します。 入力データには STFID 属性しか含まれていないため、このスクリプト例ではこれらのフィーチャクラスをマージし、さらに 2 つのフィールド TRACTID および BLOCKID を作成します。 そのためには、addTable メソッドを使って FieldMappings オブジェクトを初期化し、それぞれの入力を追加します。 続いて、2 つの FieldMap オブジェクトを作成し、そのプロパティを設定し、それらを FieldMappings オブジェクトに追加することにより、デフォルトの FieldMappings オブジェクトを変更します。

import arcpy

arcpy.env.workspace = "C:/Data/CityBlocks.gdb"
outfc = "C:/Data/CityBlocks.gdb/AllBlocks"

# Each of the input Feature classes has an STFID, which is the combination of 
# the Tract ID and Block ID for each block. Separate these values out from this 
# field into two new fields, TRACTID and BLOCKID.

# Create a fieldmappings and two new fieldmaps.
fieldmappings = arcpy.FieldMappings()
fldmap_TRACTID = arcpy.FieldMap()
fldmap_BLOCKID = arcpy.FieldMap()

# List all the feature classes in the workspace that start with 'block' in 
# their name and are of polygon feature type.
fcs = arcpy.ListFeatureClasses("block*", "Polygon")

# Create a value table that will hold the input feature classes to Merge
vTab = arcpy.ValueTable()
for fc in fcs:
    # Adding a table is the fast way to load all the fields from the input 
    # into fieldmaps held by the fieldmappings object.
    fieldmappings.addTable(fc)

    # In this example also create two fieldmaps by 'chopping up' an input field. 
    # Feed the chopped field into the new fieldmaps.
    fldmap_TRACTID.addInputField(fc, "STFID")
    fldmap_BLOCKID.addInputField(fc, "STFID")

    # Populate the input value table with feature classes
    vTab.addRow(fc)

# Set the starting and ending position of the fields going into the TractID 
# fieldmap. This is the location in the STFID field where the TractID falls.
for x in range(0, fldmap_TRACTID.inputFieldCount):
    fldmap_TRACTID.setStartTextPosition(x, 5)
    fldmap_TRACTID.setEndTextPosition(x, 10)

# Set the Name of the Field output from this field map.
fld_TRACTID = fldmap_TRACTID.outputField
fld_TRACTID.name = "TRACTID"
fldmap_TRACTID.outputField = fld_TRACTID

# Set the starting and ending position of the fields going into the BlockID 
# fieldmap. This is the location in the STFID field where the blockID falls.
for x in range(0, fldmap_BLOCKID.inputFieldCount):
    fldmap_BLOCKID.setStartTextPosition(x, 11)
    fldmap_BLOCKID.setEndTextPosition(x, 16)

# Set the Name of the Field output from this field map.
fld_BLOCKID = fldmap_BLOCKID.outputField
fld_BLOCKID.name = "BLOCKID"
fldmap_BLOCKID.outputField = fld_BLOCKID

# Add the custom fieldmaps into the fieldmappings object.
fieldmappings.addFieldMap(fldmap_TRACTID)
fieldmappings.addFieldMap(fldmap_BLOCKID)

# Run the Merge tool.
arcpy.Merge_management(vTab, outfc, fieldmappings)

次の例では、FieldMap オブジェクトの addTable メソッドを使用して FieldMappings オブジェクトを作成した後、このオブジェクトを変更する方法を示します。 この方法は、異なる名前を持つ入力のフィールドが、論理的には同じ値を含んでいる場合に重要となります。

import arcpy

outfc = "C:/data/CityData.gdb/AllBlocks"

# Want to merge these two feature classes together. Have a field that has the 
# same content but the names are slightly different: Blocks1 has TRACT2000 
# and Blocks2 TRACTCODE. Name the output the same as Blocks1.
fc1 = "C:/data/CityData.gdb/Blocks1"
fc2 = "C:/data/CityData.gdb/Blocks2"

# Create a new fieldmappings and add the two input feature classes.
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(fc1)
fieldmappings.addTable(fc2)

# First get the TRACT2000 fieldmap. Then add the TRACTCODE field from Blocks2 
# as an input field. Then replace the fieldmap within the fieldmappings object.
fieldmap = fieldmappings.getFieldMap(fieldmappings.findFieldMapIndex("TRACT2000"))
fieldmap.addInputField(fc2, "TRACTCODE")
fieldmappings.replaceFieldMap(fieldmappings.findFieldMapIndex("TRACT2000"), fieldmap)

# Remove the TRACTCODE fieldmap.
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("TRACTCODE"))

# Run the Merge tool.
arcpy.Merge_management([fc1, fc2], outfc, fieldmappings)

このトピックの内容
  1. FieldMap
  2. FieldMappings