Creating a raster unique value renderer
Do the following steps to create a raster unique value renderer:
- Get the raster attribute table and table size.
- Create colors for each unique value.
- Create a unique value renderer.
- Set the renderer properties.
See the following code example:
public IRasterRenderer UnqueValueRenderer(ESRI.ArcGIS.Geodatabase.IRasterDataset
rasterDataset)
{
try
{
//Get the raster attribute table and the size of the table.
IRaster2 raster=(IRaster2)rasterDataset.CreateDefaultRaster();
ITable rasterTable=raster.AttributeTable;
if (rasterTable == null)
{
return null;
}
int tableRows=rasterTable.RowCount(null);
//Create colors for each unique value.
IRandomColorRamp colorRamp=new RandomColorRampClass();
colorRamp.Size=tableRows;
colorRamp.Seed=100;
bool createColorRamp;
colorRamp.CreateRamp(out createColorRamp);
if (createColorRamp == false)
{
return null;
}
//Create a unique value renderer.
IRasterUniqueValueRenderer uvRenderer=new RasterUniqueValueRendererClass();
IRasterRenderer rasterRenderer=(IRasterRenderer)uvRenderer;
rasterRenderer.Raster=rasterDataset.CreateDefaultRaster();
rasterRenderer.Update();
//Set the renderer properties.
uvRenderer.HeadingCount=1;
uvRenderer.set_Heading(0, "All Data Value");
uvRenderer.set_ClassCount(0, tableRows);
uvRenderer.Field="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, Convert.ToByte(row.get_Value(1)));
// Assuming the raster is 8-bit.
uvRenderer.set_Label(0, i, Convert.ToString(row.get_Value(1)));
fillSymbol=new SimpleFillSymbolClass();
fillSymbol.Color=colorRamp.get_Color(i);
uvRenderer.set_Symbol(0, i, (ISymbol)fillSymbol);
}
return rasterRenderer;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}
[VB.NET] Public Function UnqueValueRenderer(ByVal rasterDataset As ESRI.ArcGIS.GeodataMyBase.IRasterDataset) As IRasterRenderer
Try
'Get the raster attribute table and the size of the table.
Dim raster As IRaster2=CType(rasterDataset.CreateDefaultRaster(), IRaster2)
Dim rasterTable As ITable=raster.AttributeTable
If rasterTable Is Nothing Then
Return Nothing
End If
Dim tableRows As Integer=rasterTable.RowCount(Nothing)
'Create colors for each unique value.
Dim colorRamp As IRandomColorRamp=New RandomColorRampClass()
colorRamp.Size=tableRows
colorRamp.Seed=100
Dim createColorRamp As Boolean
colorRamp.CreateRamp( createColorRamp)
If createColorRamp=False Then
Return Nothing
End If
'Create a unique value renderer.
Dim uvRenderer As IRasterUniqueValueRenderer=New RasterUniqueValueRendererClass()
Dim rasterRenderer As IRasterRenderer=CType(uvRenderer, IRasterRenderer)
rasterRenderer.Raster=rasterDataset.CreateDefaultRaster()
rasterRenderer.Update()
'Set the renderer properties.
uvRenderer.HeadingCount=1
uvRenderer.set_Heading(0, "All Data Value")
uvRenderer.set_ClassCount(0, tableRows)
uvRenderer.Field="Value" 'Or any other field in the table.
Dim row As IRow
Dim fillSymbol As ISimpleFillSymbol
Dim i As Integer
For i=0 To tableRows - 1 Step i + 1
row=rasterTable.GetRow(i)
Dim ' Assuming the raster is 8-bit As uvRenderer.AddValue(0,i,Convert.ToByte(row.get_Value(1))).
uvRenderer.set_Label(0, i, Convert.ToString(row.get_Value(1)))
fillSymbol=New SimpleFillSymbolClass()
fillSymbol.Color=colorRamp.get_Color(i)
uvRenderer.set_Symbol(0, i, CType(fillSymbol, ISymbol))
Next
Return rasterRenderer
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.Message)
Return Nothing
End Try
End Function
}
See Also:
How to create a raster layerTo use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
- ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)
- ESRI.ArcGIS.Display
- ESRI.ArcGIS.Geometry
- ESRI.ArcGIS.DataSourcesGDB
- ESRI.ArcGIS.Geodatabase
- ESRI.ArcGIS.DataSourcesRaster
- ESRI.ArcGIS.Carto
Development licensing | Deployment licensing |
---|---|
ArcGIS Desktop Standard | ArcGIS Desktop Advanced |
ArcGIS Desktop Standard | ArcGIS Desktop Standard |
ArcGIS Desktop Basic | ArcGIS Desktop Basic |
Engine Developer Kit | Engine |