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 王晓佳1609040125_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();
}
}
}