Arcgis Engine 开发初步

学习Arcgis Engine有一段时间了,总结一下做过的实验,不能让自己白学,毕竟好记性不如烂笔头。

Arcgis Engine二次开发在VS平台上通过操作控件、接口来编写程序,以此达到我们想要的效果和目的。在编写程序的过程中,不可避免的要使用帮助文档,通过阅读以及查看Library Object Model Diagram PDF文档,了解不同类、方法、属性、接口的关系,这样技能是极其重要的。

Arcgis Engine 开发初步_第1张图片

Arcgis Engine 开发初步_第2张图片

实验一
加载shape文件(嗯,就是”hello world”级的初级问题)


Arcgis Engine 开发初步_第3张图片

private void Form1_Load(object sender, EventArgs e)
        {
            IWorkspaceFactory pWorkspaceFactory;
            pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspace pWorkSpace;
            pWorkSpace = pWorkspaceFactory.OpenFromFile(Application.StartupPath + "\\实验1",0);
            IFeatureWorkspace pFeatureWorksapce;
            pFeatureWorksapce = pWorkSpace as IFeatureWorkspace;
            IFeatureClass pFeatureClass;
            pFeatureClass = pFeatureWorksapce.OpenFeatureClass("林班分布图");
            IFeatureLayer pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pFeatureClass;
            axMapControl1.AddLayer(pFeatureLayer as ILayer);
        }


实验二
用C#+ArcGIS Engine加载shapefile文件,实现放大、缩小、全图、移动等基本功能。

Arcgis Engine 开发初步_第4张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;

namespace my_pro
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int flag = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            axMapControl2.AddShapeFile(Application.StartupPath + "\\pro", "林班分布图");
            axMapControl1.AddShapeFile(Application.StartupPath + "\\pro", "林班分布图");
        }

        private void 放大ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axMapControl2.MousePointer = esriControlsMousePointer.esriPointerZoomIn;
            flag = 1;
        }

        private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axMapControl2.MousePointer = esriControlsMousePointer.esriPointerZoomOut;
            flag = 2;
        }

        private void 移动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axMapControl2.MousePointer = esriControlsMousePointer.esriPointerPan;
            flag = 3; 
        }

        private void 全图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axMapControl2.Extent = axMapControl1.FullExtent;
        }

        private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            if (flag == 1) axMapControl2.Extent = axMapControl2.TrackRectangle();
            else if (flag == 2)
            {
                IEnvelope pEnvelope = axMapControl2.Extent;
                pEnvelope.Expand(2, 2, true);
                axMapControl2.Extent = pEnvelope;
            }
            else if (flag == 3) axMapControl2.Pan();

            IPoint point = new PointClass();
            point.PutCoords(e.mapX, e.mapY);
            axMapControl1.CenterAt(point);

        }
        private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            IEnvelope envelope = (IEnvelope)e.newEnvelope;
            IGraphicsContainer graphicsContainer = axMapControl2.Map as IGraphicsContainer;
            IActiveView activeView = graphicsContainer as IActiveView;
            //在绘图前,清除axMapControl2中的任何图形元素
            graphicsContainer.DeleteAllElements();
            IElement element = new RectangleElementClass();
            element.Geometry = envelope;
            //设置鹰眼图中的红线
            //产生一个线符号对象
            ILineSymbol outLineSymbol = new SimpleLineSymbolClass();
            outLineSymbol.Width = 2;
            outLineSymbol.Color = GetColor(255, 0, 0, 255);
            //设置颜色属性
            //设置填充符号的属性
            IFillSymbol fillSymbol = new SimpleFillSymbolClass();
            fillSymbol.Color = GetColor(9, 0, 0, 0);
            fillSymbol.Outline = outLineSymbol;
            IFillShapeElement fillShapeElement = element as IFillShapeElement;
            fillShapeElement.Symbol = fillSymbol;
            graphicsContainer.AddElement((IElement)fillShapeElement, 0);
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }
        private IRgbColor GetColor(int r, int g, int b, int t)
        {
            IRgbColor rgbColor = new RgbColorClass();
            rgbColor.Red = r;
            rgbColor.Green = g;
            rgbColor.Blue = b;
            rgbColor.Transparency = (byte)t;
            return rgbColor;
        }
    }
}


实验三
(1)通过调用地图文档的方法,调用地图,从而获得图层;
(2)通过点击不同的图层,获得图层的属性字段。

Arcgis Engine 开发初步_第5张图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace excel3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        IMap m_pMap;                //获得AxMapControl1的地图
        IEnumLayer m_pEnumLayers;   //在一个地图中枚举层集
        ILayer m_pLayer;            //在一个地图中指向一个图层
        private void Form1_Load(object sender, EventArgs e)
        {
            //axMapControl1.AddShapeFile(Application.StartupPath+"实验三数据","
            string str = Application.StartupPath + "\\实验三数据\\实验三数据.mxd";
            if (axMapControl1.CheckMxFile(str))
            {
                axMapControl1.LoadMxFile(str);
            }
            axTOCControl1.SetBuddyControl(axMapControl1);
            cboMaps.Items.Add(axMapControl1.Map.Name);

        }

        private void cboMaps_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_pMap = axMapControl1.Map;
            m_pEnumLayers = m_pMap.Layers;
            m_pLayer = m_pEnumLayers.Next();
            /*while (m_pLayer != null) { lboMapLayers.Items.Add(m_pLayer); m_pLayer = m_pEnumLayers.Next(); }*/
            while(m_pLayer != null)
            {
                lboMapLayers.Items.Add(m_pLayer.Name);
                m_pLayer = m_pEnumLayers.Next();
            }

        }

        private void lboMapLayers_Click(object sender, EventArgs e)
        {
            lboFields.Items.Clear(); 
            m_pEnumLayers.Reset();  //将枚举指针移到最开始
            m_pLayer = m_pEnumLayers.Next(); //获得枚举指针的第一个图层
            while(m_pLayer!= null) 
            { 
                if (m_pLayer.Name == lboMapLayers.Text)//检验看是否是这个图层
                { 
                    break;
                }
                m_pLayer = m_pEnumLayers.Next(); //获取下一个图层
            }
                IFeatureLayer pFLayer;
                pFLayer = m_pLayer as IFeatureLayer;
                IFeatureClass pFClass;
                IFields pFields;   
                pFClass =pFLayer.FeatureClass;
                pFields = pFClass.Fields;
                //pFields = pFClass.Fields;
                int i;
                for (i = 0; i < pFields.FieldCount - 1; i++)
                {
                    lboFields.Items.Add(pFields.get_Field(i).Name);
                }

        }
    }
}

总结:我觉得学会了使用VBA帮助文档理清各种接口、方法、属性的关系,以此编程开发就是一个质的飞跃。

最后,一首音乐谢谢你的阅读
你本来就很美 try

你可能感兴趣的:(arcgis)