shape文件导入mdb文件

构造mdb,并在其中构造要素数据集,将shape文件都导入该要素数据集中


using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.ConversionTools;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.DataSourcesFile;
namespace EngineWindowsApplication1
{
    class C_MDBBuilder
    {
        Geoprocessor GP_Tool = new Geoprocessor();//GP运行工具
        string S_Status; //当前状态
        System.Diagnostics.Stopwatch WATCH_StopWatch = new System.Diagnostics.Stopwatch();//计时器
        IGeoProcessorResult GP_Progress;//GP状态
        TimeSpan TS_TimeSpan;//时间间隔
        string S_MDBFile;//MDB路径
        string S_LDBFile;//LDB的路径
        IFeatureDataset FDS_Featuredataset;//生成的数据集
        Thread TH_TimeSpan;//线程
        List LI_AllShapePath = new List();//要导入MDB的所有Shp文件的地址
        /// 
        /// 构造MDB
        /// 
        /// 所有要导入shape的地址
        /// 要构建的MDB的地址
        /// 要构建的MDB的名称
        public C_MDBBuilder(List IN_AllShapePath, string IN_MDBPath, string IN_MDBName,string IN_DatasetName)
        {
            LI_AllShapePath = IN_AllShapePath;
            S_MDBFile = IN_MDBPath + IN_MDBName + ".mdb";//MDB路径
            S_LDBFile= IN_MDBPath + IN_MDBName + ".ldb";//LDB路径
            IWorkspaceFactory Temp_WorkFactory = new AccessWorkspaceFactory();
            if (File.Exists(S_MDBFile))//清理MDB和LDB文件
            {
                File.Delete(S_MDBFile);
                if (File.Exists(S_LDBFile))
                    File.Delete(S_LDBFile);
            }
            Temp_WorkFactory.Create(IN_MDBPath, IN_MDBName, null, 0);//创建一个MDB
            PRV_CreatFeatureDataset(IN_DatasetName, IN_AllShapePath[0]);//创建要素数据集,以第一个shp文件为空间参考
            foreach (string Each_Path in LI_AllShapePath)
            {
                PRV_AddFeatureClass(Each_Path);//将每一个shp文件添加进去
            }
        }
        public IFeatureDataset PUB_GetFeaturedataset()
        {
            return FDS_Featuredataset;
        }
        public string PUB_GetStatus()
        {
            return S_Status;
        }

        public string PUB_GetMDBFile()
        {
            return S_MDBFile;
        }

       
        //注册要素类
        private void PRV_AddFeatureClass(string IN_ShapePath)
        {
            string Temp_Direction = System.IO.Path.GetDirectoryName(IN_ShapePath);//该Shp文件的目录
            string Temp_Name = System.IO.Path.GetFileNameWithoutExtension(IN_ShapePath);//该Shp文件的名称
            IWorkspaceFactory Temp_ShapeWorkFactory = new ShapefileWorkspaceFactory();
            IFeatureWorkspace Temp_ShapeWorkspace = Temp_ShapeWorkFactory.OpenFromFile(Temp_Direction,0) as IFeatureWorkspace;
            IWorkspaceFactory Temp_AccessWorkFactory = new AccessWorkspaceFactory();
            IFeatureWorkspace Temp_Workspace = Temp_AccessWorkFactory.OpenFromFile(S_MDBFile, 0) as IFeatureWorkspace;
            IFeatureClass Temp_FeatureClass = Temp_ShapeWorkspace.OpenFeatureClass(Temp_Name);
            FeatureClassToFeatureClass Temp_FCToFC = new FeatureClassToFeatureClass(IN_ShapePath, S_MDBFile + "\\" + FDS_Featuredataset.Name, Temp_Name);//将Shp文件导入要素数据集
            GP_Progress = GP_Tool.ExecuteAsync(Temp_FCToFC);
            TH_TimeSpan = new Thread(PRV_GetStatus);//开辟线程计时
            TH_TimeSpan.Start();
            TH_TimeSpan.Join();
           // IFeatureClassContainer ss = (FDS_Featuredataset.Workspace as IFeatureWorkspace).OpenFeatureDataset(FDS_Featuredataset.Name) as IFeatureClassContainer;
          //  Console.WriteLine("完成");
        }
        //创建要素数据集
        private void PRV_CreatFeatureDataset(string IN_FeatureDataSetName,string IN_ShapePath)
        {
            string Temp_Direction = System.IO.Path.GetDirectoryName(IN_ShapePath);//Shp文件的目录
            string Getname = System.IO.Path.GetFileNameWithoutExtension(IN_ShapePath);//Shp文件的文件名
            IWorkspaceFactory Temp_MDBWorkFactory = new AccessWorkspaceFactory();
            IFeatureWorkspace Temp_MDBWorkspace = Temp_MDBWorkFactory.OpenFromFile(S_MDBFile, 0) as IFeatureWorkspace;
            IWorkspaceFactory Temp_ShapeWorkFactory = new ShapefileWorkspaceFactory();
            IFeatureWorkspace Temp_ShapeWorkspace = Temp_ShapeWorkFactory.OpenFromFile(Temp_Direction, 0) as IFeatureWorkspace;
            IFeatureClass Temp_ShapeFeatureClass = Temp_ShapeWorkspace.OpenFeatureClass(Getname);//获取shp文件
            ISpatialReference SP_SpatialRefer = (Temp_ShapeFeatureClass as IGeoDataset).SpatialReference;//获取空间投影
            Temp_MDBWorkspace.CreateFeatureDataset(IN_FeatureDataSetName, SP_SpatialRefer);//在MDB中创建要素数据集
            FDS_Featuredataset = Temp_MDBWorkspace.OpenFeatureDataset(IN_FeatureDataSetName);//获取返回的要素数据集
        }
        //GP工具监督器,获取状态
        private void PRV_GetStatus()
        {
            WATCH_StopWatch.Start(); //  开始监视代码运行时间
            while (GP_Progress.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded && GP_Progress.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobFailed)
            {
                TS_TimeSpan = WATCH_StopWatch.Elapsed;
                S_Status = "状态:" + GP_Progress.Status.ToString() + "已经运行:" + ((int)(TS_TimeSpan.TotalSeconds)).ToString() + "秒";
                Thread.Sleep(1000);
                Console.WriteLine(S_Status);//控制台输出状态
            }
            WATCH_StopWatch.Stop();//停止计时器
            WATCH_StopWatch.Reset();//重置计时器
            TH_TimeSpan.Abort();//线程自杀
        }

    }
}


你可能感兴趣的:(ArcEngine)