AE开发----图层操作

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using System.IO;

namespace AEKF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //加载地图
        private void button1_Click(object sender, EventArgs e)
        {
            loadMapDocument();
        }

        //加载特定地图
        private void button2_Click(object sender, EventArgs e)
        {
            loadMapDocument2();
        }

        //加载地图方法
        private void loadMapDocument()
        {
            System.Windows.Forms.OpenFileDialog openFileDialog;
            openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "打开地图文档";
            openFileDialog.Filter = "map documents(*.mxd)|*.mxd";
            openFileDialog.ShowDialog();
            string filePath = openFileDialog.FileName;
            if (axMapControl1.CheckMxFile(filePath))
            {
                axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
                axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
            }
            else
            {
                MessageBox.Show(filePath + "不是有效文档");
            }
        }

        //加载特定地图方法
        private void loadMapDocument2()
        {
            System.Windows.Forms.OpenFileDialog openFileDialog;
            openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "打开地图文档";
            openFileDialog.Filter = "map documents(*.mxd)|*.mxd";
            openFileDialog.ShowDialog();
            string filePath = openFileDialog.FileName;
            if (axMapControl1.CheckMxFile(filePath))
            {
                IArray arrayMap = axMapControl1.ReadMxMaps(filePath, Type.Missing);
                int i;
                IMap map;
                for (i = 0; i < arrayMap.Count; i++)
                {
                    map = arrayMap.get_Element(i) as IMap;
                    if (map.Name == "layers")
                    {
                        axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                        axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
                        axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
                        break;
                    }
                }
            }
            else
            {
                MessageBox.Show(filePath + "不是有效地图文档");
            }
        }

        // 打开地图
        private void button3_Click(object sender, EventArgs e)
        {
            loadMapDoc();
        }

        IMapDocument mapDocument;

        //打开地图方法
        private void loadMapDoc()
        {
            mapDocument = new MapDocumentClass();
            try
            {
                System.Windows.Forms.OpenFileDialog openFileDialog;
                openFileDialog = new OpenFileDialog();
                openFileDialog.Title = "打开地图文档";
                openFileDialog.Filter = "map document(*.mxd)|*.mxd";
                openFileDialog.ShowDialog();
                string filePath = openFileDialog.FileName;
                mapDocument.Open(filePath, "");
                for (int i = 0; i < mapDocument.MapCount; i++)
                {
                    axMapControl1.Map = mapDocument.get_Map(i);
                }
                axMapControl1.Refresh();

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

        }

        //保存地图
        private void button4_Click(object sender, EventArgs e)
        {
            saveDocument();
        }

        //保存地图方法
        private void saveDocument()
        {
            if (mapDocument.get_IsReadOnly(mapDocument.DocumentFilename) == true)
            {
                MessageBox.Show("地图文档是只读,无法保存");
            }

            try
            {
                mapDocument.Save(mapDocument.UsesRelativePaths, true);
                MessageBox.Show("保存地图成功");
            }
            catch (Exception e)
            {
                MessageBox.Show("保存地图失败!"+e.ToString());
            }
        }

        //另存地图
        private void button5_Click(object sender, EventArgs e)
        {
            saveASDocument();
        }

        //另存地图方法
        private void saveASDocument()
        {
            if (mapDocument.get_IsReadOnly(mapDocument.DocumentFilename) == true)
            {
                MessageBox.Show("地图文档是只读,无法保存");
            }
            string fileSavePath = @"e:\new.mxd";
            try
            {
                mapDocument.SaveAs(fileSavePath, true, true);
                MessageBox.Show("另存地图文档成功");
            }
            catch(Exception e)
            {
                MessageBox.Show("另存地图文档失败"+e.ToString());
            }
        }

        //添加图层
        private void button6_Click(object sender, EventArgs e)
        {
            addLayerFile();
        }

        //添加图层方法
        private void addLayerFile()
        {
            System.Windows.Forms.OpenFileDialog openFileDialog;
            openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "打开图层文件";
            openFileDialog.Filter = "map document(*.lyr)|*.lyr";
            openFileDialog.ShowDialog();
            string filepath = openFileDialog.FileName;
            try
            {
                if (filepath != "" && filepath != null)
                {
                    axMapControl1.AddLayerFromFile(filepath);
                }
            }
            catch(Exception e)
            {
                MessageBox.Show("添加图层失败"+e.ToString());
            }
        }

        //添加shape文件
        private void button7_Click(object sender, EventArgs e)
        {
            addShapeFile();
        }

        //添加shape文件方法
        private void addShapeFile()
        {
            System.Windows.Forms.OpenFileDialog openFileDialog;
            openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "打开图层文件";
            openFileDialog.Filter = "map document(*.shp)|*.shp";
            openFileDialog.ShowDialog();
            FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
            string path = openFileDialog.FileName.Substring(0,openFileDialog.FileName.Length);
            try
            {
                axMapControl1.AddShapeFile(path,fileInfo.Name);
            }
            catch(Exception e)
            {
                MessageBox.Show("添加图层失败"+e.ToString());
            }
        }

       //删除文件
        private void button8_Click(object sender, EventArgs e)
        {
            deleteLayer();
        }

        //删除文件方法
        private void deleteLayer()
        {
            try
            {
                for (int i = axMapControl1.LayerCount - 1; i >= 0; i--)
                {
                    axMapControl1.DeleteLayer(i);
                }
            }
            catch(Exception e)
            {
                MessageBox.Show("删除图层失败"+e.ToString());
            }
        }

        //移动图层
        private void button9_Click(object sender, EventArgs e)
        {
            moveLayer();
        }
        //移动图层方法
        private void moveLayer()
        {
            if (axMapControl1.LayerCount > 0)
            {
                try
                {
                    axMapControl1.MoveLayerTo(axMapControl1.LayerCount - 1, 0);
                }
                catch(Exception e)
                {
                    MessageBox.Show("移动图层失败"+e.ToString());
                }
            }
        }

    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值