How to create a raster unique value renderer


Summary
This article shows how to create a raster unique value renderer.

Creating a raster unique value renderer

See the following code example:
[Java]
static IRasterRenderer uniqueValueRenderer(IRasterDataset rasterDataset)throws
    Exception{
    //get the raster attribute table and the size of the table
    IRaster2 raster = new IRaster2Proxy(rasterDataset.createDefaultRaster());
    ITable rasterTable = raster.getAttributeTable();
    if (rasterTable == null){
        return null;
    }
    int tableRows = rasterTable.rowCount(null);
    //create colors for each unique value
    IRandomColorRamp colorRamp = new RandomColorRamp();
    colorRamp.setSize(tableRows);
    colorRamp.setSeed(100);
    boolean createColorRamp[] = {
        false
    };
    colorRamp.createRamp(createColorRamp);
    if (createColorRamp[0] == false){
        return null;
    }
    //Create unique value renderer
    IRasterUniqueValueRenderer uvRenderer = new RasterUniqueValueRenderer();
    IRasterRenderer rasterRenderer = (IRasterRenderer)uvRenderer;
    rasterRenderer.setRasterByRef(rasterDataset.createDefaultRaster());
    rasterRenderer.update();
    //set renderer properties
    uvRenderer.setHeadingCount(1);
    uvRenderer.setHeading(0, "All Data Value");
    uvRenderer.setClassCount(0, tableRows);
    uvRenderer.setField("Value"); //or any other field in the table
    IRow row;
    ISimpleFillSymbol fillSymbol;
    for (int i = 0; i < tableRows; i++){
        row = rasterTable.getRow(i);
        uvRenderer.addValue(0, i, row.getValue(i)); // assuming the raster is 8 bit
        uvRenderer.setLabel(0, i, row.getValue(0).toString());
        fillSymbol = new SimpleFillSymbol();
        fillSymbol.setColor(colorRamp.getColor(i));
        uvRenderer.setSymbol(0, i, (ISymbol)fillSymbol);
    }
    return rasterRenderer;
}


See Also:

How to add a raster layer to a map




Development licensing Deployment licensing
ArcGIS for Desktop Standard ArcGIS for Desktop Advanced
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
Engine Developer Kit Engine