AE构建获取栅格图层属性表(ITable)

来源:互联网 发布:增值税开票软件安装 编辑:程序博客网 时间:2024/05/18 11:50

有时候生成的栅格图层没有属性表,需要自己构建属性表。其中,不能为 32 位浮点像素类型的栅格数据集构建栅格属性表。
因此,首先要判断栅格图层是否可以已存在属性表,已存在的话,就不需要重新构建。不存在的话,也要判断是否可以构建属性表,然后才能重新构建。

关键点:使用接口IRasterDatasetEdit2的BuildAttributeTable()方法。
代码如下:

        /// <summary>        /// 构建栅格图层属性表        /// </summary>        /// <param name="pLayer">图层</param>        /// <returns>属性表ITable</returns>        public static ITable BuildRasterTable(ILayer pLayer)        {            if (!(pLayer is IRasterLayer)) return null;            IRasterLayer rasterLayer = pLayer as IRasterLayer;            IRaster pRaster = rasterLayer.Raster;            IRasterProps rProp = pRaster as IRasterProps;            if (rProp == null)            {                return null;            }            if (rProp.PixelType == rstPixelType.PT_FLOAT || rProp.PixelType == rstPixelType.PT_DOUBLE) //判断栅格像元值是否是整型            {                return null;            }            IRasterBandCollection pRasterbandCollection = (IRasterBandCollection)pRaster;            IRasterBand rasterBand = pRasterbandCollection.Item(0);            ITable rTable = rasterBand.AttributeTable;            if (rTable != null) return rasterBand.AttributeTable; //直接获取属性表            string strPath = rasterLayer.FilePath;            string strDirName = System.IO.Path.GetDirectoryName(strPath);            string strRasterName = System.IO.Path.GetFileName(strPath);            //创建工作空间            IWorkspaceFactory pWork = new RasterWorkspaceFactoryClass();            //打开工作空间路径 ,工作空间的参数是目录,不是具体的文件名            IRasterWorkspace pRasterWs = (IRasterWorkspace)pWork.OpenFromFile(strDirName, 0);            //打开工作空间下的文件,            IRasterDataset rasterDataset = pRasterWs.OpenRasterDataset(strRasterName);            IRasterDatasetEdit2 rasterDatasetEdit = (IRasterDatasetEdit2)rasterDataset;            if (rasterDatasetEdit == null)            {                return null;            }            //Build default raster attribute table with VALUE and COUNT            rasterDatasetEdit.BuildAttributeTable();  //建立属性表            //更新属性表            pRasterbandCollection = (IRasterBandCollection)rasterDataset;            rasterBand = pRasterbandCollection.Item(0);            return rasterBand.AttributeTable;    //重新获取属性表        }

获取栅格属性表:

                /// <summary>        /// 获取栅格图层的属性表        /// </summary>        /// <param name="layer">栅格图层</param>        /// <returns></returns>        public static DataTable GetRasterTableByLayer(ILayer layer)        {            ITable iTable = BuildRasterTable(layer);            //ITable iTable = (ITable)layer; //若已存在属性表,直接用该行代码            if (iTable == null)              {                return null;            }            DataTable dataTable = new DataTable();            IFields fields = iTable.Fields;            for (int i = 0; i < fields.FieldCount; i++)            {                dataTable.Columns.Add(fields.Field[i].Name);            }            ICursor pCursor = iTable.Search(null, false);            IRow pRrow = pCursor.NextRow();            while (pRrow != null)            {                DataRow pRow = dataTable.NewRow();                for (int i = 0; i < pRrow.Fields.FieldCount; i++)                {                    pRow[i] = pRrow.Value[i].ToString();                }                dataTable.Rows.Add(pRow);                pRrow = pCursor.NextRow();            }            return dataTable;        }
原创粉丝点击