首页 > 代码库 > FeatureClass的"import"(转换)功能

FeatureClass的"import"(转换)功能

 1 /// <summary> 2         /// FeatureClass的"import"功能. 3         /// </summary> 4         /// <param name="srcWorkspaceFactory">源IWorkspaceFactory2对象</param> 5         /// <param name="srcWorkspacePath">源工作空间的路径,如:"G:\doc\gis\1.400\data\pdb.mdb"</param> 6         /// <param name="srcFeatureName">源要素名,如:"hyd1_4l_1"</param> 7         /// <param name="desWorkspaceFactory">目标IWorkspaceFactory2对象</param> 8         /// <param name="desWorkspacePath">目标工作空间的路径,如:"G:\doc\gis\1.400\data\desPdb.mdb"(必须已存在)</param> 9         /// <param name="desFeatureName">目标要素名,如:"desF"</param>10         public static void ConvertFeatureClass(ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 srcWorkspaceFactory, string srcWorkspacePath, string srcFeatureName, ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 desWorkspaceFactory, string desWorkspacePath, string desFeatureName) {11             //工作空间.12             ESRI.ArcGIS.Geodatabase.IWorkspace srcWs = srcWorkspaceFactory.OpenFromFile(srcWorkspacePath, 0);13             ESRI.ArcGIS.Geodatabase.IWorkspace desWs = desWorkspaceFactory.OpenFromFile(desWorkspacePath, 0);   //必须已存在,才能open.14 15             //源.16             ESRI.ArcGIS.Geodatabase.IFeatureWorkspace srcFws = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)srcWs;17             ESRI.ArcGIS.Geodatabase.IFeatureClass srcFc = srcFws.OpenFeatureClass(srcFeatureName);18             ESRI.ArcGIS.Geodatabase.IDataset srcDs = (ESRI.ArcGIS.Geodatabase.IDataset)srcFc;19             ESRI.ArcGIS.Geodatabase.IFeatureClassName srcFcName = (ESRI.ArcGIS.Geodatabase.IFeatureClassName)srcDs.FullName;20 21             //目标.22             ESRI.ArcGIS.Geodatabase.IDataset desDs = (ESRI.ArcGIS.Geodatabase.IDataset)desWs;23             ESRI.ArcGIS.esriSystem.IName desName = (ESRI.ArcGIS.esriSystem.IName)desDs.FullName;24             ESRI.ArcGIS.Geodatabase.IWorkspaceName2 desWsName = (ESRI.ArcGIS.Geodatabase.IWorkspaceName2)desName;25             ESRI.ArcGIS.Geodatabase.IFeatureClassName desFcName = new ESRI.ArcGIS.Geodatabase.FeatureClassNameClass();26             ESRI.ArcGIS.Geodatabase.IDatasetName2 desDsName = (ESRI.ArcGIS.Geodatabase.IDatasetName2)desFcName;27             desDsName.Name = desFeatureName;28             desDsName.WorkspaceName = desWsName;29 30             //字段检查.31             ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();32             ESRI.ArcGIS.Geodatabase.IFields srcFields = srcFc.Fields;33             ESRI.ArcGIS.Geodatabase.IFields desFields = null;34             ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldErr = null;35             fieldChecker.InputWorkspace = srcWs;36             fieldChecker.ValidateWorkspace = desWs;37             fieldChecker.Validate(srcFields, out enumFieldErr, out desFields);38             if (enumFieldErr != null)39                 System.Windows.Forms.MessageBox.Show("Errors were encountered during field validation");40 41             string shpFieldName = srcFc.ShapeFieldName;42             int fieldIndex = srcFc.FindField(shpFieldName);43             ESRI.ArcGIS.Geodatabase.IField shpField = srcFields.get_Field(fieldIndex);44             ESRI.ArcGIS.Geodatabase.IGeometryDef DesGeoDef = shpField.GeometryDef;45 46             //转换.47             ESRI.ArcGIS.Geodatabase.IFeatureDataConverter fdConverter = new ESRI.ArcGIS.Geodatabase.FeatureDataConverterClass();48             ESRI.ArcGIS.Geodatabase.IEnumInvalidObject enuInvalidObj = fdConverter.ConvertFeatureClass(srcFcName, null, null, desFcName, DesGeoDef, desFields, "", 1000, 0);49             enuInvalidObj.Reset();50             ESRI.ArcGIS.Geodatabase.IInvalidObjectInfo inValidObjInfo = null;51             while ((inValidObjInfo = enuInvalidObj.Next()) != null)52                 System.Windows.Forms.MessageBox.Show(string.Format("Errors occurred for the following feature: {0}", inValidObjInfo.InvalidObjectID));53         }

在主函数中调用:

1 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 srcWsf = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();2 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 desWsf = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();3 4 string srcWsPath = @"G:\doc\gis\1.400\data\pdb.mdb";5 string desWsPath = @"G:\doc\gis\1.400\data\desPdb.mdb";6 7 string srcFcName = "hyd1_4l_1";8 string desFcName = "desF";9 Engine.App_Code.Feature_Assist.ConvertFeatureClass(srcWsf, srcWsPath, srcFcName, desWsf, desWsPath, desFcName);

 

FeatureClass的"import"(转换)功能