FieldMappings

Récapitulatif

L'objet FieldMappings est un ensemble d'objets FieldMap et il sert de valeur de paramètre pour les outils qui exécutent un appariement de champs, tels que l'outil Fusionner.

Discussion

L'objet FieldMappings est une collection d'objets FieldMap et il sert de valeur de paramètre pour les outils qui exécutent un appariement de champs, tels que l'outil Fusionner. La méthode la plus simple pour utiliser ces objets consiste à créer d'abord un objet FieldMappings, puis à initialiser ses objets FieldMap en ajoutant les classes d'entités ou les tables en entrée à associer. Une fois toutes les entrées fournies, l'objet FieldMappings contient un objet FieldMap, ou un champ en sortie, pour chaque nom de champ unique issu de toutes les entrées. Cette liste peut être modifiée en ajoutant de nouveaux champs, en modifiant les propriétés et/ou le contenu d'un champ en sortie ou en supprimant tous les champs en sortie non souhaités.

Les propriétés de l'objet FieldMap incluent les positions de début et de fin d'une valeur textuelle en entrée, afin qu'une nouvelle valeur en sortie puisse être créée à l'aide d'une partie d'une valeur en entrée. Si un objet FieldMap contient plusieurs champs en entrée provenant de la même table ou classe d'entités, les valeurs de chaque enregistrement sont fusionnées à l'aide de la propriété mergeRule. Cette méthode est pratique pour joindre des valeurs, telles qu'un nom de rue stocké dans un champ et un type de rue stocké dans un autre, par exemple, Eureka et Rue. La propriété joinDelimiter de FieldMap est utilisée si la valeur spécifiée de mergeRule est Jointure. Tout ensemble de caractères, tel qu'un espace, peut être utilisé comme délimiteur. Dans l'exemple ci-dessus, la valeur Rue Eureka serait créée.

Syntaxe

FieldMappings ()

Propriétés

PropriétéExplicationType de données
fieldCount
(Lecture seule)

The number of output fields.

Integer
fieldValidationWorkspace
(Lecture et écriture)

The workspace type that defines the rules for attribute field naming. These rules are used when determining the output field names, which are based on the names of the input fields. For example, setting the fieldValidationWorkspace property to the path of a folder on disk containing the input shapefiles will result in the output field names being truncated to 10 characters. Setting the fieldValidationWorkspace property to the path of a file geodatabase will allow for much longer field names. The fieldValidationWorkspace property should be set with a consideration for the output format.

String
fields
(Lecture seule)

A Python List that contains a list of field objects. Each field object represents the properties of each output field.

Field

Vue d'ensemble des méthodes

MéthodeExplication
addFieldMap (field_map)

Add a field map to the field mappings.

addTable (table_dataset)

Adds a table to the field mappings object.

exportToString ()

Exports the object to its string representation.

findFieldMapIndex (field_map_name)

Find a field map within the field mappings by name.

getFieldMap (index)

Gets a field map from the field mappings.

loadFromString (string)

Restore the object using its string representation. The exportToString method can be used to create a string representation.

removeAll ()

Removes all values and creates an empty object.

removeFieldMap (index)

Removes a field map from the field mappings.

replaceFieldMap (index, value)

Replace a field map within the field mappings.

Méthodes

addFieldMap (field_map)
ParamètreExplicationType de données
field_map

The field map to add to the field mappings

FieldMap
addTable (table_dataset)
ParamètreExplicationType de données
table_dataset

The table to add to the field mappings object.

String
exportToString ()
Valeur renvoyée
Type de donnéesExplication
String

The string representation of the object.

findFieldMapIndex (field_map_name)
ParamètreExplicationType de données
field_map_name

Find the field map by name.

String
Valeur renvoyée
Type de donnéesExplication
Integer

The index position of the field map.

getFieldMap (index)
ParamètreExplicationType de données
index

The index position of the field map.

Integer
Valeur renvoyée
Type de donnéesExplication
FieldMap

The field map from the field mappings.

loadFromString (string)
ParamètreExplicationType de données
string

The string representation of the object.

String
removeAll ()
removeFieldMap (index)
ParamètreExplicationType de données
index

The index position of the field map.

Integer
replaceFieldMap (index, value)
ParamètreExplicationType de données
index

The index position of the field map to be replaced.

Integer
value

The replacement field map.

FieldMap

Exemple de code

Exemple d'objet FieldMappings
import arcpy
from arcpy import env

# Input feature classes contain a PID field that is a composite of
#   the Township, Range, Section, QuarterSection, Block, Lot, and SubLot values.
#   We need to define how to break the PID field into two fields: TWNSHIP and BLOCK,
#   then run the Merge tool.
#   PID field and field map. 
#                    1 1 1 1 1 1 1
#  1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
# +-------------------------------+
# |2|0|3|0|8|2|1|0|0|1|0|0|5|0|0|0|
# +-------------------------------+
# +---TWNSHIP---+-------BLOCK-----+

infc1 = "C:/Data/NewParcels.shp"
infc2 = "Parcels"
outfc = "Parcels_Merge"

# Create a fieldmappings object and two fieldmap objects
#
fieldmappings = arcpy.FieldMappings()
fldmap_TWNSHIP = arcpy.FieldMap()
fldmap_BLOCK = arcpy.FieldMap()

# Create a value table to hold names of input feature classes for Merge tool.
#
vt = arcpy.ValueTable()

# Add all fields from first input feature class to fieldmappings object
#   AddTable is the most efficient way.
#
fieldmappings.addTable(infc1)

# Add the TWNSHIP and BLOCK field from the first input feature class 
#   to fieldmap object.
#
fldmap_TWNSHIP.addInputField(infc1)
fldmap_BLOCK.addInputField(infc1)
vt.addRow(infc1)

# Add all fields from second input feature class to fieldmappings object
#
env.workspace = "C:/Data/Boulder.gdb"
fieldmappings.addTable(infc2)

# Add the TWNSHIP and BLOCK field from the second input feature class 
#   to fieldmap object.
#
fldmap_TWNSHIP.addInputField(infc2)
fldmap_BLOCK.addInputField(infc2)
vt.addRow(infc2)

# Set the starting and ending positions of the input field in the fieldmap.
#   This is for TWNSHIP
for x in range(0, fldmap_TWNSHIP.inputFieldCount):
    fldmap_TWNSHIP.setStartTextPosition(x, 0)
    fldmap_TWNSHIP.setEndFieldPosition(x, 6)

# Set the starting and ending positions of the input field in the fieldmap.
#   This is for BLOCK
for x in range(0, fldmap_BLOCK.inputFieldCount):
    fldmap_BLOCK.setStartTextPosition(x, 7)
    fldmap_BLOCK.setEndFieldPosition(x, 15)

# Get the new field object from fieldmap, set field name, place back in fieldmap.
#
fld_TWNSHIP = fldmap_TWNSHIP.outputField
fld_TWNSHIP.name = "TWNSHIP"
fldmap_TWNSHIP.outputField = fld_TWNSHIP
fld_BLOCK = fldmap_BLOCK.outputField
fld_BLOCK.name = "BLOCK"
fldmap_BLOCK.outputField = fld_BLOCK

# Add the new fieldmap object to fieldmappings object.
fieldmappings.addFieldMap(fldmap_TWNSHIP)
fieldmappings.addFieldMap(fldmap_BLOCK)

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

Rubriques connexes


7/10/2012