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

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);
            }
        }

        
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值