第一种:使用IFeatureDataConverter.ConvertFeatureClass方法
/// <summary> ///使用IFeatureDataConverter.ConvertFeatureClass方法 /// </summary> /// <param name="pFeatureClass"></param> /// <param name="pQueryFilter"></param> /// <param name="outWorkspace"></param> /// <returns></returns> public int ImportFeatureClassToNewWorkSpace(IFeatureClass pFeatureClass, IQueryFilter pQueryFilter, IWorkspace outWorkspace, string outName) { try { //输入的要素类空间 IDataset inDataSet = pFeatureClass as IDataset; IFeatureClassName inFCName = inDataSet.FullName as IFeatureClassName; IWorkspace inWorkspace = inDataSet.Workspace; //输出的shapfile文件的工作空间 IDataset outDataSet = outWorkspace as IDataset; IWorkspaceName outWorkspaceName = outDataSet.FullName as IWorkspaceName; IFeatureClassName outFCName = new FeatureClassNameClass(); IDatasetName dataSetName = outFCName as IDatasetName; dataSetName.WorkspaceName = outWorkspaceName; //dataSetName.Name = pFeatureClass.AliasName.ToString(); dataSetName.Name = outName; //检查字段的有效性 IFieldChecker fieldChecker = new FieldCheckerClass(); fieldChecker.InputWorkspace = inWorkspace; fieldChecker.ValidateWorkspace = outWorkspace; IFields fields = pFeatureClass.Fields; IFields outFields = null; IEnumFieldError enumFieldError = null; fieldChecker.Validate(fields, out enumFieldError, out outFields); //调用IFeatureDataConverter接口进行数据转换 IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureClass(inFCName, pQueryFilter, null, outFCName, null, outFields, "", 1000, 0); } catch { return 0; } return 1; }
调用:
//将shp数据转换成GDB string fileName = GetVectorFileName(subDir[0]); string folder = subDir[0]; IFeatureClass shpFeatureClass = m_GisHelp.GetFeatureClass(folder, fileName); IWorkspace outWorkspace=m_GisHelp.Get_Workspace(gdbPath); int success = m_GisHelp.ImportFeatureClassToNewWorkSpace(shpFeatureClass, null, outWorkspace, "parcel");
第二种:通过GP工具,FeatureClassToGeodatabase
public void FeatureClassToGDB(string shpRoot, string outGDBPath) { string shapes = GetShapes(shpRoot); ESRI.ArcGIS.ConversionTools.FeatureClassToGeodatabase featureClassToGeodatabase = new ESRI.ArcGIS.ConversionTools.FeatureClassToGeodatabase(); featureClassToGeodatabase.Input_Features = shapes; featureClassToGeodatabase.Output_Geodatabase = outGDBPath; featureClassToGeodatabase.Derived_Geodatabase = ""; Geoprocessor geoprocessor = new Geoprocessor(); geoprocessor.OverwriteOutput = true; try { geoprocessor.Execute(featureClassToGeodatabase, null); } catch (Exception ex) { //string mess = null; //for (int k = 0; k < geoprocessor.MessageCount; k++) //{ // mess += geoprocessor.GetMessage(k) + " "; //} throw ex; } }