Tuesday, May 6, 2008

ArcGIS Programming Trick: Adding a Legend to a Raster Renderer

In the previous entry on creating a DTED mosaic in the FalconView ArcGIS editor, I touched on creating a RasterStretchColorRampRenderer to symbolize the output of the DTED mosaic. This post is a short extension of that entry. One detail that we discovered in implementing the renderer is that the color ramp did not draw on the table of contents legend. It turns out that there is a trick to getting the color ramp to draw. As far as I can tell, this trick is undocumented on EDN, but we learned it through the help of an ArcGIS guru at ESRI.

There are two things that need to happen for the legend to be created, neither of which we were doing. After instantiating the RasterStretchColorRampRenderer, it is important to associate the raster we are rendering with the renderer and to call Update on the renderer immediately thereafter. The code in the previous blog entry must be changed to look like this (the highlighted code is new):

// Create a renderer to be used with the raster layer

IRasterStretchColorRampRenderer pRasterStretchColorRampRenderer = new RasterStretchColorRampRendererClass();
IRasterRenderer pRasterRenderer = (IRasterRenderer)pRasterStretchColorRampRenderer;

pRasterRenderer.Raster = pRasterLayer.Raster;
pRasterRenderer.Update(); // the call of Update right after setting Raster is important

pRasterStretchColorRampRenderer.BandIndex = 0;

In this case pRasterLayer is created in the same way as before, except we moved the block of code that creates the layer earlier in the raster creation procedure.

1 comment:

nataly said...

It helped! Thanks a lot!