关于ArcGIS开发中设置栅格分级专题的说明

在ArcGIS的二次开发中我们可以通过接口“IRasterClassifyColorRampRenderer”设置栅格图层的分级专题显示。以下介绍了两种常用的栅格分级方法:使用ArcGIS内置的分级方法和自定义间断点的分级方法。

一、使用内置分级方法:
ArcGIS内置了“自然间断点”、“相等间隔”、“几何间隔”、“标准差”、“分位数”等分级方法。

// 定义要分级显示的栅格图层
IRasterLayer rasterLayer;
// 定义分级方法名称
string classifyMethod;
// 获取栅格数据
IRaster raster = rasterLayer.Raster;
IRasterBandCollection rasterBandCollection = raster as IRasterBandCollection;
IRasterBand rasterBand = rasterBandCollection.Item(0);
// 计算栅格统计直方图
if (rasterBand.Histogram == null)
{
    rasterBand.ComputeStatsAndHist();
}
// 创建分级方法对象
IClassify classify;
if (classifyMethod == "自然间断点")
{
    classify = new NaturalBreaksClass();
}
else if (classifyMethod == "相等间隔")
{
    classify = new EqualIntervalClass();
}
else if (classifyMethod == "几何间隔")
{
    classify = new GeometricalIntervalClass();
}
else if (classifyMethod == "标准差")
{
    classify = new StandardDeviationClass();
    // 设置偏差数据
    IRasterStatistics rasterStatistics = rasterBand.Statistics;
    IDeviationInterval deviationInterval = classify as IDeviationInterval;
    deviationInterval.Mean = rasterStatistics.Mean;
    deviationInterval.StandardDev = rasterStatistics.StandardDeviation;
    deviationInterval.DeviationInterval = 1;
}
else if (classifyMethod == "分位数")
{
    classify = new QuantileClass();
}
else
{
    classify = new NaturalBreaksClass();
}
// 设置分级数
int numClasses = 3;
classify.Classify(ref numClasses);
// 创建分级渲染对象
IRasterClassifyColorRampRenderer rasterClassifyColorRampRenderer = new RasterClassifyColorRampRendererClass();
// 设置分级方式
IRasterClassifyUIProperties rasterClassifyUIProperties = rasterClassifyColorRampRenderer as IRasterClassifyUIProperties;
rasterClassifyUIProperties.ClassificationMethod = classify.ClassID;
// 设置分级数
rasterClassifyColorRampRenderer.ClassCount = 3;
// 设置分级样式1
Color color = Color.FromArgb(255, 0, 0);
IFillSymbol fillSymbol = new SimpleFillSymbolClass();
IRgbColor rgbColor = new RgbColorClass();
rgbColor.Red = color.R;
rgbColor.Green = color.G;
rgbColor.Blue = color.B;
rgbColor.Transparency = color.A;
fillSymbol.Color = rgbColor;
rasterClassifyColorRampRenderer.set_Symbol(0, fillSymbol as ISymbol);
// 设置分级样式2
color = Color.FromArgb(0, 255, 0);
fillSymbol = new SimpleFillSymbolClass();
rgbColor = new RgbColorClass();
rgbColor.Red = color.R;
rgbColor.Green = color.G;
rgbColor.Blue = color.B;
rgbColor.Transparency = color.A;
fillSymbol.Color = rgbColor;
rasterClassifyColorRampRenderer.set_Symbol(1, fillSymbol as ISymbol);
// 设置分级样式3
color = Color.FromArgb(0, 0, 255);
fillSymbol = new SimpleFillSymbolClass();
rgbColor = new RgbColorClass();
rgbColor.Red = color.R;
rgbColor.Green = color.G;
rgbColor.Blue = color.B;
rgbColor.Transparency = color.A;
fillSymbol.Color = rgbColor;
rasterClassifyColorRampRenderer.set_Symbol(2, fillSymbol as ISymbol);
// 设置栅格图层分级专题
IRasterRenderer rasterRender = rasterClassifyColorRampRenderer as IRasterRenderer;
rasterLayer.Renderer = rasterRender;
// 设置分级标签,必须在应用栅格专题后设置
rasterClassifyColorRampRenderer.set_Label(0, "0");
rasterClassifyColorRampRenderer.set_Label(1, "1");
rasterClassifyColorRampRenderer.set_Label(2, "2");

通过以上方法设置“IClassify”和“IRasterClassifyColorRampRenderer”接口后,我们也可以继续设置需要分级的栅格数据,然后手动获取内置分级方法计算出来的间断点值:

// 设置需要分级的栅格数据
IRasterRenderer rasterRender = rasterClassifyColorRampRenderer as IRasterRenderer;
rasterRender.Raster = raster;
rasterRender.Update();
// 获取对应索引的间断点值
double value = rasterClassifyColorRampRenderer.get_Break(0);

获取了内置分级方法计算出来的间断点值后,我们可以再次使用自定义间断点方法去进行栅格分级。

二、自定义间断点方法:

// 定义要分级显示的栅格图层
IRasterLayer rasterLayer;
// 定义间断点值集合
IList<double> breakPoints;
// 定义分段颜色集合
IList<Color> colors;
// 定义分级标签
IList<string> labels;
// 创建分级渲染对象
IRasterClassifyColorRampRenderer rasterClassifyColorRampRenderer = new RasterClassifyColorRampRendererClass();
IRasterRenderer rasterRender = rasterClassifyColorRampRenderer as IRasterRenderer;
// 设置分级数
rasterClassifyColorRampRenderer.ClassCount = colors.Count;
// 设置分级间断点
for (int i = 0, length = breakPoints.Count; i < length; i++)
{
    rasterClassifyColorRampRenderer.set_Break(i, breakPoints[i]);
}
// 设置各分级样式
for (int i = 0, length = colors.Count; i < length; i++)
{
    Color color = colors[i];
    IFillSymbol fillSymbol = new SimpleFillSymbolClass();
    IRgbColor rgbColor = new RgbColorClass();
    rgbColor.Red = color.R;
    rgbColor.Green = color.G;
    rgbColor.Blue = color.B;
    rgbColor.Transparency = color.A;
    fillSymbol.Color = rgbColor;
    rasterClassifyColorRampRenderer.set_Symbol(i, fillSymbol as ISymbol);
}
// 设置栅格图层分级专题
rasterLayer.Renderer = rasterRender;
// 设置分级标签,必须在应用栅格分级专题后设置
for (int i = 0, length = labels.Count; i < length; i++)
{
    rasterClassifyColorRampRenderer.set_Label(i, labels[i]);
}

发表回复