ArcGIS Engine数据导入导出


打开类

  1. public class clsOpenClass  
  2.    {  
  3.        public static void OpenFeatureClass(AxMapControl MapControl,  
  4.            IFeatureClassName pFcName, ListView listview1)  
  5.        {  
  6.            try  
  7.            {  
  8.                MapControl.Map.ClearLayers();  
  9.                MapControl.SpatialReference = null;  
  10.                IName pName = pFcName as IName;  
  11.                IFeatureClass pFc = pName.Open() as IFeatureClass;  
  12.   
  13.                listview1.Items.Clear();  
  14.                listview1.Columns.Clear();  
  15.                LoadListView(pFc, listview1);  
  16.   
  17.                IFeatureCursor pCursor = pFc.Search(nullfalse);  
  18.                IFeature pfea = pCursor.NextFeature();  
  19.                int j = 0;  
  20.                while (pfea != null)  
  21.                {  
  22.                    ListViewItem lv = new ListViewItem();  
  23.   
  24.                    for (int i = 0; i < pfea.Fields.FieldCount; i++)  
  25.                    {  
  26.                        string sFieldName = pfea.Fields.get_Field(i).Name;  
  27.                        lv.SubItems.Add(FeatureHelper.GetFeatureValue(pfea, sFieldName).ToString());  
  28.                    }  
  29.   
  30.                    lv.Tag = pfea;  
  31.                    if (j % 2 == 0)  
  32.                    {  
  33.                        lv.BackColor = System.Drawing.Color.GreenYellow;  
  34.                    }  
  35.                    listview1.Items.Add(lv);  
  36.                    pfea = pCursor.NextFeature();  
  37.                    j++;  
  38.                }  
  39.                LSGISHelper.OtherHelper.ReleaseObject(pCursor);  
  40.                //最后加载图形数据  
  41.                 
  42.                  
  43.                    if (pFcName.FeatureType == esriFeatureType.esriFTRasterCatalogItem)  
  44.                    {  
  45.                        ESRI.ArcGIS.Carto.IGdbRasterCatalogLayer pGdbRCLayer = new ESRI.ArcGIS.Carto.GdbRasterCatalogLayerClass();  
  46.                        pGdbRCLayer.Setup(pFc as ITable);  
  47.                        MapControl.Map.AddLayer(pGdbRCLayer as ILayer);  
  48.                    }  
  49.                    else if( (pFcName.FeatureType == esriFeatureType.esriFTSimple)||  
  50.                         (pFcName.FeatureType == esriFeatureType.esriFTComplexEdge)||  
  51.                        (pFcName.FeatureType == esriFeatureType.esriFTComplexJunction)||  
  52.                        (pFcName.FeatureType == esriFeatureType.esriFTSimpleEdge)||  
  53.                         (pFcName.FeatureType == esriFeatureType.esriFTSimpleJunction))  
  54.                    {  
  55.                        
  56.                        IFeatureLayer pLayer = new FeatureLayerClass();  
  57.                        pLayer.FeatureClass = pFc;  
  58.                        pLayer.Name = (pFc as IDataset).Name;  
  59.                        MapControl.Map.AddLayer(pLayer as ILayer);  
  60.                    }  
  61.                    else if (pFcName.FeatureType == esriFeatureType.esriFTAnnotation)  
  62.                    {  
  63.                        ILayer pLayer = OpenAnnotationLayer(pFc);  
  64.                        pLayer.Name = (pFc as IDataset).Name;  
  65.                        MapControl.Map.AddLayer(pLayer as ILayer);  
  66.                    }  
  67.                     
  68.                MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, nullnull);  
  69.            }  
  70.            catch (Exception ex)  
  71.            { }  
  72.        }  
  73.        public static void OpenRasterDataset(AxMapControl MapControl,  
  74.            IRasterDatasetName pRdName, ListView listview1)  
  75.        {  
  76.            MapControl.ClearLayers();  
  77.            MapControl.SpatialReference = null;  
  78.            listview1.Items.Clear();  
  79.            listview1.Columns.Clear();  
  80.            IDatasetName pDsName = pRdName as IDatasetName;  
  81.            string sName = pDsName.Name;  
  82.             
  83.            IName pName = pRdName as IName;  
  84.   
  85.            IRasterDataset pRds = pName.Open() as IRasterDataset;  
  86.            IRasterLayer pRL = new RasterLayerClass();  
  87.            pRL.CreateFromDataset(pRds);  
  88.            pRL.Name = sName;  
  89.            MapControl.AddLayer(pRL as ILayer);  
  90.            MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, nullnull);  
  91.   
  92.        }  
  93.   
  94.        public static void OpenMosaicDataset(AxMapControl MapControl,  
  95.           IMosaicDatasetName pMdName, ListView listview1)  
  96.        {  
  97.            MapControl.ClearLayers();  
  98.            MapControl.SpatialReference = null;  
  99.            listview1.Items.Clear();  
  100.            listview1.Columns.Clear();  
  101.            IDatasetName pDsName = pMdName as IDatasetName;  
  102.            string sName = pDsName.Name;  
  103.   
  104.            IName pName = pMdName as IName;  
  105.   
  106.            IMosaicDataset pMds = pName.Open() as IMosaicDataset;  
  107.            IFeatureClass pFc = pMds.Catalog;  
  108.            listview1.Items.Clear();  
  109.            listview1.Columns.Clear();  
  110.            LoadListView(pFc, listview1);  
  111.   
  112.            IFeatureCursor pCursor = pFc.Search(nullfalse);  
  113.            IFeature pfea = pCursor.NextFeature();  
  114.            int j = 0;  
  115.            while (pfea != null)  
  116.            {  
  117.                ListViewItem lv = new ListViewItem();  
  118.   
  119.                for (int i = 0; i < pfea.Fields.FieldCount; i++)  
  120.                {  
  121.                    string sFieldName = pfea.Fields.get_Field(i).Name;  
  122.                    lv.SubItems.Add(FeatureHelper.GetFeatureValue(pfea, sFieldName).ToString());  
  123.                }  
  124.   
  125.                lv.Tag = pfea;  
  126.                if (j % 2 == 0)  
  127.                {  
  128.                    lv.BackColor = System.Drawing.Color.GreenYellow;  
  129.                }  
  130.                listview1.Items.Add(lv);  
  131.                pfea = pCursor.NextFeature();  
  132.                j++;  
  133.            }  
  134.            LSGISHelper.OtherHelper.ReleaseObject(pCursor);  
  135.            IMosaicLayer pML = new MosaicLayerClass();  
  136.            pML.CreateFromMosaicDataset(pMds);  
  137.             
  138.            MapControl.AddLayer(pML as ILayer);  
  139.            MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, nullnull);  
  140.   
  141.        }  
  142.   
  143.        public static void OpenTable(AxMapControl MapControl,   
  144.            ITableName pTName, ListView listview1)  
  145.        {  
  146.            try  
  147.            {  
  148.                MapControl.Map.ClearLayers();  
  149.                MapControl.SpatialReference = null;  
  150.                MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewAll, nullnull);  
  151.                IName pName = pTName as IName;  
  152.                ITable pFc = pName.Open() as ITable;  
  153.   
  154.                listview1.Items.Clear();  
  155.                listview1.Columns.Clear();  
  156.                LoadListView(pFc, listview1);  
  157.   
  158.                ICursor pCursor = pFc.Search(nullfalse);  
  159.                IRow pfea = pCursor.NextRow();  
  160.                int j = 0;  
  161.                while (pfea != null)  
  162.                {  
  163.                    ListViewItem lv = new ListViewItem();  
  164.   
  165.                    for (int i = 0; i < pfea.Fields.FieldCount; i++)  
  166.                    {  
  167.                        string sFieldName = pfea.Fields.get_Field(i).Name;  
  168.                        lv.SubItems.Add(FeatureHelper.GetRowValue(pfea, sFieldName).ToString());  
  169.                    }  
  170.   
  171.                    lv.Tag = pfea;  
  172.                    if (j % 2 == 0)  
  173.                    {  
  174.                        lv.BackColor = System.Drawing.Color.GreenYellow;  
  175.                    }  
  176.                    listview1.Items.Add(lv);  
  177.                    pfea = pCursor.NextRow();  
  178.                    j++;  
  179.                }  
  180.                LSGISHelper.OtherHelper.ReleaseObject(pCursor);  
  181.            }  
  182.            catch { }  
  183.        }  
  184.        public static void LoadListView(IFeatureClass pFC, ListView listView1)  
  185.        {  
  186.            try  
  187.            {  
  188.                 
  189.                listView1.Columns.Clear();  
  190.                //添加一个空  
  191.                ColumnHeader columnHeader = new ColumnHeader();  
  192.   
  193.                listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {  
  194.                columnHeader  
  195.                });  
  196.                columnHeader.Text = "";  
  197.   
  198.                for (int i = 0; i < pFC.Fields.FieldCount; i++)  
  199.                {  
  200.                    ColumnHeader columnHeader1 = new ColumnHeader();  
  201.   
  202.                    listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {  
  203.                columnHeader1  
  204.                });  
  205.                    IFields pFields = pFC.Fields;  
  206.   
  207.                    IField pField = pFields.get_Field(i);  
  208.   
  209.   
  210.                    columnHeader1.Text = pField.AliasName;  
  211.   
  212.   
  213.                }  
  214.   
  215.            }  
  216.            catch (Exception ex)  
  217.            { }  
  218.        }  
  219.        public static void LoadListView(ITable pFC, ListView listView1)  
  220.        {  
  221.            try  
  222.            {  
  223.   
  224.                listView1.Columns.Clear();  
  225.                //添加一个空  
  226.                ColumnHeader columnHeader = new ColumnHeader();  
  227.   
  228.                listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {  
  229.                columnHeader  
  230.                });  
  231.                columnHeader.Text = "";  
  232.   
  233.                for (int i = 0; i < pFC.Fields.FieldCount; i++)  
  234.                {  
  235.                    ColumnHeader columnHeader1 = new ColumnHeader();  
  236.   
  237.                    listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {  
  238.                columnHeader1  
  239.                });  
  240.                    IFields pFields = pFC.Fields;  
  241.   
  242.                    IField pField = pFields.get_Field(i);  
  243.   
  244.   
  245.                    columnHeader1.Text = pField.AliasName;  
  246.   
  247.   
  248.                }  
  249.   
  250.            }  
  251.            catch (Exception ex)  
  252.            { }  
  253.        }  
  254.   
  255.        public static ILayer  OpenAnnotationLayer(IFeatureClass pfc)  
  256.        {  
  257.            IFDOGraphicsLayerFactory pfdof = new FDOGraphicsLayerFactoryClass();  
  258.           IFeatureDataset pFDS=pfc.FeatureDataset;  
  259.            IWorkspace pWS = pFDS.Workspace;  
  260.            IFeatureWorkspace pFWS=pWS as IFeatureWorkspace;  
  261.            ILayer pLayer = pfdof.OpenGraphicsLayer(pFWS, pFDS, (pfc as IDataset).Name );  
  262.            return pLayer;  
  263.        }  
  264.           
  265.    }  

创建类

  1. public  class clsCreateClass  
  2.     {  
  3.         public IFeatureDataset CreateDataset(IWorkspace pWorkspace)  
  4.         {  
  5.             try  
  6.             {  
  7.                 if (pWorkspace == nullreturn null;  
  8.                 IFeatureWorkspace aFeaWorkspace = pWorkspace as IFeatureWorkspace;  
  9.                 if (aFeaWorkspace == nullreturn null;  
  10.                 DatasetPropertiesForm aForm = new DatasetPropertiesForm();  
  11.                 aForm.HignPrecision = LSGISHelper.WorkspaceHelper.HighPrecision(pWorkspace);  
  12.                 if (aForm.ShowDialog() == DialogResult.OK)  
  13.                 {  
  14.                     string dsName = aForm.FeatureDatasetName;  
  15.                     ISpatialReference aSR = aForm.SpatialReference;  
  16.                     IFeatureDataset aDS = aFeaWorkspace.CreateFeatureDataset(dsName, aSR);  
  17.                     return aDS;  
  18.                 }  
  19.             }  
  20.             catch (Exception ex) { }  
  21.             return null;  
  22.         }  
  23.         public IRasterDataset CreateRasterDataset(IWorkspace pWorkspace,string sName  
  24.            )  
  25.         {  
  26.             try  
  27.             {  
  28.                 IRasterWorkspaceEx pRWEx = pWorkspace as IRasterWorkspaceEx;  
  29.                 IGeometryDef pGDef=new GeometryDefClass ();  
  30.                  
  31.                 IRasterDataset pRD = pRWEx.CreateRasterDataset(  
  32.                     sName, 3, rstPixelType.PT_CHAR, nullnullnullnull);  
  33.             }  
  34.             catch { }  
  35.             return null;  
  36.         }  
  37.         public IFeatureClass CreateFeatureClass(IWorkspace pWorkspace)  
  38.         {  
  39.             if (pWorkspace == nullreturn null;  
  40.             IFeatureWorkspace aFeaWorkspace = pWorkspace as IFeatureWorkspace;  
  41.             if (aFeaWorkspace == nullreturn null;  
  42.             IFeatureClass aClass = null;  
  43.             FeatureClassWizard aForm = new FeatureClassWizard();  
  44.             aForm.Workspace = pWorkspace;  
  45.             if (aForm.ShowDialog() == DialogResult.OK)  
  46.             {  
  47.                 while (true)  
  48.                 {  
  49.                     string className = aForm.FeatureClassName;  
  50.                     string aliasName = aForm.FeatureClassAliasName;  
  51.                     IFields flds = aForm.Fields;  
  52.                     try  
  53.                     {  
  54.                         aClass = aFeaWorkspace.CreateFeatureClass(className, flds  
  55.                             , nullnull, esriFeatureType.esriFTSimple, "SHAPE"null);  
  56.                         if (!aliasName.Equals(""))  
  57.                         {  
  58.                             IClassSchemaEdit aClassEdit = aClass as IClassSchemaEdit;  
  59.                             if (aClassEdit != null) aClassEdit.AlterAliasName(aliasName);  
  60.                         }  
  61.                         break;  
  62.                     }  
  63.                     catch (Exception ex)  
  64.                     {  
  65.                         //MessageBox.Show ("错误:\n"+ex.Message ,"新建特性类",  
  66.                         //    MessageBoxButtons.OK ,MessageBoxIcon.Error );  
  67.                         LSCommonHelper.MessageBoxHelper.ShowErrorMessageBox(ex, "");  
  68.                     }  
  69.                     aForm = new FeatureClassWizard();  
  70.                     aForm.Workspace = pWorkspace;  
  71.                     aForm.FeatureClassName = className;  
  72.                     aForm.FeatureClassAliasName = aliasName;  
  73.                     aForm.Fields = flds;  
  74.                     if (aForm.ShowDialog() == DialogResult.Cancel) break;  
  75.                 }  
  76.             }  
  77.             return aClass;  
  78.         }  
  79.         public IFeatureClass CreateFeatureClass(IFeatureDataset pDS)  
  80.         {  
  81.             if (pDS == nullreturn null;  
  82.             IFeatureClass aClass = null;  
  83.   
  84.             FeatureClassWizard aForm = new FeatureClassWizard();  
  85.             aForm.Workspace = (pDS as IDataset).Workspace;  
  86.             IGeoDataset pGDS = pDS as IGeoDataset;  
  87.             if (pGDS != null)  
  88.             {  
  89.                 aForm.SpatialReference = pGDS.SpatialReference;  
  90.             }  
  91.             if (aForm.ShowDialog() == DialogResult.OK)  
  92.             {  
  93.                 while (true)  
  94.                 {  
  95.                     string className = aForm.FeatureClassName;  
  96.                     string aliasName = aForm.FeatureClassAliasName;  
  97.                     IFields flds = aForm.Fields;  
  98.   
  99.                     try  
  100.                     {  
  101.                         aClass = pDS.CreateFeatureClass(className, flds  
  102.                             , nullnull, esriFeatureType.esriFTSimple, "SHAPE"null);  
  103.                         if (!aliasName.Equals(""))  
  104.                         {  
  105.                             IClassSchemaEdit aClassEdit = aClass as IClassSchemaEdit;  
  106.                             if (aClassEdit != null) aClassEdit.AlterAliasName(aliasName);  
  107.                         }  
  108.                         break;  
  109.                     }  
  110.                     catch (Exception ex)  
  111.                     {  
  112.                         LSCommonHelper.MessageBoxHelper.ShowErrorMessageBox(ex, "请选择高精度坐标系");  
  113.                     }  
  114.                     aForm = new FeatureClassWizard();  
  115.                     aForm.Workspace = (pDS as IDataset).Workspace;  
  116.                     aForm.FeatureClassName = className;  
  117.                     aForm.FeatureClassAliasName = aliasName;  
  118.                     
  119.   
  120.                     aForm.Fields = flds;  
  121.                     if (aForm.ShowDialog() == DialogResult.Cancel) break;  
  122.                 }  
  123.             }  
  124.             return aClass;  
  125.         }  
  126.         public ITable CreateTable(IWorkspace pWorkspace)  
  127.         {  
  128.             if (pWorkspace == nullreturn null;  
  129.             IFeatureWorkspace aFeaWorkspace = pWorkspace as IFeatureWorkspace;  
  130.             if (aFeaWorkspace == nullreturn null;  
  131.             ITable aTable = null;  
  132.             DataTableWizard aWizard = new DataTableWizard();  
  133.             aWizard.Workspace = pWorkspace;  
  134.             if (aWizard.ShowDialog() == DialogResult.OK)  
  135.             {  
  136.                 while (true)  
  137.                 {  
  138.                     string tableName = aWizard.TableName;  
  139.                     string aliasName = aWizard.TableAliasName;  
  140.                     IFields flds = aWizard.Fields;  
  141.                     try  
  142.                     {  
  143.                         aTable = aFeaWorkspace.CreateTable(tableName, flds  
  144.                             , nullnullnull);  
  145.   
  146.                         if (!aliasName.Equals(""))  
  147.                         {  
  148.                             IClassSchemaEdit aClassEdit = aTable as IClassSchemaEdit;  
  149.                             aClassEdit.RegisterAsObjectClass("OBJECTID"null);  
  150.                             if (aClassEdit != null) aClassEdit.AlterAliasName(aliasName);  
  151.                         }  
  152.                         break;  
  153.                     }  
  154.                     catch (Exception ex)  
  155.                     {  
  156.                         //MessageBox.Show ("错误:\n"+ex.Message ,"新建表",  
  157.                         //    MessageBoxButtons.OK ,MessageBoxIcon.Error );  
  158.                         LSCommonHelper.MessageBoxHelper.ShowErrorMessageBox(ex, "");  
  159.                     }  
  160.                     aWizard = new DataTableWizard();  
  161.                     aWizard.Workspace = pWorkspace;  
  162.                     aWizard.TableName = tableName;  
  163.                     aWizard.TableAliasName = aliasName;  
  164.                     aWizard.Fields = flds;  
  165.                     if (aWizard.ShowDialog() == DialogResult.Cancel) break;  
  166.                 }  
  167.             }  
  168.             return aTable;  
  169.         }  
  170.     }  

导出类

  1. public class clsExportClass  
  2.     {  
  3.      
  4.         /// <summary>  
  5.         /// 导出FeatureClass到Shapefile文件  
  6.         /// </summary>  
  7.         /// <param name="apFeatureClass"></param>  
  8.         public static bool ExportFeatureClassToShp(string sPath,IFeatureClass apFeatureClass)  
  9.         {  
  10.             try  
  11.             {  
  12.                 string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(sPath);  
  13.                 if (ExportFileShortName == "")  
  14.                 {  
  15.                     ExportFileShortName =LSCommonHelper.OtherHelper.GetRightName( (apFeatureClass as IDataset).Name,".");  
  16.                 }  
  17.                 string ExportFilePath = System.IO.Path.GetDirectoryName(sPath);  
  18.                 if (ExportFilePath == null)  
  19.                 {  
  20.                     ExportFilePath = sPath;  
  21.                 }  
  22.                 //设置导出要素类的参数  
  23.                 IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();  
  24.                 IDataset pOutDataset = (IDataset)apFeatureClass;  
  25.                 pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;  
  26.                 //创建一个输出shp文件的工作空间  
  27.                 IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();  
  28.                 IWorkspaceName pInWorkspaceName = new WorkspaceNameClass();  
  29.                 pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0);  
  30.   
  31.                 //创建一个要素集合  
  32.                 IFeatureDatasetName pInFeatureDatasetName = null;  
  33.                 //创建一个要素类  
  34.                 IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();  
  35.                 IDatasetName pInDatasetClassName;  
  36.                 pInDatasetClassName = (IDatasetName)pInFeatureClassName;  
  37.                 pInDatasetClassName.Name = ExportFileShortName;//作为输出参数  
  38.                 pInDatasetClassName.WorkspaceName = pInWorkspaceName;  
  39.                 //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合  
  40.                 long iCounter;  
  41.                 IFields pOutFields, pInFields;  
  42.         
  43.                 IField pGeoField;  
  44.                 IEnumFieldError pEnumFieldError = null;  
  45.                 pInFields = apFeatureClass.Fields;  
  46.                 IFieldChecker pFieldChecker = new FieldChecker();  
  47.                 pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);  
  48.                 //通过循环查找几何字段  
  49.                 pGeoField = null;  
  50.                 for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)  
  51.                 {  
  52.                     if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)  
  53.                     {  
  54.                         pGeoField = pOutFields.get_Field((int)iCounter);  
  55.                         break;  
  56.                     }  
  57.                 }  
  58.                 //得到几何字段的几何定义  
  59.                 IGeometryDef pOutGeometryDef;  
  60.                 IGeometryDefEdit pOutGeometryDefEdit;  
  61.                 pOutGeometryDef = pGeoField.GeometryDef;  
  62.                 //设置几何字段的空间参考和网格  
  63.                 pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef;  
  64.                 pOutGeometryDefEdit.GridCount_2 = 1;  
  65.                 pOutGeometryDefEdit.set_GridSize(0, 1500000);  
  66.   
  67.                 //开始导入  
  68.                 IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();  
  69.                 pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);  
  70.                 return true;  
  71.             }  
  72.             catch (Exception ex)  
  73.             {  
  74.              return false;  
  75.             }  
  76.         }  
  77.         public static void ExportFeatureClass2Shapefile(IFeatureClassName pFcName)  
  78.         {  
  79.               
  80.             IName pName = pFcName as IName;  
  81.             IFeatureClass pFc = pName.Open() as IFeatureClass;  
  82.   
  83.             SaveFileDialog ofd = new SaveFileDialog();  
  84.             ofd.Filter = "Shapefile文件(.shp)|*.shp";  
  85.             if (ofd.ShowDialog() == DialogResult.OK)  
  86.             {  
  87.                 string sPath = ofd.FileName;  
  88.                 if (ExportFeatureClassToShp(sPath, pFc))  
  89.                 {  
  90.                     LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出成功");  
  91.                 }  
  92.                 LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出失败");  
  93.             }  
  94.         }  
  95.   
  96.         public static bool  ConvertFeatureDataset(IWorkspace sourceWorkspace, IWorkspace targetWorkspace,  
  97.      string nameOfSourceFeatureDataset, string nameOfTargetFeatureDataset)  
  98.         {  
  99.             try  
  100.             {  
  101.                 //create source workspace name    
  102.                 IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;  
  103.                 IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;  
  104.                 //create source dataset name     
  105.                 IFeatureDatasetName sourceFeatureDatasetName = new FeatureDatasetNameClass();  
  106.                 IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureDatasetName;  
  107.                 sourceDatasetName.WorkspaceName = sourceWorkspaceName;  
  108.                 sourceDatasetName.Name = nameOfSourceFeatureDataset;  
  109.                 //create target workspace name     
  110.                 IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;  
  111.                 IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;  
  112.                 //create target dataset name    
  113.                 IFeatureDatasetName targetFeatureDatasetName = new FeatureDatasetNameClass();  
  114.                 IDatasetName targetDatasetName = (IDatasetName)targetFeatureDatasetName;  
  115.                 targetDatasetName.WorkspaceName = targetWorkspaceName;  
  116.                 targetDatasetName.Name = nameOfTargetFeatureDataset;  
  117.                 //Convert feature dataset       
  118.                 IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();  
  119.                 featureDataConverter.ConvertFeatureDataset(sourceFeatureDatasetName, targetFeatureDatasetName, null"", 1000, 0);  
  120.                 return true;  
  121.             }  
  122.             catch (Exception ex)  
  123.             { return false; }  
  124.         }  
  125.   
  126.         public static void ExportFeatureDataset2GDB(IDatasetName pDSName,int flag)  
  127.         {  
  128.             FolderBrowserDialog fbd = new FolderBrowserDialog();  
  129.             fbd.Description = "选择保存路径";  
  130.             if (fbd.ShowDialog() == DialogResult.OK)  
  131.             {  
  132.                 string sPath = fbd.SelectedPath;  
  133.                 string sTemplate = "";  
  134.                 if (flag == 0)  
  135.                 {  
  136.                     sTemplate = Application.StartupPath + @"\template\pgdb.mdb";  
  137.                     File.Copy(sTemplate, sPath + "\\pgdb.mdb");  
  138.                 }  
  139.                 else  
  140.                 {  
  141.                     sTemplate = Application.StartupPath + @"\template\fgdb.gdb";  
  142.                     FileHelper.CopyDir(sTemplate, sPath+"\\fgdb.gdb");  
  143.                 }  
  144.                  
  145.                 IName pName = pDSName as IName;  
  146.                 string sSrcDSName = pDSName.Name;  
  147.                 sSrcDSName = LSCommonHelper.OtherHelper.GetRightName(sSrcDSName, ".");  
  148.                 IDataset pDS = pName.Open() as IDataset;  
  149.                 IWorkspace pSrcWS = pDS.Workspace;  
  150.                 IWorkspace pDesWS = null;  
  151.                 if (flag == 0)  
  152.                 {  
  153.                     pDesWS = LSGISHelper.WorkspaceHelper.GetAccessWorkspace(sPath + "\\pgdb.mdb");  
  154.                 }  
  155.                 else  
  156.                 {  
  157.                     pDesWS = LSGISHelper.WorkspaceHelper.GetFGDBWorkspace(sPath + "\\fgdb.gdb");  
  158.                 }  
  159.                 if (ConvertFeatureDataset(pSrcWS, pDesWS, sSrcDSName, sSrcDSName))  
  160.                 {  
  161.                     LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出成功");  
  162.                 }  
  163.                 else  
  164.                 {  
  165.                     LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出失败");  
  166.                 }  
  167.             }  
  168.   
  169.         }  
  170.   
  171.         public static void ExportFeatureDataset2Shapefile(IDatasetName pDSName,  
  172.              TaskMonitor mTaskMonitor)  
  173.         {  
  174.               
  175.                 FolderBrowserDialog fbd = new FolderBrowserDialog();  
  176.                 fbd.Description = "选择保存路径";  
  177.                 if (fbd.ShowDialog() == DialogResult.OK)  
  178.                 {  
  179.                     string sPath = fbd.SelectedPath;  
  180.                     IName pName = pDSName as IName;  
  181.                     IDataset pDS = pName.Open() as IDataset;  
  182.                     IFeatureDataset pFDS = pDS as IFeatureDataset;  
  183.                     IFeatureClassContainer pFCC = pFDS as IFeatureClassContainer;  
  184.                     IFeatureClass pfc = null;  
  185.                     mTaskMonitor.EnterWaitState();  
  186.   
  187.                     for (int i = 0; i < pFCC.ClassCount; i++)  
  188.                     {  
  189.                         pfc = pFCC.get_Class(i);  
  190.                         mTaskMonitor.TaskCaption = "正在导出第"+(i+1)+"个"+pfc.AliasName+"图层,共"+pFCC.ClassCount+"个";  
  191.                         mTaskMonitor.TaskProgress = LSCommonHelper.MathHelper.Precent(  
  192.                             0, pFCC.ClassCount, i);  
  193.                         if (ExportFeatureClassToShp(sPath, pfc))  
  194.                         { }  
  195.                     }  
  196.                     mTaskMonitor.ExitWaitState();  
  197.                     LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出完毕");  
  198.                 }  
  199.         }  
  200.      
  201.     }  

导入类

  1. public  class clsImportClass  
  2.     {  
  3.         public static void ImportGDB2SDE(IWorkspace pDesWS, int flag)  
  4.         {  
  5.             IWorkspace pSrcWS = null;  
  6.             try  
  7.             {  
  8.                 if (flag == 0)  
  9.                 {  
  10.                     OpenFileDialog ofd = new OpenFileDialog();  
  11.                     ofd.Filter = "PGDB文件(.mdb)|*.mdb";  
  12.                     ofd.Multiselect = false;  
  13.                     if (ofd.ShowDialog() == DialogResult.OK)  
  14.                     {  
  15.                         string sFileName = ofd.FileName;  
  16.                         pSrcWS = LSGISHelper.WorkspaceHelper.GetAccessWorkspace(sFileName);  
  17.                     }  
  18.                 }  
  19.                 else  
  20.                 {  
  21.                     FolderBrowserDialog fdb = new FolderBrowserDialog();  
  22.                     if (fdb.ShowDialog() == DialogResult.OK)  
  23.                     {  
  24.                         string sFileName = fdb.SelectedPath;  
  25.                         pSrcWS = LSGISHelper.WorkspaceHelper.GetFGDBWorkspace(sFileName);  
  26.                     }  
  27.                 }  
  28.                 if (pSrcWS != null)  
  29.                 {  
  30.                     IEnumDatasetName pEnumDSName = pSrcWS.get_DatasetNames(esriDatasetType.esriDTFeatureDataset);  
  31.                     IDatasetName pDName = pEnumDSName.Next();  
  32.           
  33.                     while (pDName != null)  
  34.                     {  
  35.                         clsExportClass.ConvertFeatureDataset(pSrcWS, pDesWS, pDName.Name, pDName.Name);  
  36.                  
  37.                         pDName = pEnumDSName.Next();  
  38.                     }  
  39.                     LSCommonHelper.MessageBoxHelper.ShowMessageBox("导入成功");  
  40.                 }  
  41.             }  
  42.             catch { }  
  43.         }  
  44.   
  45.         public static void ImportShapefile2SDE(IWorkspace pDesWS, TaskMonitor mTaskMonitor,  
  46.             IFeatureDatasetName pFDN)  
  47.         {  
  48.             OpenFileDialog ofd = new OpenFileDialog();  
  49.             ofd.Title = "打开SHP数据";  
  50.             ofd.Filter = "SHP数据(*.shp)|*.shp";  
  51.             ofd.Multiselect = true;  
  52.             ofd.RestoreDirectory = true;  
  53.             if (ofd.ShowDialog() == DialogResult.OK)  
  54.             {  
  55.                 string[] sFileNames = ofd.FileNames;  
  56.                 string sFileName = "";  
  57.                 IFeatureClass pFC = null;  
  58.   
  59.                 mTaskMonitor.EnterWaitState();  
  60.                 string sName = "";  
  61.                 IWorkspace pSrcWS=null;  
  62.                 for (int i = 0; i < sFileNames.Length; i++)  
  63.                 {  
  64.                     mTaskMonitor.TaskCaption = "共" + sFileNames.Length + "个文件,先处理第" + (i + 1) + "个文件";  
  65.                     mTaskMonitor.TaskProgress = LSCommonHelper.MathHelper.Precent(0, sFileNames.Length, i);  
  66.                     sFileName = sFileNames[i].ToString();  
  67.                     pSrcWS=LSGISHelper.WorkspaceHelper.GetShapefileWorkspace(sFileName);  
  68.                    sFileName = System.IO.Path.GetFileNameWithoutExtension(sFileName);  
  69.                    IFeatureWorkspace pFWS = pSrcWS as IFeatureWorkspace;  
  70.                     pFC = pFWS.OpenFeatureClass(sFileName);  
  71.                     sName = (pFC as IDataset).Name;  
  72.                     if (ConvertFeatureClass2FeatureDataset(pSrcWS, pDesWS, sName, sName, pFDN))  
  73.                     { }  
  74.                 }  
  75.                 mTaskMonitor.ExitWaitState();  
  76.                 LSCommonHelper.MessageBoxHelper.ShowMessageBox("导入成功");  
  77.             }  
  78.         }  
  79.         public static bool ConvertFeatureClass2FeatureDataset(IWorkspace sourceWorkspace,  
  80.       IWorkspace targetWorkspace, string nameOfSourceFeatureClass,  
  81.       string nameOfTargetFeatureClass, IFeatureDatasetName pName)  
  82.         {  
  83.             try  
  84.             {  
  85.                 //create source workspace name   
  86.                 IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;  
  87.                 IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;  
  88.                 //create source dataset name     
  89.                 IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();  
  90.                 IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;  
  91.                 sourceDatasetName.WorkspaceName = sourceWorkspaceName;  
  92.                 sourceDatasetName.Name = nameOfSourceFeatureClass;  
  93.   
  94.                 //create target workspace name     
  95.                 IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;  
  96.                 IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;  
  97.                 //create target dataset name      
  98.                 IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();  
  99.                 IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;  
  100.                 targetDatasetName.WorkspaceName = targetWorkspaceName;  
  101.                 targetDatasetName.Name = nameOfTargetFeatureClass;  
  102.                 //Open input Featureclass to get field definitions.    
  103.                 ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;  
  104.                 IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();  
  105.                 //Validate the field names because you are converting between different workspace types.     
  106.                 IFieldChecker fieldChecker = new FieldCheckerClass();  
  107.                 IFields targetFeatureClassFields;  
  108.                 IFields sourceFeatureClassFields = sourceFeatureClass.Fields;  
  109.                 IEnumFieldError enumFieldError;  
  110.                 // Most importantly set the input and validate workspaces!   
  111.                 fieldChecker.InputWorkspace = sourceWorkspace;  
  112.                 fieldChecker.ValidateWorkspace = targetWorkspace;  
  113.                 fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError,  
  114.                     out targetFeatureClassFields);  
  115.                 // Loop through the output fields to find the geomerty field     
  116.                 IField geometryField;  
  117.                 for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)  
  118.                 {  
  119.                     if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)  
  120.                     {  
  121.                         geometryField = targetFeatureClassFields.get_Field(i);  
  122.                         // Get the geometry field's geometry defenition            
  123.                         IGeometryDef geometryDef = geometryField.GeometryDef;  
  124.                         //Give the geometry definition a spatial index grid count and grid size       
  125.                         IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;  
  126.                         targetFCGeoDefEdit.GridCount_2 = 1;  
  127.                         targetFCGeoDefEdit.set_GridSize(0, 0);  
  128.                         //Allow ArcGIS to determine a valid grid size for the data loaded       
  129.                         targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;  
  130.                         // we want to convert all of the features      
  131.                         IQueryFilter queryFilter = new QueryFilterClass();  
  132.                         queryFilter.WhereClause = "";  
  133.                         // Load the feature class              
  134.                         IFeatureDataConverter fctofc = new FeatureDataConverterClass();  
  135.                         IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName,  
  136.                             queryFilter, pName, targetFeatureClassName,  
  137.                             geometryDef, targetFeatureClassFields, "", 1000, 0);  
  138.                         break;  
  139.                     }  
  140.                 }  
  141.                 return true;  
  142.             }  
  143.             catch (Exception ex) { return false; }  
  144.         }  
  145.     } 

你可能感兴趣的:(ArcGIS Engine数据导入导出)