2011년 2월 26일 토요일

[ArcObjects] Creating a multipart color ramp

ArcMap 래스터 레이어에서 Multipart Color Ramp를 사용하는 Stretch Renderer를 ArcObjects를 사용하여 적용할 경우에 다음 단계를 따라하면 쉽게(?) 가능합니다.
 

▣ ArcMap Raster Stretch Renderer
▣ Style Manager - ColorRamps 
▣ MultiPart Color Ramp Properties 
3개( Red ~ Yellow, Yellow ~ Cyan, Cyan ~ Blue)의 Algorithmic Color Ramp로 구성

▣ Algorithmic Color Ramp
각 Algorithmic Color Ramp는 From Color ~ To Color로 구성 

▣ C# Code Snippet
private void AddRasterLayer()
{
      IMap targetMap = GetMap();
      IRasterLayer rasterLayer = CreateRasterLayer();

      rasterLayer.Renderer = CreateMultiPartColorRamp(rasterLayer.Raster);
      targetMap.AddLayer(rasterLayer);
}

private IRasterRenderer CreateMultiPartColorRamp(IRaster srcRaster)
{
      IRasterStretchColorRampRenderer stretchColorRamp = new RasterStretchColorRampRendererClass();
      IRasterRenderer rasterRenderer = (IRasterRenderer)stretchColorRamp;
      rasterRenderer.Raster = srcRaster;
      rasterRenderer.Update();

      // create multipart color ramp
      IMultiPartColorRamp mpColorRamp = new MultiPartColorRampClass();

      // using red, green blue index
      mpColorRamp.AddRamp(CreateAlgorithmicColorRamp(ToColor(255, 0, 0), ToColor(255, 255, 0)));
      mpColorRamp.AddRamp(CreateAlgorithmicColorRamp(ToColor(255, 255, 0), ToColor(0, 255, 255)));
      mpColorRamp.AddRamp(CreateAlgorithmicColorRamp(ToColor(0, 255, 255), ToColor(0, 0, 255)));

      // or using system color
      mpColorRamp.AddRamp(CreateAlgorithmicColorRamp(ToColor(Color.Red), ToColor(Color.Yellow)));
      mpColorRamp.AddRamp(CreateAlgorithmicColorRamp(ToColor(Color.Yellow), ToColor(Color.Cyan)));
      mpColorRamp.AddRamp(CreateAlgorithmicColorRamp(ToColor(Color.Cyan), ToColor(Color.Blue)));

      // create ramp
      mpColorRamp.Size = 255;
      bool sOK = false;
      mpColorRamp.CreateRamp(out sOK);

      stretchColorRamp.BandIndex = 0;
      stretchColorRamp.ColorRamp = mpColorRamp;

      rasterRenderer.Update();
      rasterRenderer.ResamplingType = rstResamplingTypes.RSP_BilinearInterpolation;

      return rasterRenderer;
}

private IColorRamp CreateAlgorithmicColorRamp(IColor fromColor, IColor toColor)
{
      IAlgorithmicColorRamp algColorRamp = new AlgorithmicColorRampClass();

      algColorRamp.Algorithm = esriColorRampAlgorithm.esriCIELabAlgorithm;
      algColorRamp.FromColor = fromColor;
      algColorRamp.ToColor = toColor;

      return algColorRamp;
}

private IRgbColor ToColor(int red, int green, int blue)
{
      IRgbColor rgbColor = new RgbColorClass();

      rgbColor.Red = red;
      rgbColor.Green = green;
      rgbColor.Blue = blue;
      rgbColor.UseWindowsDithering = true;

      return rgbColor;
}

private IRgbColor ToColor(System.Drawing.Color color)
{
      IRgbColor rgbColor = new RgbColorClass();

      rgbColor.RGB = System.Drawing.ColorTranslator.ToWin32(color);
      if (color == System.Drawing.Color.Transparent)
      {
          rgbColor.NullColor = true;
      }

      rgbColor.UseWindowsDithering = true;

      return rgbColor;
}