FieldMap
汇总
FieldMap 对象提供一个字段定义和一个输入字段列表,该输入字段列表来自一组表或要素类。
讨论
FieldMap 对象的属性包括输入文本值的起始位置和结束位置,因此可以使用一组输入值创建输出值。 如果 FieldMap 对象包含多个来自同一表或要素类的输入字段,则将使用 mergeRule 属性合并每个记录的值。 这是一种连接值的简便方法,例如在一个字段中保存的街道名称和在另一个字段中保存的街道类型,如 Eureka 和 Street。 当指定 Join 作为 mergeRule 值时,会使用 FieldMap 对象的 joinDelimiter 属性。 任何字符集(包括空格)都可以用作分隔符。 在上方示例中,将会创建值 Eureka Street。
FieldMappings 对象是一组 FieldMap 对象,用作字段映射工具的参数值,例如 合并 。 要处理这些对象,最直接的方法就是先创建 FieldMappings 对象,然后通过添加要组合的输入要素类或表对其 FieldMap 对象进行初始化。 提供了所有输入后, FieldMappings 对象将为所有输入中的每个唯一字段名提供一个 FieldMap 对象或输出字段。 通过添加新字段或移除不需要的输出字段来修改此列表。 要更改 FieldMap 对象的 outputField 属性,请将此属性分配给变量。 然后更改变量的属性(如 name 和 type ),并将 outputField 属性重新分配给此变量。
注:
使用此方法修改 FieldMap 对象的 outputField 属性。无法就地修改属性。
语法
FieldMap()
属性
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
inputFieldCount (只读) |
所定义的输入字段的数量。 |
Integer |
|
joinDelimiter (读取和写入) |
字符串值,用于在输出字段类型为字符串而 |
String |
|
mergeRule (读取和写入) |
指定如何将同一输入表中的两个或多个字段值合并成单个输出值。
|
String |
|
outputField (读取和写入) |
输出字段的属性在 |
Field |
方法
addInputField(table_dataset, field_name, {start_position}, {end_position})
将输入字段添加到字段映射。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
table_dataset |
The table that will be added to the field map. |
String |
|
field_name |
The input field name. |
String |
|
start_position (可选) |
The start position of an input text value. (默认值为 -1) |
Integer |
|
end_position (可选) |
The end position of an input text value. (默认值为 -1) |
Integer |
findInputFieldIndex(table_dataset, field_name)
从字段映射查找输入字段。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
table_dataset |
The table that will be added to the field map. |
String |
|
field_name |
The field name. |
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
Integer |
字段名的索引位置。 |
getEndTextPosition(index)
返回字段映射的文本结束位置。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
index |
The index position. |
Integer |
返回值
| 数据类型 | 说明 |
|---|---|
|
Integer |
文本结束位置。 |
getInputFieldName(index)
根据字段索引位置,返回字段映射中输入字段的名称。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
index |
The index position. |
Integer |
返回值
| 数据类型 | 说明 |
|---|---|
|
String |
输入字段名称。 |
getInputTableName(index)
根据表索引位置,返回字段映射中输入表的名称。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
index |
The index position. |
Integer |
返回值
| 数据类型 | 说明 |
|---|---|
|
String |
输入表名称。 |
getStartTextPosition(index)
返回 FieldMap 对象的文本起始位置。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
index |
The index position. |
Integer |
返回值
| 数据类型 | 说明 |
|---|---|
|
Integer |
文本起始位置。 |
removeAll()
移除所有值并创建一个空对象。
removeInputField(index)
从 FieldMap 对象中移除输入字段。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
index |
The index position. |
Integer |
setEndTextPosition(index, end_position)
设置字段映射的文本结束位置。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
index |
The index position. |
Integer |
|
end_position |
The end position of an input text value. |
Integer |
setStartTextPosition(index, start_position)
设置字段映射的文本起始位置。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
index |
The index position. |
Integer |
|
start_position |
The start position of an input text value. |
Integer |
代码示例
要将相似数据集合并到一个无所不包的数据集中时,通常使用 FieldMap 对象。 在本例中,Trees 要素类和 Plants.shp shapefile 会合并至一个要素类:Vegetation。 两个原始要素类都具有两个属性: Type 和 Diameter 。 两个属性都必须通过合并进行维护。
import arcpy
# Set the workspace
arcpy.env.workspace = 'c:/base'
in_features_1 = 'data.gdb/Trees'
in_features_2 = 'Plants.shp'
out_features = '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_features_1, tree_type)
fm_type.addInputField(in_features_2, plant_type)
fm_diam.addInputField(in_features_1, tree_diam)
fm_diam.addInputField(in_features_2, plant_diam)
# Set the output field properties for both FieldMap objects by creating a new variable.
type_field = fm_type.outputField
type_field.name = 'Veg_Type'
# Set the new variable back into the field map's outputField.
fm_type.outputField = type_field
diam_field = fm_diam.outputField
diam_field.name = 'Veg_Diam'
fm_diam.outputField = diam_field
# Add the FieldMap objects to the FieldMappings object.
fms.addFieldMap(fm_type)
fms.addFieldMap(fm_diam)
# Merge the two feature classes.
arcpy.management.Merge([in_features_1, in_features_2], out_features, fms)
此示例演示了通过 ExportFeatures 函数使用 FieldMap 对象合并字段的操作。 在本示例中,要素类包含有关城市中每个十字路口的事故数量的信息。 每年的数据都保存在一个字段中。 用户希望在不更改现有表格的情况下找出每个十字路口的平均事故数量。
import arcpy
in_features = r'c:/base/data.gdb/AccidentData'
out_features = r'c:/base/data.gdb/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'.
# 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_features, 'Yr*'):
fm.addInputField(in_features, field.name)
# Set the merge rule to find the mean value of all fields in the
# FieldMap object.
fm.mergeRule = 'Mean'
# Set the properties of the output field by creating a new variable.
out_field = fm.outputField
out_field.name = 'AvgAccidents'
out_field.aliasName = 'AvgAccidents'
# Set the new variable back into the field map's outputField
fm.outputField = out_field
# Add the intersection field to the second FieldMap object.
fm1.addInputField(in_features, "Intersection")
# Add both FieldMap objects to the FieldMappings object.
fms.addFieldMap(fm)
fms.addFieldMap(fm1)
# Create the output feature class using the FieldMappings object.
arcpy.conversion.ExportFeatures(in_features, out_features, field_mapping=fms)