ArcEngine C# GIS開發入門作業 (五)Ex06——點擊要素空間數據字段獲取,緩衝區查詢,選中要素高亮

ArcEngine C# GIS開發入門作業 (五)Ex06——點擊要素空間數據字段獲取,緩衝區查詢,選中要素高亮

題目要求在這裏插入圖片描述

實現效果

在這裏插入圖片描述

代碼實現

注意事項先不重複了,前面幾篇文章都有,直接上代碼,這幾天在實習要順便寫個實習的文章,這個作業就先還來不及行行註釋了,爲了保持作業的連續性,先把作業發完,後面再註釋:

form1.cs

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.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace Ex06
{
    public partial class Form1 : Form
    {
        #region class private members
        private IMapControl3 m_mapControl = null;
        private string m_mapDocumentName = string.Empty;
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //get the MapControl
            m_mapControl = (IMapControl3)axMapControl1.Object;

            //disable the Save menu (since there is no document yet)
        }

        private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
        {
            //get the current document name from the MapControl
            m_mapDocumentName = m_mapControl.DocumentFilename;

            //if there is no MapDocument, diable the Save menu and clear the statusbar
            
        }

        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            if (e.button == 2)
            {
                IPoint pPoint = new PointClass();
                ITopologicalOperator pTopologicalOperator = pPoint as ITopologicalOperator;
                pPoint.PutCoords(e.mapX, e.mapY);
                IGeometry pGeometry = pTopologicalOperator.Buffer(0);
                axMapControl1.Map.SelectByShape(pGeometry, null, false);
                axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                ISelection pSelection = axMapControl1.Map.FeatureSelection;
                IEnumFeatureSetup pEnumFeatureSetup = pSelection as IEnumFeatureSetup;
                pEnumFeatureSetup.AllFields = true;
                
                IEnumFeature pEnumFeature = pSelection as IEnumFeature;
                IFeature pFeature = pEnumFeature.Next();
                IFeatureLayer pL = axMapControl1.get_Layer(0) as FeatureLayer;
                IGeoDataset pDataset = pL.FeatureClass as IGeoDataset;
                ISpatialReference pSpatialReference = pDataset.SpatialReference;
                IGeographicCoordinateSystem pGeoCoordSys = (pSpatialReference as IProjectedCoordinateSystem).GeographicCoordinateSystem;                 //地理座標
                IProjection pProjection = (pSpatialReference as IProjectedCoordinateSystem).Projection;                                                 //投影座標系
                string sProjection = pProjection.Name;                                                                                            //投影座標系
                if (pFeature != null)
                {
                    ProjectionTxetBox.Text = sProjection;
                    LandUseTextBox.Text = pFeature.get_Value(pFeature.Fields.FindField("Landuse")).ToString();
                    IPolygon polygon = pFeature.Shape as IPolygon;
                    ITopologicalOperator to = polygon as ITopologicalOperator;
                    IFeatureLayer pFL = (IFeatureLayer)axMapControl1.get_Layer(0);
                    IFeatureClass pFC = pFL.FeatureClass;
                    ISpatialFilter pSF = new SpatialFilterClass();
                    pSF.Geometry = to.Buffer(250);
                    pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                    IFeatureCursor pCursor;
                    pCursor = pFC.Search(pSF, true);
                    IFeatureSelection pFS = pFL as IFeatureSelection;
                    pFS.SelectFeatures(pSF, esriSelectionResultEnum.esriSelectionResultAdd, false);
                    axMapControl1.ActiveView.Refresh();
                }
            }
        }

    }
}

program.cs

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

namespace Ex06
{
    static class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new Ex06.LicenseInitializer();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine },
            new esriLicenseExtensionCode[] { });
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            //ESRI License Initializer generated code.
            //Do not make any call to ArcObjects after ShutDownApplication()
            m_AOLicenseInitializer.ShutdownApplication();
        }
    }
}
發佈了23 篇原創文章 · 獲贊 109 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章