加载shp mxd时出现的问题

来源:互联网 发布:你好旧时光 知乎 编辑:程序博客网 时间:2024/06/10 18:39

加载shp mxd时出现的问题

学习加载数据出现的问题

  • 加载shp
  • 加载mxd

加载shp

  • 加载shp函数
    打开shp需要两个参数,shp文件所在路径,以及shp名称。用lastindexof获取最后一个“\”索引,反斜杠需要转义字符,返回字符创数组
        public string[] OpenShapeFile()        {            var ShpFile = new string[2];            var OpenShpFile = new OpenFileDialog();            OpenShpFile.Title = "打开Shape文件";            OpenShpFile.InitialDirectory = "E:";            OpenShpFile.Filter = "Shape文件(*.shp)|*.shp";            if (OpenShpFile.ShowDialog() == DialogResult.OK)            {                var ShapPath = OpenShpFile.FileName;                //利用“\\”将文件路径分成两部分                var Position = ShapPath.LastIndexOf("\\");                var FilePath = ShapPath.Substring(0, Position);                var ShpName = ShapPath.Substring(Position + 1);                ShpFile[0] = FilePath;                ShpFile[1] = ShpName;            }            return ShpFile;        }
  • 按钮点击事件
    强制转换为string数组,方便获取函数中数组的值
        private void button2_Click(object sender, EventArgs e)        {            string[] shpfile = (string[]) OpenShapeFile();            string path = shpfile[0];            string filename = shpfile[1];            if (path == null || filename == null)            {                return;            }            axMapControl1.AddShapeFile(path,filename);        }

最后发现,在打开对话框但不选择shp数据,会返回错误,可以在加载前判断一下

加载mxd文档

函数

        public string OpenMxd()        {            var MxdPath = "";            var OpenMXD = new OpenFileDialog();            OpenMXD.Title = "打开地图";            OpenMXD.InitialDirectory = "E:";            OpenMXD.Filter = "Map Documents(*.mxd)|*.mxd";            if (OpenMXD.ShowDialog() == DialogResult.OK)            {                MxdPath = OpenMXD.FileName;            }            return MxdPath;        }
  • 点击加载
    获取返回值,对返回值进行判断,然后通过函数加载文档
   private void button1_Click(object sender, EventArgs e)        {            string str =  OpenMxd();            if (str=="")            {                return;            }                axMapControl1.LoadMxFile(str);        }
0 0
原创粉丝点击