使用gp工具追加数据
源文件为shape文件,目标文件为mdb,数据集中有部分字段可以匹配,字段名称不一致部分不做对应。
/// <summary>
/// 追加数据
/// </summary>
/// <param name="inputPath">源数据对象</param>
/// <param name="targetPath">目标数据对象</param>
public static void GPAppend(AppendFeature inputPath, IFeatureClass targetPath)
{
Geoprocessor mGeoprocessor = new Geoprocessor();
mGeoprocessor.OverwriteOutput = true;
Append mAppend = new Append();
mAppend.inputs = inputPath.FeatureClass;
mAppend.target = targetPath;
mAppend.schema_type = "NO_TEST";
//mAppend.field_mapping = GetFieldMapping(inputPath.FileFullPath);
string mMessageGP = "";
mGeoprocessor.Execute(mAppend, null);
for (int pMessageCount = 0; pMessageCount < mGeoprocessor.MessageCount; pMessageCount++)
{
mMessageGP += mGeoprocessor.GetMessage(pMessageCount);
}
}
schema_type 在NO_Text情况下,追加后发现即使字段名相同也不能使属性值导入。通过比较ArcMap下Append工具的应用发现,选择NO_TEXT选项后,在Field_Mapping中自动出现对象关系。查找ArcEngine帮助文档发现此选项接收类型为IGPFiledMapping集合,集合为源对象字段与目标对象字段的对应关系,经分析如果字段名称一样的情况下,只要匹配源对象字段就可以解决属性值不能导入的问题。解决属性字段不能导入问题
public static IGPFieldMapping GetFieldMapping(string ExInputPath)
{
IGPUtilities mGPUtilities = new GPUtilitiesClass();
IDETable mDETable = (IDETable)mGPUtilities.MakeDataElement(ExInputPath, null, null);
IArray mArray = new ArrayClass();
mArray.Add(mDETable);
IGPFieldMapping mGPFieldMapping = new GPFieldMappingClass();
mGPFieldMapping.Initialize(mArray, null);
//create new fieldmap
IGPFieldMap mGPFieldMap = new GPFieldMapClass();
//mGPFieldMap.OutputField = mFC1.Fields.get_Field(1);
//match field
int fieldmap_index = mGPFieldMapping.FindFieldMap("BSM");
IGPFieldMap mGPFNew = mGPFieldMapping.GetFieldMap(fieldmap_index);
int field_index = mGPFNew.FindInputField(mDETable, "BSM");
IField mField = mGPFNew.GetField(field_index);
mGPFieldMap.AddInputField(mDETable, mField, -1, -1);
mGPFieldMapping.AddFieldMap(mGPFieldMap);
return mGPFieldMapping;
}
其中GPUtilities类获取表集合IDETable,IDETable猜测为源对象表,没有经过严格测试,只是IDETable作为源对象表可以解决产生的问题。IGPFieldMapping类是对应关系集合。IGPFieldMap对应关系,需要匹配输入字段与输出字段,匹配后添加到IGPFieldMapping。