一、作业要求
- 在地图上右键单击,根据鼠标位置查询到Parcels中的Parcel。在窗体上显示该Parcel的投影信息以及“land use”字段值。
- 根据给定半径对1中的Parcel做缓冲区,并选中与缓冲区相交的所有parcel。
二、作业过程
1.新建MapControl Application
2.窗体布局
3.引用的类库
4.编写代码
a.MainForm.cs
引入的命名空间
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
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 EX05
{
public sealed partial class MainForm : Form
{
#region class private members
private IMapControl3 m_mapControl = null;
private string m_mapDocumentName = string.Empty;
#endregion
#region class constructor
public MainForm()
{
InitializeComponent();
}
#endregion
private void MainForm_Load(object sender, EventArgs e)
{
//get the MapControl
m_mapControl = (IMapControl3)axMapControl1.Object;
//disable the Save menu (since there is no document yet)
menuSaveDoc.Enabled = false;
}
#region Main Menu event handlers
private void menuNewDoc_Click(object sender, EventArgs e)
{
//execute New Document command
ICommand command = new CreateNewDocument();
command.OnCreate(m_mapControl.Object);
command.OnClick();
}
private void menuOpenDoc_Click(object sender, EventArgs e)
{
//execute Open Document command
ICommand command = new ControlsOpenDocCommandClass();
command.OnCreate(m_mapControl.Object);
command.OnClick();
}
private void menuSaveDoc_Click(object sender, EventArgs e)
{
//execute Save Document command
if (m_mapControl.CheckMxFile(m_mapDocumentName))
{
//create a new instance of a MapDocument
IMapDocument mapDoc = new MapDocumentClass();
mapDoc.Open(m_mapDocumentName, string.Empty);
//Make sure that the MapDocument is not readonly
if (mapDoc.get_IsReadOnly(m_mapDocumentName))
{
MessageBox.Show("Map document is read only!");
mapDoc.Close();
return;
}
//Replace its contents with the current map
mapDoc.ReplaceContents((IMxdContents)m_mapControl.Map);
//save the MapDocument in order to persist it
mapDoc.Save(mapDoc.UsesRelativePaths, false);
//close the MapDocument
mapDoc.Close();
}
}
private void menuSaveAs_Click(object sender, EventArgs e)
{
//execute SaveAs Document command
ICommand command = new ControlsSaveAsDocCommandClass();
command.OnCreate(m_mapControl.Object);
command.OnClick();
}
private void menuExitApp_Click(object sender, EventArgs e)
{
//exit the application
Application.Exit();
}
#endregion
//listen to MapReplaced evant in order to update the statusbar and the Save menu
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
if (m_mapDocumentName == string.Empty)
{
menuSaveDoc.Enabled = false;
statusBarXY.Text = string.Empty;
}
else
{
//enable the Save manu and write the doc name to the statusbar
menuSaveDoc.Enabled = true;
statusBarXY.Text = System.IO.Path.GetFileName(m_mapDocumentName);
}
}
private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
statusBarXY.Text = string.Format("{0}, {1} {2}", e.mapX.ToString("#######.##"), e.mapY.ToString("#######.##"), axMapControl1.MapUnits.ToString().Substring(4));
}
#region 创建一个以点击点为圆心、0为半径的缓冲区,然后选择该缓冲区内的所有要素,并显示这些要素的所有字段。此外,它还获取了这些要素的地理坐标和投影坐标系名称。
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 2) // 如果鼠标右键被按下
{
IPoint pPoint = new PointClass();// 创建一个新的点对象
ITopologicalOperator pTopologicalOperator = pPoint as ITopologicalOperator; // 将点对象转换为TopologicalOperator接口,该接口定义了处理几何对象的一些方法
pPoint.PutCoords(e.mapX, e.mapY); // 设置点的坐标为鼠标点击的地图坐标
IGeometry pGeometry = pTopologicalOperator.Buffer(0); // 创建一个几何对象,通过调用TopologicalOperator接口的Buffer方法,该方法可以创建缓冲区几何对象
axMapControl1.Map.SelectByShape(pGeometry, null, false); // 使用地图控件的SelectByShape方法,根据几何对象选择地图上的要素
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; // 获取投影坐标系的名称并赋值给字符串sProjection //投影坐标系
if (pFeature != null)// 如果选中的要素不为空
{
ProjectionTextBox.Text = sProjection; // 设置投影坐标系的文本框
LandUseTextBox.Text = pFeature.get_Value(pFeature.Fields.FindField("Landuse")).ToString(); // 获取要素的"Landuse"字段的值并转换为字符串
IPolygon polygon = pFeature.Shape as IPolygon; // 将选中的要素的形状转换为多边形
ITopologicalOperator to = polygon as ITopologicalOperator; // 将多边形转换为TopologicalOperator接口,该接口包含了对几何对象进行操作的方法
IFeatureLayer pFL = (IFeatureLayer)axMapControl1.get_Layer(0); // 获取地图控件的第一个图层并转换为要素图层
IFeatureClass pFC = pFL.FeatureClass; // 获取要素图层的要素类
ISpatialFilter pSF = new SpatialFilterClass();// 创建一个新的空间过滤器
pSF.Geometry = to.Buffer(250); // 设置空间过滤器的几何形状为多边形的缓冲区(半径为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(); // 刷新地图控件的显示,以显示新选中的要素
}
}
#endregion
}
}
}
b.Program.cs
引入的命名空间
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ESRI.ArcGIS;
using ESRI.ArcGIS.esriSystem;
代码
namespace EX05
{
static class Program
{
//private static LicenseInitializer m_AOLicenseInitializer = new Mainform.LicenseInitializer(); // LicenseInitializer类名“LicenseInitializer.Designer.cs”和“LicenseInitializer.cs”
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!RuntimeManager.Bind(ProductCode.Engine))
{
if (!RuntimeManager.Bind(ProductCode.Desktop))
{
MessageBox.Show("Unable to bind to ArcGIS runtime. Application will be shut down.");
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
三、作业成果
- 在地图上右键单击,根据鼠标位置查询到Parcels中的Parcel。在窗体上显示该Parcel的投影信息以及“land use”字段值。
- 根据给定半径对1中的Parcel做缓冲区,并选中与缓冲区相交的所有parcel。