ArcGIS 中的字段映射
个人理解
FieldMappings —— FieldMap的载体,最终放到GP的参数中
FieldMap —— 字段的载体,每个FieldMap中承载一个基础字段、一个合并字段及其合并规则。基础字段和合并字段可以相同
使用流程
1、实例化FieldMappings对象(arcpy.FieldMappings());
2、对需要映射的所有字段,迭代创建FieldMap实例(arcpy.FieldMap())
3、对每个fieldMap对象添加两个字段(基础字段和合并字段 —— fieldMap.addInputField(要素,字段))、一个规则(合并规则 —— fieldMap.mergeRule(合并规则))
4、将所有的fieldMap实例添加到fieldMappings实例中
代码示例
# create field mapping
def _AddFieldMapping(inRas2pnt, fieldList, mergeRuleList, mergeFieldList):
fieldMappings = arcpy.FieldMappings()
fieldMapList = []
# create fieldMap with all field
for i in range(len(fieldList)):
fieldMap = arcpy.FieldMap()
fieldMapList.append(fieldMap)
# add all fieldMap to fieldMappings
for i, each in enumerate(fieldList):
fieldMapList[i].addInputField(inRas2pnt, each)
fieldMapList[i].addInputField(inRas2pnt, mergeFieldList[i])
fieldMapList[i].mergeRule = mergeRuleList[i]
fieldMappings.addFieldMap(fieldMapList[i])
return fieldMappings
ArcGIS 帮助
FieldMap
语法及属性
方法
示例
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)
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)
FieldMappings
语法及属性
方法
示例
代码示例 FieldMappings 示例 需要将相似数据集合并到一个无所不包的数据集中时,通常使用 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)
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)