1. 程式人生 > >AE二次開發-判斷柵格圖層是否存在屬性表,獲取柵格欄位的唯一值

AE二次開發-判斷柵格圖層是否存在屬性表,獲取柵格欄位的唯一值

獲取柵格欄位的唯一值:

        /// <summary>
        /// 獲取柵格欄位的唯一值
        /// </summary>
        /// <param name="pLayer">柵格圖層</param>
        /// <param name="field">欄位</param>
        /// <returns>唯一值</returns>
        public static string[] GetRasterUniqueValue(ILayer pLayer, string
field) { IRasterLayer pRlyr = pLayer as IRasterLayer; if (pRlyr == null) return null; //判斷是否存在屬性表 if (!IsRasterLayerHaveTable(pRlyr.Raster)) return null; ITable pRTable = (ITable)pRlyr; //獲得柵格遊標 ICursor pCursor = pRTable.Search(null
, false); //IDataStatistics物件例項生成 IDataStatistics pData = new DataStatisticsClass(); pData.Field = field; //欄位 pData.Cursor = pCursor; //列舉唯一值 IEnumerator pEnumVar = pData.UniqueValues; //記錄總數 int recordCount = pData.UniqueValueCount; //字元陣列
string[] strValue = new string[recordCount]; pEnumVar.Reset(); //將遊標重置到第一個成員前面 int i = 0; while (pEnumVar.MoveNext()) //將遊標的內部位置向前移動 { if (pEnumVar.Current != null) strValue[i++] = pEnumVar.Current.ToString(); //獲取當前的項(只讀屬性) } return strValue; }

判斷柵格圖層是否存在屬性表:

        /// <summary>
        /// 判斷柵格圖層是否擁有屬性表
        /// </summary>
        /// <param name="pRaster">柵格</param>
        /// <returns>是否擁有屬性表</returns>
        public static bool IsRasterLayerHaveTable(IRaster pRaster)
        {
            IRasterProps pProp = pRaster as IRasterProps;
            if (pProp == null)
            {
                return false;
            }
            if (pProp.PixelType == rstPixelType.PT_FLOAT || pProp.PixelType == rstPixelType.PT_DOUBLE) //判斷柵格像元值是否是整型
            {
                return false;
            }
            IRasterBandCollection pRasterbandCollection = (IRasterBandCollection)pRaster;
            IRasterBand rasterBand = pRasterbandCollection.Item(0);
            ITable rTable = rasterBand.AttributeTable;
            return rTable != null;
        }