AE属性表操作(2)

转载 2013年12月03日 11:59:00

转自:http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=129398


6、选中行高亮在地图中显示

/// <summary>
        /// 该行选中时高亮显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gdvAttribute_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            IQueryFilter pQuery = new QueryFilterClass();
            int count = this.gdvAttribute.SelectedRows.Count;
            string val;
            string col;
            col = this.gdvAttribute.Columns[0].Name;
            //当只选中一行时
            if (count == 1)
            {
                val = this.gdvAttribute.SelectedRows[0].Cells[col].Value.ToString();
                //设置高亮要素的查询条件
                pQuery.WhereClause = col + "=" + val;
            }
            else//当选中多行时
            {
                int i;
                string str;
                for (i = 0; i < count - 1; i++)
                {
                    val = this.gdvAttribute.SelectedRows[i].Cells[col].Value.ToString();
                    str = col + "=" + val + " OR ";
                    pQuery.WhereClause += str;
                }
                //添加最后一个要素的条件
                val = this.gdvAttribute.SelectedRows[i].Cells[col].Value.ToString();
                str = col + "=" + val;
                pQuery.WhereClause += str;
            }
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            IFeatureSelection pFeatSelection;
            pFeatSelection = pFLayer as IFeatureSelection;
            pFeatSelection.SelectFeatures(pQuery, esriSelectionResultEnum.esriSelectionResultNew, false);
            _MapControl.ActiveView.Refresh();
        }  

7、右键事件

右键事件里目前只实现了前四个:

首先是排序:

/// <summary>
        /// 数据排序
        /// </summary>
        /// <param name="pFeatureClass"></param>
        private void SortFeatures(IFeatureClass pFeatureClass)
        {
            ITableSort pTableSort = new TableSortClass();
            IFields pFields = pFeatureClass.Fields;
            IField pField = pFields.get_Field(col_index);

            pTableSort.Fields = pField.Name;

            if (up)
            {
                pTableSort.set_Ascending(pField.Name, true);
            }
            else
            {
                pTableSort.set_Ascending(pField.Name, false);
            }
            pTableSort.set_CaseSensitive(pField.Name, true);
            pTableSort.Table = pFeatureClass as ITable;
            pTableSort.Sort(null);
            ICursor pCursor = pTableSort.Rows;
            pTs = pTableSort;            
            RefreshTable();
        }


        /// <summary>
        /// 正序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolAsc_Click(object sender, EventArgs e)
        {
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            up = true;
            SortFeatures(pFLayer.FeatureClass);
        }

        /// <summary>
        /// 逆序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolDesc_Click(object sender, EventArgs e)
        {
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            up = false;
            SortFeatures(pFLayer.FeatureClass);
        }  

接下来就是删除字段:

/// <summary>
        /// 删除字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolDel_Click(object sender, EventArgs e)
        {
            int indexNum = gdvAttribute.CurrentCell.ColumnIndex;
            string strField = gdvAttribute.Columns[indexNum].HeaderText.ToString();
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            string strResult = "";
            if ((MessageBox.Show("确定要删除该字段吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes))
            {
                strResult = DeleteField(pFLayer, strField);
                gdvAttribute.Columns.Remove(strField);
                MessageBox.Show(strResult, "提示", MessageBoxButtons.OK);
            }               
        }

        /// <summary>
        /// 删除属性表字段
        /// </summary>
        /// <param name="layer">需要添加字段的IFeatureLayer</param>
        /// <param name="fieldName">添加的字段的名称</param>
        /// <returns></returns>
        public string DeleteField(IFeatureLayer layer, string fieldName)
        {
            try
            {
                ITable pTable = (ITable)layer;
                IFields pfields;
                IField pfield;
                pfields = pTable.Fields;
                int fieldIndex = pfields.FindField(fieldName);
                pfield = pfields.get_Field(fieldIndex);
                pTable.DeleteField(pfield);
                return "删除成功!";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

最后一个功能就是属性表计算:

/// <summary>
        /// 字段计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolFieldCalculator_Click(object sender, EventArgs e)
        {
            int indexNum = gdvAttribute.CurrentCell.ColumnIndex;
            string strField = gdvAttribute.Columns[indexNum].HeaderText.ToString();
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            formCalField formcalfield = new formCalField(pFLayer, gdvAttribute,strField);
            formcalfield.Show();
        }

属性表计算调用了窗口formCalField ,调用参数为IFeatureLayer pFeatureLayer,DataGridView dataGridView,string strField,那么看看formCalField 是如何设计的吧:

[attach]165443[/attach]

很简单,一个listbox,一个txtbox用与输入表达式,还有一个txtbox用于显示计算信息,其代码为:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geoprocessor;

namespace MapDemo
{
    public partial class formCalField : Form
    {
        private IFeatureLayer _FeatureLayer = null;
        private DataGridView Gridviwe;
        private string Field = "";
        public formCalField(IFeatureLayer pFeatureLayer,DataGridView dataGridView,string strField)
        {
            InitializeComponent();
            _FeatureLayer = pFeatureLayer;
            Field = strField;
            Gridviwe = dataGridView;
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void formCalField_Load(object sender, EventArgs e)
        {
            IFeatureClass pFeatureCls = _FeatureLayer.FeatureClass;
            int FieldCount = pFeatureCls.Fields.FieldCount;
            for (int i = 0; i < FieldCount; i++)
            {
                IField pField = pFeatureCls.Fields.get_Field(i);
                //去除shape字段
                if (pField.Name == "shape")
                {
                    continue;
                }
                listField.Items.Add("["+pField.Name+"]");
            }
            lblField.Text = Field+"=";
        }

        /// <summary>
        /// 双击选择字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listField_DoubleClick(object sender, EventArgs e)
        {
            string strSelectField = this.listField.SelectedItem.ToString();
            txtExpression.Text = strSelectField;
        }

        /// <summary>
        /// 计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCal_Click(object sender, EventArgs e)
        {
            string strResult = FieldCal(_FeatureLayer, Field, txtExpression.Text);
            MessageBox.Show(strResult);
            this.Close();
            RefreshTable refreshtable = new RefreshTable();
            refreshtable.Refresh(Gridviwe, _FeatureLayer);
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void brnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string FieldCal(IFeatureLayer pFtLayer,string strField,string strExpression)
        {
            txtMessage.Text = "正在计算请稍后……\r\n";
            try
            {
                Geoprocessor Gp = new Geoprocessor();
                Gp.OverwriteOutput = true;
                CalculateField calField = new CalculateField();
                calField.in_table = pFtLayer as ITable;
                calField.field = strField;
                calField.expression = strExpression;

                Gp.Execute(calField, null);
                
                for (int i = 0; i < Gp.MessageCount; i++)
                {
                    txtMessage.Text += Gp.GetMessage(i).ToString() + "\r\n";
                }
                return "计算成功";
            }
            catch (Exception e)
            {
                txtMessage.Text += e.Message;
                return "计算失败"+e.Message;
            }            
        }
    }
}

字段计算我是通过调用Geopressor实现的。最后,粘出RefreshTable这个类的源代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;

namespace MapDemo
{
    public class RefreshTable
    {
        //刷新属性表
        public void Refresh(DataGridView dataGridView, IFeatureLayer pFeatureLayer)
        {
            IFeatureLayer pFLayer = pFeatureLayer;
            IFeatureClass pFeatureClass = pFLayer.FeatureClass;

            if (pFeatureClass == null) return;

            DataTable dt = new DataTable();
            DataColumn dc = null;

            for (int i = 0; i < pFeatureClass.Fields.FieldCount; i++)
            {

                dc = new DataColumn(pFeatureClass.Fields.get_Field(i).Name);

                dt.Columns.Add(dc);

            }

            IFeatureCursor pFeatureCuror = pFeatureClass.Search(null, false);
            IFeature pFeature = pFeatureCuror.NextFeature();

            DataRow dr = null;
            while (pFeature != null)
            {
                dr = dt.NewRow();
                for (int j = 0; j < pFeatureClass.Fields.FieldCount; j++)
                {
                    if (pFeatureClass.FindField(pFeatureClass.ShapeFieldName) == j)
                    {

                        dr[j] = pFeatureClass.ShapeType.ToString();
                    }
                    else
                    {
                        dr[j] = pFeature.get_Value(j).ToString();

                    }
                }

                dt.Rows.Add(dr);
                pFeature = pFeatureCuror.NextFeature();
            }
            dataGridView.DataSource = dt;
        }
    }
}

至此,属性表基本的操作基本上已经实现了,还望对大家有所帮助……同时,继续关注我的相关更新,你的关注是我发帖最大的动力!!!

相关文章推荐

用C#+AE实现ArcGIS中的字段统计功

前几天在网上查找资料想实现ArcGIS中的统计功能,也就是Summarise,在费尽一番波折之后,终于在网上找到了解决方案:http://blog.csdn.net/gisoracle/article...

AE属性表操作(1)

今天给大家分享一个AE属性表操作的程序源码,希望对有需要的筒子一定的帮助,小弟倍感荣幸! 好了,废话少说!首先说说实现了哪些操作吧…… 实现的操作包括:1、打开属性表;2、编辑属性表;3、增加属性...

AE 统计分析

概述      一般常用的统计功能例如:唯一字段统计、数据行数统计、数据值求和统计等。     1、基础统计(BaseStatistics)组件用来生成和报告统计结果。    ...

SCSI 总线和协议

I/O 技术实现在计算机和存储设备之间的数据交换。实现从CPU 到存储系统的I/O通路的一个中间就是SCSI(Small Computer System Interface). 一.  I/O 通路 ...

SCSI协议

SCSI是一套完整的数据传输协议,其主要功能是在主机和存储设备之间传送命令、状态和块数据。在各类存储技术中,SCSI技术可谓是最重要的脊梁。 SCSI协议位于操作系统和外部资源之间,它具有一系列的功...

split(" ") 和 split(" ", -1) 区别

做实验表明:         1.如果字符串最后一位有值,则没有区别,         2.若干最后n位都是切割符,split(" ")不会继续切分,split(" ", -1...

AE属性表操作

原文连接:http://blog.csdn.net/lysc_forever/article/details/17093405 实现的操作包括:1、打开属性表;2、编辑属性表;3、增加属性列;4...

显示栅格图层和矢量图层的属性表(AE开发)

转载自:http://blog.csdn.net/xwebsite/article/details/4850383显示栅格图层和矢量图层的属性表定义一个函数showAttributeTable()用于...

QT属性表改变值操作demo

  • 2017-03-16 10:48
  • 4.86MB
  • 下载

arcengine属性表操作代码

  • 2015-05-05 08:50
  • 367KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)