开始学AE(1)——加载地图文件、图层和SHP文件

自己开始学习AE了,一点一点学吧

加载地图有两种办法,一种是直接使用MapControl自带的方法直接加载

private void OpenMxdDocument()
        {
            // 加载地图方法一
            System.Windows.Forms.OpenFileDialog openfiledialog = new OpenFileDialog();
            openfiledialog.Title = "打开地图文档文件";
            openfiledialog.Filter = "地图文档 (*.mxd)|*.mxd";
            if (openfiledialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openfiledialog.FileName;
                if (axMapControl1.CheckMxFile(filePath))       //  CheckMxFile用来判断地址是否有效 
                {
                    axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                    // MapControl控件提供的LoadMxFile方法有三个参数,文件路径、地图名称或索引和密码,也可以直接重载为只有一个文件路径参数
		// Type.Missing用来获取默认值,放在后两个参数中 
                    axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
                    axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
                    // loadEagleEye(filePath);
                    axMapControl1.Extent = axMapControl1.FullExtent;
                }
                else
                {
                    MessageBox.Show("无法打开!" + filePath + "不是有效的地图文档");
                }

            }
        }


另一种方法是通过MapDocument对象对地图进行逐个加载

 private void OpenMxdDoc(){
            // 菜单点击后的加载地图文档
            // 加载地图方法二
            try
            {
                System.Windows.Forms.OpenFileDialog openfiledialog = new OpenFileDialog();
                openfiledialog.Title = "打开地图文档文件";
                openfiledialog.Filter = "地图文档 (*.mxd)|*.mxd";
                if (openfiledialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = openfiledialog.FileName; 
                    // mapdocument对象,相当于保存一个索引文件,打开文档的时候连同数据和地图一起打开
                    //  mapdocument对象加载的OPEN方法两个参数为路径和密码 
                    mapDocument.Open(filePath, "");
                    for (int i = 0; i < mapDocument.MapCount; i++)
                    {
                    	// 【这里不是非常理解】 
                        axMapControl1.Map = mapDocument.get_Map(i);
                    }
                    axMapControl1.Refresh();
                }
                else
                {
                    mapDocument = null;
                }
                // loadEagleEye( openfiledialog.FileName);
            }
            catch (Exception a)
            {
                MessageBox.Show(a.ToString());
            }
        }


加载特定地图的方法

private void OpenSpecialMxd()
        {
            // 加载特定地图
            System.Windows.Forms.OpenFileDialog openfiledialog = new OpenFileDialog();
            openfiledialog.Title = "打开地图文档文件";
            openfiledialog.Filter = "地图文档 (*.mxd)|*.mxd";
            if (openfiledialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openfiledialog.FileName;
                if (axMapControl1.CheckMxFile(filePath))
                {
                	//ReadMxMaps获取地图数组,IArray目前认为是一个数组类 
                    IArray arrayMap = axMapControl1.ReadMxMaps(filePath, Type.Missing);
                    int i;
                    IMap map = new MapClass();
                    for (i = 0; i < arrayMap.Count; i++)
                    {
                        if (map.Name == "Layers") // 找到特定地图再打开 
                        {
                            axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                            // 指针变成一个沙漏 
                            axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
                            axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
                            // loadEagleEye(filePath);
                            axMapControl1.Extent = axMapControl1.FullExtent;
                            break;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("无法打开!" + filePath + "不是有效的地图文档");
                }
            }
        }


保存地图文档(另存为与之类似)

private void saveDocument( )
        {
            // 保存文档
            if (mapDocument == null)
            {
                MessageBox.Show("地图文档对象为空,请先加载");
            }
            else
            {
                if (mapDocument.get_IsReadOnly(mapDocument.DocumentFilename))
                {
                    string fileSavePath = @"E:\newDoc.Mxd";
                    try
                    {
                        mapDocument.Save(mapDocument.UsesRelativePaths, true);
                        MessageBox.Show("保存成功!");
                    }
                    catch( Exception e)
                    {
                        MessageBox.Show("保存失败!" + e.ToString());
                    }
                }
            }
        }

添加图层文件

private void addLyrFile()
        {
            // 添加图层文件
            System.Windows.Forms.OpenFileDialog openfiledialog = new OpenFileDialog();
            openfiledialog.Title = "打开图层文件";
            openfiledialog.Filter = "图层文档 (*.lyr)|*.lyr";
            if (openfiledialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    axMapControl1.AddLayerFromFile(openfiledialog.FileName);
                }
                catch( Exception e)
                {
                    MessageBox.Show("添加失败!" + e.ToString());
                }
            }
        }

添加SHP文件

private void addShpFile()
        {
            // 添加SHP文件
            System.Windows.Forms.OpenFileDialog openfiledialog = new OpenFileDialog();
            openfiledialog.Title = "打开SHP文件";
            openfiledialog.Filter = "SHP文档 (*.shp)|*.shp";
            if (openfiledialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo fileinfo = new FileInfo(openfiledialog.FileName);     //      提供创建、复制、删除、移动和打开文件的实例方法
                string path = fileinfo.Directory.ToString();
                string filename = fileinfo.Name.Substring(0, fileinfo.Name.IndexOf("."));
                try
                {     // 第一个参数是全路径名,后一个是文件名
                    axMapControl1.AddShapeFile(path, filename);
                }
                catch( Exception e)
                {
                    MessageBox.Show("添加失败!" + e.ToString());
                }
            }
        }

移动图层

// 把最下层的图层移动到第一层
            axMapControl1.MoveLayerTo(axMapControl1.LayerCount - 1, 0);

设置鹰眼视图与主视图同步(设置显示范围的中心点)

private void setEagleEye()
        {
            // 设置鹰眼视图与主视图同步
            axMapControl2.MapScale = this.axMapControl1.MapScale * 2.0;         // 比例尺的设置
            IPoint point = new PointClass();
            point.X = (this.axMapControl1.Extent.XMax + this.axMapControl1.Extent.XMin) / 2;
            point.Y = (this.axMapControl1.Extent.YMax + this.axMapControl1.Extent.YMin) / 2;
            axMapControl2.ShowScrollbars = false;                               // 将滚轮设置为不可滚动
            axMapControl2.CenterAt(point);                                      // 将鹰眼图的中心设置为原视图的中心
            axMapControl2.Refresh();
        }



 

你可能感兴趣的:(AreEngine,的学习)