第一種:使用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; }
調用:spa
//將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工具,FeatureClassToGeodatabasecode
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) + "\n"; //} throw ex; } }