AE二次开发-获取图层的属性表


获取矢量或栅格图层的属性表,且可以指定获取的行数。
可以根据字段属性设置数据表的列属性。

        /// <summary>
        /// 获取图层的属性表
        /// </summary>
        /// <param name="layer">图层</param>
        /// <param name="rowCount">指定获取的行数</param>
        /// <returns>属性表</returns>
        public static DataTable GetAttributeTable(ILayer layer, long rowCount = -1)
        {
            ITable iTable = null;
            if (layer is IFeatureLayer)
            {
                iTable = (ITable)layer;
            }
            else
            {
                IRasterLayer rasLayer = layer as IRasterLayer;
                //判断是否存在属性表
                if (rasLayer != null && IsRasterLayerHaveTable(rasLayer.Raster))
                {
                    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 = AddTableColumnsByField(fields.Field[i], dataTable);
            }
            string shapeType = FeatureFunction.GetShapeType(layer);
            ICursor pCursor = iTable.Search(null, false);
            IRow pRow = pCursor.NextRow();
            while (pRow != null)
            {
                //新建DataTable的行对象
                DataRow pDataRow = dataTable.NewRow();
                for (int i = 0; i < pRow.Fields.FieldCount; i++)
                {
                    //如果字段类型为esriFieldTypeGeometry,则根据图层类型设置字段值
                    switch (pRow.Fields.Field[i].Type)
                    {
                        case esriFieldType.esriFieldTypeGeometry:
                            pDataRow[i] = shapeType;
                            break;
                        case esriFieldType.esriFieldTypeBlob:
                            pDataRow[i] = "Element";
                            break;
                        default:
                            pDataRow[i] = pRow.Value[i];
                            break;
                    }
                }
                //添加DataRow到DataTable
                dataTable.Rows.Add(pDataRow);
                pRow = pCursor.NextRow();
                //设置指定的行数
                if (rowCount != -1 && dataTable.Rows.Count == rowCount)
                {
                    break;
                }
            }
            return dataTable;
        }


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

        /// <summary>
        /// 根据字段属性设置数据表的列属性
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="table">数据表</param>
        /// <returns>数据表</returns>
        public static DataTable AddTableColumnsByField(IField field, DataTable table)
        {
            //新建一个DataColumn并设置其属性
            DataColumn pDataColumn = new DataColumn(field.Name);
            //字段值是否允许为空
            pDataColumn.AllowDBNull = field.IsNullable;
            //字段别名
            pDataColumn.Caption = field.AliasName;
            //字段数据类型
            pDataColumn.DataType = Type.GetType(ConvertFieldType(field.Type));
            //字段默认值
            pDataColumn.DefaultValue = field.DefaultValue;
            //当字段为String类型是设置字段长度
            if (field.VarType == 8)
            {
                pDataColumn.MaxLength = field.Length;
            }
            //字段添加到表中
            table.Columns.Add(pDataColumn);
            return table;
        }

转载自:https://blog.csdn.net/m0_37862405/article/details/78152384

You may also like...

退出移动版