• C#+AE 实现点击查询属性功能


    实现效果如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    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.Geometry;
    using ESRI.ArcGIS.Geodatabase;
    using ESRI.ArcGIS.Display;

    namespace _0920tryTreeList
    {
        public partial class Form1 : Form
        {
            //需要创建一个公共变量表格
            // public
           // public DataTable dt2 = new DataTable();
          //  DataRow dr2;
            public Form1()
            {
                InitializeComponent();
              


                //第二张表格
                //dt2.Columns.Add("Name");
                //dt2.Columns.Add("Value");
                //this.dataGridView2.DataSource = pDataTable;
                //this.dataGridView2.DataSource = dt2;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "*.mxd|*.mxd";
                ofd.ShowDialog();

                string fp = ofd.FileName;
                axMapControl1.LoadMxFile(fp,0,Type.Missing);
            }

            bool bu = false;
            private void button2_Click(object sender, EventArgs e)
            {
                bu = true;

            }

            //定义子节点的单击事件也用到的公共变量
          // public  IFeature pFeature = null;


           
            private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
            {

                try
                {
                    if (bu)
                    {
                        DataTable pDataTable = new DataTable();
                        DataRow pDataRow = null;
                        pDataTable.Columns.Add("ID");
                        pDataTable.Columns.Add("Name");
                        pDataTable.Columns.Add("ParentID");
                        pDataTable.Columns.Add("Value");

                        for (int i = 0; i < axMapControl1.Map.LayerCount;i++ )
                        {
                            pDataRow=pDataTable.NewRow();
                            string lyrName = axMapControl1.Map.get_Layer(i).Name;
                            pDataRow["ID"] = lyrName;
                            pDataRow["Name"] = lyrName;
                            pDataRow["ParentID"] = -1;
                          
                            pDataTable.Rows.Add(pDataRow);

                           
                            //开始点选查询
                            IMap pMap;
                            pMap = axMapControl1.Map as IMap;

                            //获取点图层
                            IFeatureLayer pFeatureLayer;
                            pFeatureLayer = pMap.get_Layer(i) as IFeatureLayer;
                            IFeatureClass pFeatureClass;
                            pFeatureClass = pFeatureLayer.FeatureClass;

                            //获取鼠标点击点
                            IPoint pPoint;
                            pPoint = new PointClass();
                            pPoint.PutCoords(e.mapX, e.mapY);

                            IGeometry pGeometry;

                            //定义缓冲区
                            double db = 2;
                            ITopologicalOperator pTop;
                            pTop = pPoint as ITopologicalOperator;
                            pGeometry = pTop.Buffer(db);

                            //选取
                            pMap.SelectByShape(pGeometry, null, false);
                            pMap.ClearSelection();

                            //空间过滤运算
                            ISpatialFilter pSpatialFilter = new SpatialFilterClass();
                            pSpatialFilter.Geometry = pGeometry;


                            //设置为不同的要素类型的图层
                  
                       
                            switch (pFeatureClass.ShapeType)
                            {
                                case esriGeometryType.esriGeometryPoint:
                                    pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                                    break;
                                case esriGeometryType.esriGeometryPolyline:
                                    pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
                                    break;
                                case esriGeometryType.esriGeometryPolygon:
                                    pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                                    break;

                            }
                            pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;

                            //指针
                            IFeatureCursor pFeatureCursor;
                            pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
                            IFeature pFeature;
                            pFeature = pFeatureCursor.NextFeature();

                            //开始遍历
                            while (pFeature != null)
                            {

                                //获取要素的字段名和字段值
                                int n = pFeature.Fields.FieldCount;
                                string sName;
                                string sValue;


                                //这句话的对象需要随着地图的改变而改变。可以是ID,FID 等带有唯一标识身份的 东西。
                                int index = pFeature.Fields.FindField("ObjectID");
                                if (index == -1)
                                    return;
                                IField pField = pFeature.Fields.get_Field(index);
                                sName = pField.Name;

                                sValue = pFeature.get_Value(index).ToString();


                                pDataRow = pDataTable.NewRow();

                                //之所以这样赋值是为了保证ID的唯一性;
                                pDataRow["ID"] = lyrName + sValue;
                                pDataRow["Name"] = sValue;
                                pDataRow["ParentID"] = lyrName;
                                pDataTable.Rows.Add(pDataRow);

                                pFeature = pFeatureCursor.NextFeature();

                            }
                            //这个是师兄交的,指针等占内存的东西在用完之后一定要释放;!!!
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
                        }
                     
                        treeList1.DataSource = pDataTable;
                        treeList1.ParentFieldName="ParentID";
                    

                     
                    }

                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            //换地图清除数据源
            private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
            {
                treeList1.DataSource = null;
               // dataGridView1.DataSource = null;
            }

       
            //************//出现的问题是:值不在预期范围内
            private void treeList1_FocusedNodeChanged_1(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
            {
               
                try
                {
                    int layerIndex;
                    if (e.Node.HasChildren)
                    {
                        return;
                    }
                    if (e.Node.ParentNode != null) //***********//这个存在bug,若节点超过两级则出错
                    {
                        //创建一个新的表作为属性表
                        DataTable dt = new DataTable();
                        DataRow dr = null;
                        dt.Columns.Add("Name");
                        dt.Columns.Add("Value");


                        //循环图层
                        for (int i = 0; i < this.axMapControl1.LayerCount; i++)
                        {

                            //如果父节点名称和图层名相同,获取索引
                            if (e.Node.ParentNode.GetValue(0).ToString() == this.axMapControl1.get_Layer(i).Name)
                            {
                                layerIndex = i;
                                IFeature pFeature;                             
     
                               pFeature = (this.axMapControl1.get_Layer(layerIndex) as IFeatureLayer).FeatureClass.GetFeature(int.Parse(this.treeList1.FocusedNode.GetValue(0).ToString())); ;

                              if (pFeature != null)
                                {                            
                                   //循环字段集,赋值给表dt
                                    int n = pFeature.Fields.FieldCount;
                                 
                                    for (int k = 0; k < n-1; k++)
                                    {
                                       
                                        IField pField = pFeature.Fields.get_Field(k);
                                        string fieldName = pField.Name;
                                        var  a=  pFeature.get_Value(k);
                                        string fieldValue = pFeature.get_Value(k).ToString();

                                        //赋值给表
                                        dr = dt.NewRow();
                                        dr["Name"]=fieldName;
                                         dr["Value"]=fieldValue;
                                         dt.Rows.Add(dr);
                                    }  
                             
                                    gridControl1.DataSource = dt;
                                   
                                }
                                else
                                    return;                  
                               
                            }
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

           
        }
    }
     

  • 相关阅读:
    汉诺塔学习笔记,有不正确的地方请小伙伴们指正~·~
    梯有N阶,上楼可以一步上一阶,也可以一步上二阶。编写一个程序,计算共有多少中不同的走法?
    HTTP Status 500
    java基础知识
    JAVA多线程和并发基础面试问答
    thymeleaf中的th:remove用法
    thymeleaf:局部变量 th:with
    springboot: thymeleaf 使用详解
    eclipse修改工作目录颜色
    The user specified as a definer ('root'@'%') does not exist
  • 原文地址:https://www.cnblogs.com/yanhan/p/2701520.html
Copyright © 2020-2023  润新知