1
IRasterLayer layer = map.getLayer(0);

I want know that how can get attribute table of raster layer. Such as how many rows that attribute table of raster layer have and...

1 Answer 1

1

c# code

List<string> attributesList = new List<string>();

        IRasterDataset rasterDataset = (IRasterDataset)geodataset;
        IRaster raster = (IRaster)rasterDataset.CreateDefaultRaster();
        IRasterBandCollection rasterCol = (IRasterBandCollection)raster;
        IRasterBand rasterBand = rasterCol.Item(0);

        bool isHasTable;
        rasterBand.HasTable(out isHasTable);
        if (isHasTable == false)
        {
            MessageBox.Show("without attribute table");
            return;
        }

        ITable table = rasterBand.AttributeTable;
        string attributeFieldName = layerInfo.getRasterColumnName();
        int fieldIndex = table.FindField(attributeFieldName);

        if (fieldIndex==-1)
        {
            MessageBox.Show("field x not found");
            return;
        }

        IQueryFilter qF = new QueryFilterClass();
        qF.WhereClause = "";
        ICursor curs = table.Search(qF, true);

        IRow row = curs.NextRow();
        while (row != null)
        {
            string attributeValue = "";
            try
            {
                attributeValue = (string)row.get_Value(fieldIndex);
            }
            catch (Exception)
            {
                MessageBox.Show("error!");

            }
            attributesList.Add(attributeValue);
            row = curs.NextRow();
        }

Not the answer you're looking for? Browse other questions tagged or ask your own question.