FieldMap

摘要

FieldMap 对象提供一个字段定义和一个输入字段列表,该输入字段列表来自一组表或要素类。

说明

FieldMap 对象的属性包括输入文本值的起始位置和结束位置,因此可以使用一组输入值创建输出值。如果 FieldMap 对象包含多个来自同一表或要素类的输入字段,则将使用 mergeRule 属性合并每个记录的值。这样可以方便地连接各个值,例如保存在一个字段中的街道名称以及保存在另一个字段中的街道类型,如 Eureka 和 Street。如果指定 mergeRuleJoin,则将使用 FieldMapjoinDelimiter 属性。任何字符集(如空格)都可用作分隔符。在以上示例中,将会创建值 Eureka Street。

FieldMappings 对象是一组 FieldMap 对象,用作字段映射工具的参数值,例如合并。要处理这些对象,最简单的方法就是先创建 FieldMappings 对象,然后通过添加要组合的输入要素类或表对 FieldMap 对象进行初始化。提供了所有输入后,FieldMappings 对象将为所有输入中的每个唯一字段名提供一个 FieldMap 对象或输出字段。可对此列表进行修改,具体方法有添加新字段、更改输出字段的属性或内容,或移除任何不需要的输出字段。

语法

 FieldMap  ()

属性

属性说明数据类型
inputFieldCount
(只读)

所定义的输入字段的数量。

Integer
joinDelimiter
(可读写)

字符串值,用于在输出字段类型为字符串而 mergeRule 的值为 Join 时分隔同一个表中的输入值。

String
mergeRule
(可读写)

定义如何将同一输入表中的两个或多个字段值合并成单个输出值。以下为有效选项:

  • First使用第一个输入值。
  • Last使用最后一个输入值。
  • Join对多个值进行合并,可使用分隔符将多个值分开(仅当输出字段类型是文本时才有效)。
  • Min使用最小的输入值(仅当输出字段类型是数值时才有效)。
  • Max使用最大的输入值(仅当输出字段类型是数值时才有效)。
  • Mean使用输入值的平均值(仅当输出字段类型是数值时才有效)。
  • Median使用输入值的中值(仅当输出字段类型是数值时才有效)。
  • Sum使用输入值的总和(仅当输出字段类型是数值时才有效)。
  • StDev使用输入值的标准差(仅当输出字段类型是数值时才有效)。
  • Count统计计算中包括的值的数目。计数范围包括除空值外的每个值。
String
outputField
(可读写)

输出字段的属性在 Field 对象中进行设置或返回。

Field

方法概述

方法说明
addInputField (table_dataset, field_name, {start_position}, {end_position})

将输入字段添加到字段映射。

findInputFieldIndex (table_dataset, field_name)

从字段映射查找输入字段。

getEndTextPosition (index)

返回字段映射的文本结束位置。

getInputFieldName (index)

根据字段索引位置,返回字段映射中输入字段的名称。

getInputTableName (index)

根据表索引位置,返回字段映射中输入表的名称。

getStartTextPosition (index)

返回 FieldMap 对象的文本起始位置。

removeAll ()

移除所有值并创建一个空对象。

removeInputField (index)

FieldMap 对象中移除输入字段。

setEndTextPosition (index, end_position)

设置字段映射的文本结束位置。

setStartTextPosition (index, start_position)

设置字段映射的文本起始位置。

方法

addInputField (table_dataset, field_name, {start_position}, {end_position})
参数说明数据类型
table_dataset

要添加到字段映射中的表。

String
field_name

输入字段名称。

String
start_position

输入文本值的起始位置。

(默认值为 -1)

Integer
end_position

输入文本值的结束位置。

(默认值为 -1)

Integer
findInputFieldIndex (table_dataset, field_name)
参数说明数据类型
table_dataset

要添加到字段映射中的表。

String
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)
参数说明数据类型
index

The index position.

Integer
返回值
数据类型说明
Integer

文本起始位置。

removeAll ()
removeInputField (index)
参数说明数据类型
index

The index position.

Integer
setEndTextPosition (index, end_position)
参数说明数据类型
index

索引位置。

Integer
end_position

输入文本值的结束位置。

Integer
setStartTextPosition (index, start_position)
参数说明数据类型
index

索引位置。

Integer
start_position

输入文本值的起始位置。

Integer

代码示例

FieldMap 示例

要将相似数据集合并到一个无所不包的数据集中时,通常使用 FieldMap 对象。在该示例中,要素类 Trees 和 shapefile Plants.shp 合并至一个要素类:Vegetation。这两个原始要素类都具有两个属性:类型直径。两个属性都必须通过合并进行维护。

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 示例 2

此示例使用 FeatureClassToFeatureClass 工具显示使用 FieldMap 对象合并字段的选项。在本示例中,要素类包含有关城市中每个十字路口的事故数量的信息。每年的数据都保存在一个字段中。用户希望在不更改现有表格的情况下找出每个十字路口的平均事故数量。

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)

相关主题