你的位置:首页 > ASP.net教程

[ASP.net教程]C# ArcEngine 实现点击要素高亮并弹出其属性


本文是模仿ArcMap里面的Identify(识别)功能,通过点击要素,使其高亮显示并弹出其属性表!本文只做了点击查询!

本文所用的环境为VS2010,AecEngine基于C#语言,界面是用Dev做的,比windows自带的窗体稍微好看点,效果如下:

主要实现代码(在axMapControl1_OnMouseDown下):

 1             ITopologicalOperator pTopo; 2             IGeometry pGeometry; 3             IFeature pFeature; 4             IFeatureLayer pFeatureLayer; 5             IFeatureCursor pCursor; 6             ISpatialFilter pFilter; 7             DataTable dataTable; 8              9             for (int i = 0; i < axMapControl1.Map.LayerCount; i++)10             {11               12               pPoint = new PointClass();13               pPoint.PutCoords(e.mapX, e.mapY);14               pTopo = pPoint as ITopologicalOperator;17               double m_Radius = 1;18               pGeometry = pTopo.Buffer(m_Radius); 19               if (pGeometry == null)20                 continue;21               axMapControl1.Map.SelectByShape(pGeometry, null, true);//第三个参数为是否只选中一个22               axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null); //选中要素高亮显示23               pFilter = new SpatialFilterClass();25               pFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;26               pFilter.Geometry = pGeometry;27               pFeatureLayer = axMapControl1.Map.get_Layer(i) as IFeatureLayer; 28               pCursor = pFeatureLayer.Search(pFilter, false);29               pFeature = pCursor.NextFeature();30               string fieldName;31               if (pFeature != null)33               {34                 if (Application.OpenForms["otherQueryForm"] == null)35                 {36                   otherqueryform = new otherQueryForm();37                   otherqueryform.Show();38                 }39                 else40                 {41                   Application.OpenForms["otherQueryForm"].Show();42                 }//这里主要控制子窗体不会重复弹出44                 dataTable = new DataTable();45                 for (int k = 0; k < 2; k++)46                 {47                   if (k == 0)48                   {49                     dataTable.Columns.Add("属性");50                   }51                   if (k == 1)52                   {53                     dataTable.Columns.Add("值");54                   }55                 }56                 DataRow datarow;57                 for (int j = 0; j < pFeature.Fields.FieldCount; j++)58                 {59                   datarow = dataTable.NewRow();60                   for (int m = 0; m < 2; m++)61                   {62                     if (m == 0)63                     {64                       fieldName = pFeature.Fields.get_Field(j).Name;65                       datarow[m] = fieldName;66                     }67                     if (m == 1)68                     {69                       if (pFeature.Fields.get_Field(j).Name == "Shape")70                       {71                         if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)72                         {73                           datarow[m] = "点";74                         }75                         if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)76                         {77                           datarow[m] = "线";78                         }79                         if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)80                         {81                           datarow[m] = "面";82                         }83                       }84                       else85                       {86                         datarow[m] = pFeature.get_Value(j).ToString();87                       }88                     }89                   }90                   dataTable.Rows.Add(datarow);91                 }92                 otherqueryform.dataGridView1.DataSource = dataTable;93                 otherqueryform.layerName_dev.Text = pFeatureLayer.Name;94                 otherqueryform.dataGridView1.Refresh();95                 pFeature = null;96                 break;97               }99             }