ArcgisEngine识别要素

 在arcgismap中,我们点击识别要素的按钮,然后在选中元素就可以获取这个要素的属性了。

在arcgisengine中,实现的原理如下:

获取鼠标点击位置信息

 如果是栅格图层:

图层转为Identify对象,将扩大后的点作为参数输入,进行识别

遍历识别结果记录,获取图层名和对应位置的属性值

 如果是矢量要素图层:

先对点进行缓冲,扩大点覆盖范围(尽量减少 对空白区域的识别,增加对有效记录的识别)

图层转为Identify对象,将扩大后的点作为参数输入,进行识别

遍历识别结果中的记录,获取对应的要素属性记录,通过遍历属性记录来获取属性记录的字段、字段名
代码参考:

 #region 双击识别
        private void axMapControl1_OnDoubleClick(object sender, IMapControlEvents2_OnDoubleClickEvent e)
        {
            //获取鼠标点击位置点
            IPoint point = new PointClass();
            point.PutCoords(e.mapX, e.mapY);
            string output = "";
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                output += Identify_layer(axMapControl1.get_Layer(i), point);
            }
            MessageBox.Show(output);
        }
        private string Identify_layer(ILayer layer, IPoint SelectedPoint)
        {
            //定义几何图形
            IGeometry pGeometry;
            //定义结果字符串
            string output = "";
            //如果是矢量图层
            if (layer is IFeatureLayer)
            {
                //图层转Identify对象
                IIdentify pFL = layer as IIdentify;
                //对点进行缓冲赋给几何图形,尽量减少对空白区域判定
                ITopologicalOperator pTopo = SelectedPoint as ITopologicalOperator;
                pGeometry = pTopo.Buffer(500);
                //将几何图形作为输入传入Identify对象,进行识别
                IArray id_result = pFL.Identify(pGeometry);
                if (id_result != null)
                {
                    //对识别结果中的记录进行遍历(由于缓冲扩大,可能识别到同一图层多条记录)
                    for (int i = 0; i < id_result.Count; i++)
                    {
                        //获取识别结果记录中的属性记录
                        IIdentifyObj featureIdentifyobj = (IIdentifyObj)id_result.get_Element(i);
                        IRowIdentifyObject iRowIdentifyObject = featureIdentifyobj as IRowIdentifyObject;
                        IRow pRow = iRowIdentifyObject.Row;//添加引用GeoDatabase
                        output += "\"" + featureIdentifyobj.Layer.Name + "\":\n";
                        //遍历属性记录,获取字段名、字段值
                        for (int a = 0; a < pRow.Fields.FieldCount; a++)
                        {
                            if (pRow.Fields.get_Field(a).Type != esriFieldType.esriFieldTypeGeometry)
                            {
                                output += String.Format("{0}={1} \n", pRow.Fields.get_Field(a).Name, pRow.get_Value(a).ToString());
                            }
                        }
                    }
                }
            }
            else if (layer is IRasterLayer)
            {
                //图层转Identify对象
                IIdentify pFL = layer as IIdentify;
                //点转几何对象
                pGeometry = SelectedPoint as IGeometry;
                //将几何图形作为输入传入Identify对象,进行识别
                IArray id_result = pFL.Identify(pGeometry);
                if (id_result != null)
                {
                    //遍历识别结果记录,获取图层名和对应位置的属性值
                    for (int i = 0; i < id_result.Count; i++)
                    {
                        IIdentifyObj featureIdentifyobj = (IIdentifyObj)id_result.get_Element(i);
                        IRasterIdentifyObj rasterIdentifyobj = featureIdentifyobj as IRasterIdentifyObj;
                        output += "\"" + featureIdentifyobj.Layer.Name + "\":" + "\n" + rasterIdentifyobj.MapTip + "\n";
                    }
                }
            }
 
            return output + "\n";
        }
        #endregion

参考代码2:

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                                 else
40                                 {
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                                             else
85                                             {
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                         }

你好!对于在 ArcGIS for Android Runtime SDK 进行要素识别,你可以使用 IdentifyTask 类来实现。IdentifyTask 类可以通过查询图层来获取与用户绘制的几何图形相交的要素。 下面是一个示例代码,展示了如何使用 IdentifyTask 进行要素识别: ```java // 创建 IdentifyTask 对象 IdentifyTask identifyTask = new IdentifyTask(mapServiceUrl); // 创建 IdentifyParameters 对象 IdentifyParameters identifyParams = new IdentifyParameters(); // 设置查询的图层 identifyParams.setLayers(new int[]{0, 1, 2}); // 这里的图层索引根据你的地图服务而定 // 设置查询的几何图形 identifyParams.setGeometry(geometry); // 这里的 geometry 是用户绘制的几何图形 // 设置查询结果的返回类型 identifyParams.setSpatialReference(mapView.getSpatialReference()); identifyParams.setMapExtent(mapView.getExtent()); // 执行要素识别任务 identifyTask.executeAsync(identifyParams, new CallbackListener<IdentifyResult[]>() { @Override public void onCallback(IdentifyResult[] identifyResults) { // 处理查询结果 if (identifyResults != null && identifyResults.length > 0) { for (IdentifyResult result : identifyResults) { // 处理每个要素识别结果 // result.getFeature() 返回识别到的要素 // result.getLayerName() 返回要素所在图层的名称 // ... } } } @Override public void onError(Throwable throwable) { // 处理错误 } }); ``` 上述代码的 `mapServiceUrl` 是你要查询的地图服务的 URL,`geometry` 是用户绘制的几何图形。你可以根据自己的需求修改代码的参数。 希望这能帮到你!如果你还有其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值