Résumé
The FieldMappings object is a collection of FieldMap objects and it is used as the parameter value for tools that perform field mapping, such as Merge.
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é | Explication | Type de données |
fieldCount (Lecture seule) | The number of output fields. | Integer |
fieldMappings (Lecture/écriture) | A list of FieldMap objects that make up the FieldMappings object. | FieldMap |
fieldValidationWorkspace (Lecture/é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 list of Field objects. Each field object represents the properties of each output field. | Field |
Vue d'ensemble des méthodes
Méthode | Explication |
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ètre | Explication | Type de données |
field_map | The field map to add to the field mappings | FieldMap |
addTable (table_dataset)
Paramètre | Explication | Type de données |
table_dataset | The table to add to the field mappings object. | String |
exportToString ()
Valeur renvoyée
Type de données | Explication |
String | The string representation of the object. |
findFieldMapIndex (field_map_name)
Paramètre | Explication | Type de données |
field_map_name | Find the field map by name. | String |
Valeur renvoyée
Type de données | Explication |
Integer | The index position of the field map. |
getFieldMap (index)
Paramètre | Explication | Type de données |
index | The index position of the field map. | Integer |
Valeur renvoyée
Type de données | Explication |
FieldMap | The field map from the field mappings. |
loadFromString (string)
Paramètre | Explication | Type de données |
string | The string representation of the object. | String |
removeAll ()
removeFieldMap (index)
Paramètre | Explication | Type de données |
index | The index position of the field map. | Integer |
replaceFieldMap (index, value)
Paramètre | Explication | Type de données |
index | The index position of the field map to be replaced. | Integer |
value | The replacement field map. | FieldMap |
Exemple de code
FieldMappings example
FieldMap objects are often used when there is a need to merge similar datasets into one, all-encompassing dataset. In this example, the feature class Trees and the shapefile Plants.shp are merged into one feature class: Vegetation. Both original feature classes have two attributes: Type and Diameter. These two attributes must be maintained through the merge.
import arcpy
# Set the workspace
arcpy.env.workspace = 'c:/base'
in_file1 = 'data.gdb/Trees'
in_file2 = 'Plants.shp'
output_file = 'data.gdb/Vegetation'
# Create the required FieldMap and FieldMappings objects
fm_type = arcpy.FieldMap()
fm_diam = arcpy.FieldMap()
fms = arcpy.FieldMappings()
# Get the field names of vegetation type and diameter for both original
# files
tree_type = "Tree_Type"
plant_type = "Plant_Type"
tree_diam = "Tree_Diameter"
plant_diam = "Diameter"
# Add fields to their corresponding FieldMap objects
fm_type.addInputField(in_file1, tree_type)
fm_type.addInputField(in_file2, plant_type)
fm_diam.addInputField(in_file1, tree_diam)
fm_diam.addInputField(in_file2, plant_diam)
# Set the output field properties for both FieldMap objects
type_name = fm_type.outputField
type_name.name = 'Veg_Type'
fm_type.outputField = type_name
diam_name = fm_diam.outputField
diam_name.name = 'Veg_Diam'
fm_diam.outputField = diam_name
# Add the FieldMap objects to the FieldMappings object
fms.addFieldMap(fm_type)
fms.addFieldMap(fm_diam)
# Merge the two feature classes
arcpy.Merge_management([in_file1, in_file2], output_file, fms)
FieldMappings example 2
This sample displays the option of using FieldMap objects to merge fields, using the FeatureClassToFeatureClass tool. In this example, a feature class contains information about the number of accidents per intersection in a city. Each year of data is maintained in one field. The user would like to find the average number of accidents in each intersection, without altering the existing table.
import arcpy
# Set the workspace
arcpy.env.workspace = 'c:/base/data.gdb'
in_file = 'AccidentData'
out_file = 'AverageAccidents'
# Create the necessary FieldMap and FieldMappings objects
fm = arcpy.FieldMap()
fm1 = arcpy.FieldMap()
fms = arcpy.FieldMappings()
# Each field with accident data begins with 'Yr' (from Yr2007 to Yr2012).
# The next step loops through each of the fields beginning with 'Yr',
# and adds them to the FieldMap Object
for field in arcpy.ListFields(in_file, 'Yr*'):
fm.addInputField(in_file, field.name)
# Set the merge rule to find the mean value of all fields in the
# FieldMap object
fm.mergeRule = 'Mean'
# Set properties of the output name.
f_name = fm.outputField
f_name.name = 'AvgAccidents'
f_name.aliasName = 'AvgAccidents'
fm.outputField = f_name
# Add the intersection field to the second FieldMap object
fm1.addInputField(in_file, "Intersection")
# Add both FieldMaps to the FieldMappings Object
fms.addFieldMap(fm)
fms.addFieldMap(fm1)
# Create the output feature class, using the FieldMappings object
arcpy.FeatureClassToFeatureClass_conversion(
in_file, arcpy.env.workspace, out_file, field_mapping=fms)