C# 加载Shp文件到MapControl控件

加载Shp文件到地图控件MapControl有一般有两种方式。

1、用MapControl控件的AddShapeFile方法可加载Shape格式文件,调用该方法需要传入两个参数,分别是文件路径和不带扩展名的文件名,其调用方法如下:

axMapControl1.AddShapeFile(pathName, fileName);

    利用此方法加载Shape文件时,可能会报未知错误,试试:将LicenseControl控件拖入窗体。

  1.    private void AddShp(object sender, EventArgs e)  
  2.    {  
  3.        //利用文件对话框选择加载的shp文件路径  
  4.        OpenFileDialog OpenFdlg = new OpenFileDialog();  
  5.        OpenFdlg.Title = "选择shp文件";  
  6.        OpenFdlg.Filter = "Shape格式文件(*.shp)|*.shp";  
  7.        OpenFdlg.ShowDialog();  
  8.        string strFileName = OpenFdlg.FileName;  
  9.        if (strFileName == string.Empty)  
  10.        {  
  11.            return;  
  12.        }  
  13. //将Shp文件的路径分割为路径和不带格式后缀的文件名  
  14.        string pathName = System.IO.Path.GetDirectoryName(strFileName);  
  15.        string fileName = System.IO.Path.GetFileNameWithoutExtension(strFileName);  
  16.   
  17. 下面两行为默认指定文件路径和文件名  
  18.        //string pathName = @"E:\HiOS\DataSHP";  
  19.        //string fileName = "River_X";  
  20.        axMapControl1.AddShapeFile(pathName, fileName);  
  21.        axMapControl1.Extent = axMapControl1.FullExtent;  
  22.    }  
        private void AddShp(object sender, EventArgs e)
        {
            //利用文件对话框选择加载的shp文件路径
            OpenFileDialog OpenFdlg = new OpenFileDialog();
            OpenFdlg.Title = "选择shp文件";
            OpenFdlg.Filter = "Shape格式文件(*.shp)|*.shp";
            OpenFdlg.ShowDialog();
            string strFileName = OpenFdlg.FileName;
            if (strFileName == string.Empty)
            {
                return;
            }
	    //将Shp文件的路径分割为路径和不带格式后缀的文件名
            string pathName = System.IO.Path.GetDirectoryName(strFileName);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(strFileName);

	    下面两行为默认指定文件路径和文件名
            //string pathName = @"E:\HiOS\DataSHP";
            //string fileName = "River_X";
            axMapControl1.AddShapeFile(pathName, fileName);
            axMapControl1.Extent = axMapControl1.FullExtent;
        }

2、也可以利用工作空间工厂打开Shape格式文件

  1. private void AddShpData(object sender, EventArgs e)  
  2. {  
  3.     //利用文件对话框选择加载的shp文件路径  
  4.     OpenFileDialog OpenFdlg = new OpenFileDialog();  
  5.     OpenFdlg.Title = "选择shp文件";  
  6.     OpenFdlg.Filter = "Shape格式文件(*.shp)|*.shp";  
  7.     OpenFdlg.ShowDialog();  
  8.     string strFileName = OpenFdlg.FileName;  
  9.     if (strFileName == string.Empty)  
  10.     {  
  11.         return;  
  12.     }  
  13.   
  14.     string pathName = System.IO.Path.GetDirectoryName(strFileName);  
  15.     string fileName = System.IO.Path.GetFileNameWithoutExtension(strFileName);  
  16.       
  17.     //声明Shape文件工作空间工厂,并获取要素工作空间,加载要素类  
  18.     IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();  
  19.     IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pathName, 0);  
  20.     IFeatureWorkspace pFeatureWorkspace;  
  21.     pFeatureWorkspace = pWorkspace as IFeatureWorkspace;  
  22.     IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);  
  23.     IDataset pDataset = pFeatureClass as IDataset;  
  24.   
  25.     //声明要素图层,设置相应属性  
  26.     IFeatureLayer pFeatureLayer = new FeatureLayerClass();  
  27.     pFeatureLayer.FeatureClass = pFeatureClass;  
  28.     pFeatureLayer.Name = pDataset.Name;  
  29.   
  30.     //将要素图层转为图层,Add to MapControl   
  31.     ILayer pLayer = pFeatureLayer as ILayer;  
  32.     axMapControl1.Map.AddLayer(pLayer);  
  33. }  
        private void AddShpData(object sender, EventArgs e)
        {
            //利用文件对话框选择加载的shp文件路径
            OpenFileDialog OpenFdlg = new OpenFileDialog();
            OpenFdlg.Title = "选择shp文件";
            OpenFdlg.Filter = "Shape格式文件(*.shp)|*.shp";
            OpenFdlg.ShowDialog();
            string strFileName = OpenFdlg.FileName;
            if (strFileName == string.Empty)
            {
                return;
            }

            string pathName = System.IO.Path.GetDirectoryName(strFileName);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(strFileName);
            
            //声明Shape文件工作空间工厂,并获取要素工作空间,加载要素类
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pathName, 0);
            IFeatureWorkspace pFeatureWorkspace;
            pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
            IDataset pDataset = pFeatureClass as IDataset;

            //声明要素图层,设置相应属性
            IFeatureLayer pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pFeatureClass;
            pFeatureLayer.Name = pDataset.Name;

            //将要素图层转为图层,Add to MapControl 
            ILayer pLayer = pFeatureLayer as ILayer;
            axMapControl1.Map.AddLayer(pLayer);
        }





  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值