一个GIS系统需要的基本功能的代码

 

一些基本的拖拽操作就不讲了,直接上代码吧。

 

1、 打开.mxd文件

基本思路:判断mxd路径存在→打开mxd文件

string filename = Application.StartupPath + "\\GIS系统.mxd";

if (axMapControl1.CheckMxFile(filename))

{

    axTOCControl1.SetBuddyControl(axMapControl1); //设置TOC与MapControl绑定

    axMapControl1.LoadMxFile(filename); //加载mxd文件

}

 

2、添加Shp(打开文件方式)

基本思路:打开文件→判断shp→添加shp→刷新地图

                OpenFileDialog pOpenFileDialog = new OpenFileDialog();

                pOpenFileDialog.CheckFileExists = true;

                pOpenFileDialog.Title = "导入文件";

                pOpenFileDialog.Filter = "Shape文件(*.shp)|*.shp|个人地理数据库(*.mdb)|*.mdb";

                pOpenFileDialog.ShowDialog();

 

                ////获取文件路径

                //FileInfo pFileInfo = new FileInfo(pOpenFileDialog.FileName);

                //string pPath = pOpenFileDialog.FileName.Substring(0, pOpenFileDialog.FileName.Length - pFileInfo.Name.Length);

                //mainMapControl.AddShapeFile(pPath, pFileInfo.Name);

 

                IWorkspaceFactory pWorkspaceFactory;

                IFeatureWorkspace pFeatureWorkspace;

                IFeatureLayer pFeatureLayer;

 

                string pFullPath = pOpenFileDialog.FileName;

                if (pFullPath == "") return;

 

                int pIndex = pFullPath.LastIndexOf("\\");

                string pFilePath = pFullPath.Substring(0, pIndex); //文件路径

                string pFileName = pFullPath.Substring(pIndex + 1); //文件名

                string pLastName = pFileName.Substring(pFileName.Length - 3);

                if (pLastName == "shp")

                {

                    //实例化ShapefileWorkspaceFactory工作空间,打开Shape文件

                    pWorkspaceFactory = new ShapefileWorkspaceFactory();

                    pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(pFilePath, 0);

                    //创建并实例化要素集

                    IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pFileName);

                    pFeatureLayer = new FeatureLayer();

                    pFeatureLayer.FeatureClass = pFeatureClass;

                    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;

 

                    //ClearAllData();    //新增删除数据

 

                    axMapControl1.Map.AddLayer(pFeatureLayer);

                    axMapControl1.ActiveView.Refresh();

                }

 

3、退出系统

基本思路:关闭窗口→释放资源→关闭系统

   this.Close();

            //if (mExitOperate == -1) //取消关闭

            //    return;

            //else

            //{

            //    Process.GetCurrentProcess().Kill();

            //    System.Environment.Exit(0);

            //}

            Process.GetCurrentProcess().Kill();

            System.Environment.Exit(0);

 

 

这三个基本功能可以满足一个基础的浏览数据的GIS系统。

 

 

 

 

 

说明:后文中可能会出现一些并未声明的变量,极有可能为全局变量,程序中的全局变量如下:

        //图层右键

        private ILayer m_Layer;

        private ILayer pMoveLayer;

        private int toIndex;

        private System.Drawing.Point pMoveLayerPoint;

        IFeatureLayer pTocFeatureLayer = null;

        private string sMapUnits = "";       

//指北针

        private INorthArrow m_NorthArrrow;

        private IElement pElement;

        private string operation;

 

若有任何问题,欢迎留言!

 

扫码关注微信公众号