ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(layerComboBox.SelectedIndex) as ESRI.ArcGIS.Carto.IFeatureLayer; ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass(); point.PutCoords(e.mapX, e.mapY);
ESRI.ArcGIS.Geodatabase.ISpatialFilter spatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass(); spatialFilter.Geometry = point; spatialFilter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects; ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor = pFeatureLayer.Search(spatialFilter, false);
ESRI.ArcGIS.Geodatabase.IFeature pFeature;
while ((pFeature = featureCursor.NextFeature()) != null) {
axMapControl1.FlashShape(pFeature.Shape); } } }
其中,如果为点击模式(即DoQueryIndex = 1)则执行点击查询的代码。首先仍然是点击查询的图层对象或FeatureClass,在此使用IFeatureLayer,然后定义一个IPoint对象,通过PutCoords方法设置其值为地图上点击的位置。然后定义一个ISpatialFilter对象用于设置空间查询的条件,设置其查询的几何范围为前面定义的IPoint对象,同时设置几何对象的几何关系,最后通过IFeatureLayer的Search()方法来执行查询,查询返回结果为一个IFeatureCursor游标对象,通过该游标的NextFeature()方法可以获取游标中的每一个IFeature对象,这些IFeature就是要查询的结果。在此只是闪烁显示了这些对象,如果要获取该对象的属性等信息,直接使用IFeature的Fields属性即可获得。 运行程序,其结果如下图所示:
2、面范围查询
面范围查询首先还是要绘制面的几何图形,然后通过IFeatureClass或IFeatureLayer的Search()方法来执行查询,其方式和点击查询完全相同,只是绘制几何图形的方式不同。如果采用面范围查询方式,在AxMapControl控件的OnMouseDown()事件中记录鼠标点击位置,同时在OnMouseDown()事件中绘制这样一个面区域。 1)全局变量定义
在全局中定义变量pointCollection,其为IPointCollection对象,它用于保存每次在AxMapControl控件的OnMouseDown()事件中点击的鼠标位置,如下: private ESRI.ArcGIS.Geometry.IPointCollection pointCollection; 2)定义绘制面函数
由于在点击鼠标的同时,还要显示已经绘制的面的范围以便用户更好的选择查询区域,所以需要定义一个绘制面的函数DrawPolygon(),其代码如下:
private void DrawPolygon(ESRI.ArcGIS.Geometry.IPointCollection pPointCollection, ESRI.ArcGIS.Controls.AxMapControl axMapControl)
{
ESRI.ArcGIS.Geometry.IPolygon pPolygon;
pPolygon = (ESRI.ArcGIS.Geometry.IPolygon)pPointCollection; axMapControl.DrawShape(pPolygon); }
3)记录面的边界点
由于每次在AxMapControl上点击鼠标表示该为面创建一个边界点,所以在OnMouseDown()事件中要添加该点到变量pointCollection中去,同时绘制已经形成的面。故在axMapControl1的OnMouseDown()事件中继续添加如下代码: else if(DoQueryIndex == 2)//面范围查询 {
ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass(); point.PutCoords(e.mapX, e.mapY);
pointCollection.AddPoints(1, ref point);
if (pointCollection.PointCount > 2) {
DrawPolygon(pointCollection, axMapControl1); } }
4)查询范围内对象
设计当鼠标在axMapControl1控件上双击时即完成面范围的绘制,所以执行面范围的查询就在axMapControl1的OnDoubleClick()事件中,如下代码所示: private void axMapControl1_OnDoubleClick(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnDoubleClickEvent e) {
if(DoQueryIndex == 2)//面范围查询 {
ESRI.ArcGIS.Geometry.IPoint point = new ESRI.ArcGIS.Geometry.PointClass(); point.PutCoords(e.mapX, e.mapY); pointCollection.AddPoints(1, ref point); if (pointCollection.PointCount > 2) {
axMapControl1.Refresh();
ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(layerComboBox.SelectedIndex) as ESRI.ArcGIS.Carto.IFeatureLayer; ESRI.ArcGIS.Geometry.IPolygon pPolygon;
pPolygon = (ESRI.ArcGIS.Geometry.IPolygon)pointCollection;
ESRI.ArcGIS.Geodatabase.ISpatialFilter spatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
spatialFilter.Geometry = pPolygon; spatialFilter.SpatialRel =
ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects; ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor = pFeatureLayer.Search(spatialFilter, false);
ESRI.ArcGIS.Geodatabase.IFeature pFeature; int featureCount = 0;
while ((pFeature = featureCursor.NextFeature()) != null) {
axMapControl1.FlashShape(pFeature.Shape); featureCount++; }
MessageBox.Show(\选择范围内共有\个对象!\
} } }
调用几何条件查询在“根据几何条件查询对象”按钮的Click()事件中完成,所以在调用面范围查询时还需要对全局变量pointCollection进行实例化,所以“根据几何条件查询对象”按钮Click()事件的代码如下:
private void 根据几何条件查询对象_Click(object sender, EventArgs e) {
if (根据几何条件查询对象.Text == \根据几何条件查询对象\ {
if (pointRadioButton.Checked)//点击查询 {
DoQueryIndex = 1; }
else if (polygonRadioButton.Checked)//面范围查询 {
DoQueryIndex = 2;
pointCollection = new ESRI.ArcGIS.Geometry.PolygonClass(); }
根据几何条件查询对象.Text = \停止几何条件查询对象\ } else {
根据几何条件查询对象.Text = \根据几何条件查询对象\ DoQueryIndex = 0; }

