框架搭建及数据加载

框架搭建

窗体布局

  1. 创建窗体(size: 914,642),首先加载工具栏
    工具箱–>meauStrip;手动输入所有菜单;
    框架搭建及数据加载_第1张图片
  2. 添加容器(百度搜索容器的介绍),此步骤很多可视化编程首先添加。
    工具箱–>SplitContainer:点击右上角小三角,选择水平,停靠父容器(实际上主窗体是一个大容器),其属性Dock改为fill,在左边的子容器里再添加SplitContainer容器,改为垂直,其属性Dock改为fill;这样基本布局就完成了。最后添加可视控件。
  3. 添加引用。
    框架搭建及数据加载_第2张图片
using System;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;

框架搭建及数据加载_第3张图片
以上引用全部打勾–>确定
所有引用皆在属性表里修改“嵌入互操作类型”为False;
框架搭建及数据加载_第4张图片

  1. 添加控件
    在添加GIS控件之前先添加TabControl到下图所示位置,
    框架搭建及数据加载_第5张图片
    Dock属性改为Fill,添加“数据视图”,“布局视图”,修改Alignment属性为Bottom;
    然后添加GIS控件:
    该控件拖进来就可以了;
    在这里插入图片描述
    然后:这三个控件,拖到相应的位置,所有Dock属性改为fill;
    三个控件的命名:
    axTOCControlEagleEyeMapControlmainMapControl、切换到布局视图再添加PageLayoutControl控件,命名为:axPageLayoutControl;修改命名是为了抄写源代码与命名规范的需要;
    最后添加toolstrip控件,点击黑三角,按下图设置属性
    框架搭建及数据加载_第6张图片
    框架搭建及数据加载_第7张图片
    输入字段“当前坐标为:”。修改名称如下:
    在这里插入图片描述
  2. 打开该窗体对应.cs文件,设置以下注释(会使后续工作快捷许多);

框架搭建及数据加载_第8张图片

关键设置

using ESRI.ArcGIS.esriSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MapOperation
{
     
    static class Program
    {
     
        /// 
        /// 应用程序的主入口点。
        /// 
        [STAThread]
        static void Main()
        {
     
            //基本权限,一定要的
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine);
            IAoInitialize m_aoinitialize = new AoInitializeClass();
            m_aoinitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
            //扩展权限
            m_aoinitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
           // LicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormMain());
        }
    }
}

代码布局及实现数据加载

  1. 首先,将以下代码放入封装的方法注释中,暂且不做解释;因为其中的许多方法需要引用;
    #region 封装的方法
        /// 
        /// 加载工作空间里面的要素和栅格数据
        /// 
        /// 
        private void AddAllDataset(IWorkspace pWorkspace, AxMapControl mapControl)
        {
     
            IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
            pEnumDataset.Reset();
            //将Enum数据集中的数据一个个读到DataSet中
            IDataset pDataset = pEnumDataset.Next();
            //判断数据集是否有数据
            while (pDataset != null)
            {
     
                if (pDataset is IFeatureDataset)  //要素数据集
                {
     
                    IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
                    IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
                    IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets;
                    pEnumDataset1.Reset();
                    IGroupLayer pGroupLayer = new GroupLayerClass();
                    pGroupLayer.Name = pFeatureDataset.Name;
                    IDataset pDataset1 = pEnumDataset1.Next();
                    while (pDataset1 != null)
                    {
     
                        if (pDataset1 is IFeatureClass)  //要素类
                        {
     
                            IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                            pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
                            if (pFeatureLayer.FeatureClass != null)
                            {
     
                                pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                                pGroupLayer.Add(pFeatureLayer);
                                mapControl.Map.AddLayer(pFeatureLayer);
                            }
                        }
                        pDataset1 = pEnumDataset1.Next();
                    }
                }
                else if (pDataset is IFeatureClass) //要素类
                {
     
                    IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
                    IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                    pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset.Name);

                    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                    mapControl.Map.AddLayer(pFeatureLayer);
                }
                else if (pDataset is IRasterDataset) //栅格数据集
                {
     
                    IRasterWorkspaceEx pRasterWorkspace = (IRasterWorkspaceEx)pWorkspace;
                    IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(pDataset.Name);
                    //影像金字塔判断与创建
                    IRasterPyramid3 pRasPyrmid;
                    pRasPyrmid = pRasterDataset as IRasterPyramid3;
                    if (pRasPyrmid != null)
                    {
     
                        if (!(pRasPyrmid.Present))
                        {
     
                            pRasPyrmid.Create(); //创建金字塔
                        }
                    }
                    IRasterLayer pRasterLayer = new RasterLayerClass();
                    pRasterLayer.CreateFromDataset(pRasterDataset);
                    ILayer pLayer = pRasterLayer as ILayer;
                    mapControl.AddLayer(pLayer, 0);
                }
                pDataset = pEnumDataset.Next();
            }

            mapControl.ActiveView.Refresh();
            //同步鹰眼
            SynchronizeEagleEye();
        }

        private void ClearAllData()
        {
     
            if (mainMapControl.Map != null && mainMapControl.Map.LayerCount > 0)
            {
     
                //新建mainMapControl中Map
                IMap dataMap = new MapClass();
                dataMap.Name = "Map";
                mainMapControl.DocumentFilename = string.Empty;
                mainMapControl.Map = dataMap;

                //新建EagleEyeMapControl中Map
                IMap eagleEyeMap = new MapClass();
                eagleEyeMap.Name = "eagleEyeMap";
                EagleEyeMapControl.DocumentFilename = string.Empty;
                EagleEyeMapControl.Map = eagleEyeMap;
            }
        }

        /// 
        /// 获取RGB颜色
        /// 
        /// 
        /// 绿
        /// 
        /// 
        private IRgbColor GetRgbColor(int intR, int intG, int intB)
        {
     
            IRgbColor pRgbColor = null;
            if (intR < 0 || intR > 255 || intG < 0 || intG > 255 || intB < 0 || intB > 255)
            {
     
                return pRgbColor;
            }
            pRgbColor = new RgbColorClass();
            pRgbColor.Red = intR;
            pRgbColor.Green = intG;
            pRgbColor.Blue = intB;
            return pRgbColor;
        }

        /// 
        /// 获取地图单位
        /// 
        /// 
        /// 
        private string GetMapUnit(esriUnits _esriMapUnit)
        {
     
            string sMapUnits = string.Empty;
            switch (_esriMapUnit)
            {
     
                case esriUnits.esriCentimeters:
                    sMapUnits = "厘米";
                    break;
                case esriUnits.esriDecimalDegrees:
                    sMapUnits = "十进制";
                    break;
                case esriUnits.esriDecimeters:
                    sMapUnits = "分米";
                    break;
                case esriUnits.esriFeet:
                    sMapUnits = "尺";
                    break;
                case esriUnits.esriInches:
                    sMapUnits = "英寸";
                    break;
                case esriUnits.esriKilometers:
                    sMapUnits = "千米";
                    break;
                case esriUnits.esriMeters:
                    sMapUnits = "米";
                    break;
                case esriUnits.esriMiles:
                    sMapUnits = "英里";
                    break;
                case esriUnits.esriMillimeters:
                    sMapUnits = "毫米";
                    break;
                case esriUnits.esriNauticalMiles:
                    sMapUnits = "海里";
                    break;
                case esriUnits.esriPoints:
                    sMapUnits = "点";
                    break;
                case esriUnits.esriUnitsLast:
                    sMapUnits = "UnitsLast";
                    break;
                case esriUnits.esriUnknownUnits:
                    sMapUnits = "未知单位";
                    break;
                case esriUnits.esriYards:
                    sMapUnits = "码";
                    break;
                default:
                    break;
            }
            return sMapUnits;
        }

        /// 
        /// 绘制多边形
        /// 
        /// 
        /// 
        public IPolygon DrawPolygon(AxMapControl mapCtrl)
        {
     
            IGeometry pGeometry = null;
            if (mapCtrl == null) return null;
            IRubberBand rb = new RubberPolygonClass();
            pGeometry = rb.TrackNew(mapCtrl.ActiveView.ScreenDisplay, null);
            return pGeometry as IPolygon;
        }
        #endregion
  1. 双击LoadMxFile方法,将产生的代码剪切到对应的注释中去;
    框架搭建及数据加载_第9张图片
    然后添加代码
#region LoadMxFile方法加载地图文档文件
        private void btnLoadMxFile_Click(object sender, EventArgs e)
        {
     
            //加载数据前如果有数据则清空
            try
            {
     
                OpenFileDialog pOpenFileDialog = new OpenFileDialog();
                pOpenFileDialog.CheckFileExists = true;
                pOpenFileDialog.Title = "打开地图文档";
                pOpenFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd;|ArcMap模板(*.mxt)|*.mxt|发布地图文件(*.pmf)|*.pmf|所有地图格式(*.mxd;*.mxt;*.pmf)|*.mxd;*.mxt;*.pmf";
                pOpenFileDialog.Multiselect = false;   //不允许多个文件同时选择
                pOpenFileDialog.RestoreDirectory = true;   //存储打开的文件路径
                if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
                {
     
                    string pFileName = pOpenFileDialog.FileName;
                    if (pFileName == "")
                    {
     
                        return;
                    }
                    if (mainMapControl.CheckMxFile(pFileName)) //检查地图文档有效性
                    {
     
                        ClearAllData();
                        mainMapControl.LoadMxFile(pFileName);
                    }
                    else
                    {
     
                        MessageBox.Show(pFileName + "是无效的地图文档!", "信息提示");
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
     
                MessageBox.Show("打开地图文档失败" + ex.Message);
            }
        }
        #endregion

注意添加try 语句,以防该操作失败致使程序暂停运行;
然后依次点击控件按钮,操作其余步骤*******
(所有代码书上源码包中)
小编匆促的写了这个技术文档,有问题不许提(呵呵),本书叫ArcGIS Engine,ArcGIS 安装有问题的先安装软件(不装Engine的,没认证权限的等)。

你可能感兴趣的:(ArcGIS二次开发,c#,.net,gis)