ArcEngine中获取图层唯一值的方法

 ArcEngine中获取图层唯一值的方法
    
    这是在ArcEngine9.2中,获取图层指定字段唯一值的方法。鉴于ArcEngine众多的接口、函数,选择

合适的接口、方法来进行操作是很关键的,而且对它的学习也相对困难一些,需要一定的时间积累才能渡

过初期的困难时刻,这里与大家共勉,希望能早日成为AE高手。获取图层唯一值,一般通过

IDataStatistics和IQueryDef接口,IDataStatistics代码如下:

        /// <summary>
        /// 通过IDataStatistics获取图层指定字段唯一值
        /// </summary>
        /// <param name="pFeatureLayer"></param>
        /// <param name="fieldName"></param>
        /// <returns>指定字段所有唯一值</returns>
        private ArrayList GetLayerUniqueFieldValueByDataStatistics(IFeatureLayer

pFeatureLayer, string fieldName)
        {
            ArrayList arrValues = new ArrayList();
            IQueryFilter pQueryFilter = new QueryFilterClass();
            IFeatureCursor pFeatureCursor = null;

            pQueryFilter.SubFields = fieldName;
            pFeatureCursor = pFeatureLayer.FeatureClass.Search(pQueryFilter, true);

            IDataStatistics pDataStati = new DataStatisticsClass();
            pDataStati.Field = fieldName;
            pDataStati.Cursor = (ICursor)pFeatureCursor;

            IEnumerator pEnumerator = pDataStati.UniqueValues;
            pEnumerator.Reset();
            while (pEnumerator.MoveNext())
            {
                object pObj = pEnumerator.Current;
                arrValues.Add(pObj.ToString());
            }

            arrValues.Sort();
            return arrValues;
        }


      据网上资料介绍,当图层的数据量很大时,以上这种方法执行起来速度较慢,性能影响比较明显,

得考虑使用另外一种方法。通过IQueryDef接口使用类似SQL的查询语句来获取图层字段的唯一值集合,执

行速度等同于执行SQL查询语句。
        /// <summary>
        /// 通过IQueryDef获取图层指定字段唯一值
        /// </summary>
        /// <param name="pFeatureLayer"></param>
        /// <param name="fieldName"></param>
        /// <returns>指定字段所有唯一值</returns>
        private ArrayList GetLayerUniqueFieldValueByQueryDef(IFeatureLayer pFeatureLayer,

string fieldName)
        {
            ArrayList arrValues = new ArrayList();

            IQueryDef pQueryDef = null;
            IRow pRow = null;
            ICursor pCursor = null;
            IFeatureWorkspace pFeatWrok = null;
            IDataset pDataset = null;

            pDataset = (IDataset)pFeatureLayer.FeatureClass;
            pFeatWrok = (IFeatureWorkspace)pDataset.Workspace;
            pQueryDef = pFeatWrok.CreateQueryDef();
            pQueryDef.Tables = pDataset.Name;
            pQueryDef.SubFields = "DISTINCT(" + fieldName + ")";
            pCursor = pQueryDef.Evaluate();

            pRow = pCursor.NextRow();
            while (pRow != null)
            {
                object pObj = pRow.get_Value(0);
                arrValues.Add(pObj.ToString());

                pRow = pCursor.NextRow();
            }

            arrValues.Sort();
            return arrValues;
        }
      经过的我的实际测试,在C#中,用IQueryDef方法没有成功,运行到这句pCursor =

pQueryDef.Evaluate();就出错了,并没有得到结果集,估计是workspace的原因,查看ArcEngine的帮助

例子,确定应该是这个原因。因此后面的这个函数,在使用时需要注意,在同一个workspace下进行。

你可能感兴趣的:(ArcEngine)