FieldMap (arcpy)

Zusammenfassung

The FieldMap object provides a field definition and a list of input fields taken from a set of tables or feature classes.

Diskussion

Das FieldMappings-Objekt ist eine Sammlung von FieldMap-Objekten und wird als Parameterwert für Werkzeuge für die Feldzuordnung verwendet, beispielsweise Zusammenführen (Merge). Sie können am einfachsten mit diesen Objekten arbeiten, indem Sie zuerst ein FieldMappings-Objekt erstellen und dann die zugehörigen FieldMap-Objekte initialisieren, indem Sie die zu kombinierenden Eingabe-Feature-Classes oder Tabellen hinzufügen. Wenn alle Eingaben bereitgestellt wurden, enthält das FieldMappings-Objekt ein FieldMap-Objekt (bzw. ein Ausgabefeld) für jeden eindeutigen Feldnamen aus allen Eingaben. Sie können diese Liste ändern, indem Sie neue Felder hinzufügen, die Eigenschaften und/oder den Inhalt von Ausgabefeldern ändern oder unerwünschte Ausgabefelder entfernen.

Die Eigenschaften des FieldMap-Objekts enthalten die Anfangs- und die Endposition eines Eingabetextwertes, sodass ein neuer Ausgabewert mit einem Teil eines Eingabewertes erstellt werden kann. Wenn ein FieldMap-Objekt mehrere Eingabefelder aus derselben Tabelle oder Feature-Class enthält, werden die Werte aller Datensätze unter Verwendung der Eigenschaft MergeRule zusammengeführt. Dies ist eine benutzerfreundliche Möglichkeit zum Verbinden von Werten, beispielsweise eines Straßennamens in einem Feld und einer Straßenart, die in einem anderen Feld enthalten ist (z. B. "Eureka" und "Street"). Der joinDelimiter-Eigenschaft eines FieldMap-Objekts wird verwendet, wenn der mergeRule-Wert Join angegeben wird. Als Trennzeichen können beliebige Zeichen verwendet werden, beispielsweise Leerzeichen. Im obigen Beispiel würde damit der Wert "Eureka Street" erstellt.

Syntax

FieldMap ()

Eigenschaften

EigenschaftErläuterungDatentyp
inputFieldCount
(Schreibgeschützt)

The number of defined input fields.

Integer
joinDelimiter
(Lesen und schreiben)

A string value used to separate input values from the same table if the output field type is string and the mergeRule is Join.

String
mergeRule
(Lesen und schreiben)

Defines how values from two or more fields from the same input table are merged into a single output value. Valid choices are as follows:

  • FirstThe first input value is used.
  • LastThe last input value is used.
  • JoinMerge the values together using the delimiter value to separate the values (only valid if the output field type is text).
  • MinThe smallest input value is used (only valid if the output field type is numeric).
  • MaxThe largest input value is used (only valid if the output field type is numeric).
  • MeanThe mean is calculated using the input values (only valid if the output field type is numeric).
  • MedianThe median is calculated using the input values (only valid if the output field type is numeric).
  • SumThe sum is calculated using the input values (only valid if the output field type is numeric).
  • StDevThe standard deviation is calculated using the input values (only valid if the output field type is numeric).
  • CountThe number of values included in statistical calculations. This counts each value except null values.
String
outputField
(Lesen und schreiben)

The properties of the output field are either set or returned in a Field object.

Field

Methodenübersicht

MethodeErläuterung
addInputField (table_dataset, field_name, {start_position}, {end_position})

Adds input field to the field map.

findInputFieldIndex (table_dataset, field_name)

Finds an input field from the field map.

getEndTextPosition (index)

Gets end text position from the field map.

getInputFieldName (index)

Gets the name of an input field from the field map, based on the field's index.

getInputTableName (index)

Gets the name of an input table from the field map, based on the table's index.

getStartTextPosition (index)

Gets start text position from the field map.

removeAll ()

Removes all values and creates an empty object.

removeInputField (index)

Removes an input field from the field map.

setEndTextPosition (index, end_position)

Sets end text position for the field map.

setStartTextPosition (index, start_position)

Sets the start text position from the field map.

Methoden

addInputField (table_dataset, field_name, {start_position}, {end_position})
ParameterErläuterungDatentyp
table_dataset

The table added to the field map.

String
field_name

The input field name.

String
start_position

The start position of an input text value.

(Der Standardwert ist -1)

Integer
end_position

The end position of an input text value.

(Der Standardwert ist -1)

Integer
findInputFieldIndex (table_dataset, field_name)
ParameterErläuterungDatentyp
table_dataset

The table added to the field map.

String
field_name

The field name.

String
Rückgabewert
DatentypErläuterung
Integer

The index position of the field name.

getEndTextPosition (index)
ParameterErläuterungDatentyp
index

The index position.

Integer
Rückgabewert
DatentypErläuterung
Integer

The end text position.

getInputFieldName (index)
ParameterErläuterungDatentyp
index

The index position.

Integer
Rückgabewert
DatentypErläuterung
String

The input field name.

getInputTableName (index)
ParameterErläuterungDatentyp
index

The index position.

Integer
Rückgabewert
DatentypErläuterung
String

The input table name.

getStartTextPosition (index)
ParameterErläuterungDatentyp
index

The index position.

Integer
Rückgabewert
DatentypErläuterung
Integer

The start text position.

removeAll ()
removeInputField (index)
ParameterErläuterungDatentyp
index

The index position.

Integer
setEndTextPosition (index, end_position)
ParameterErläuterungDatentyp
index

The index position.

Integer
end_position

The end position of an input text value.

Integer
setStartTextPosition (index, start_position)
ParameterErläuterungDatentyp
index

The index position.

Integer
start_position

The start position of an input text value.

Integer

Codebeispiel

FieldMap 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 the arcpy module.
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)
FieldMap 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 the arcpy module
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 f in arcpy.ListFields(in_file, 'Yr*'):
    fm.addInputField(in_file, f.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, "", fms)

Verwandte Themen

9/11/2013