基于AE+C#的栅格IDW插值的实现
查阅了许多参考资料,代码五花八门,能用的确少之又少,即便百般调试之下也会出些意料之外且无能为力的BUG,其中艰难不足为外人道也。但是最终仍是实现了插值的效果,于是写下这篇博文一是给有需求的小伙伴们一点参考,二是纪念下自己在这个点投入的并不对称的汗水和收获。。。。
在我这个系统中的IDW插值实现,主要可以分为两个部分,一是对目标点图层进行插值,二是对插值结果(栅格图层)进行栅格渲染(代码会在下面给出)。在这之前先给大家提供实现此功能所需添加的引用,每次参考哪些大神的博客时,都在在这个上面话不少功夫。
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using Microsoft.VisualBasic;
using ESRI.ArcGIS.GeoAnalyst;
进行插值时主要可分为以下几个步骤,一是创建目标点图层的要素图层,IDW插值的环境设置,包括像元大小、搜索半径等(如果程序在这里报错,建议将目标点图层在ARCMAP中先利用IDW插值功能运行一遍,如果能成功出图,记录下各类参数),代码如下:
#region 插值
IInterpolationOp pInterpolationOp;
pInterpolationOp = new RasterInterpolationOpClass();
IFeatureClass pFeatureClass;
IFeatureLayer pFeaturelayer;
pFeaturelayer = this.axMapControl1.Map.get_Layer(0) as IFeatureLayer;//点图层在axMapControl控件的第一个,索引为0
pFeatureClass = pFeaturelayer.FeatureClass;
IRasterRadius pRadius;
pRadius = new RasterRadiusClass();
object maxDistance = Type.Missing;
pRadius.SetVariable(12, ref maxDistance);//定义搜索半径,本例为12
IFeatureClassDescriptor pFCDescriptor;
pFCDescriptor = new FeatureClassDescriptorClass();
pFCDescriptor.Create(pFeatureClass, null, “value”);//设置进行插值的属性字段
object dCellSize = 113.039027413432;//设置像元值(参考ARCMAP)
IRasterAnalysisEnvironment pEnv;
pEnv = new RasterAnalysis();
pEnv = pInterpolationOp as IRasterAnalysisEnvironment;
pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref dCellSize);
object objectbarrier = Type.Missing;
IGeoDataset rasDataset;
rasDataset = pInterpolationOp.IDW((IGeoDataset)pFCDescriptor,
2, pRadius, ref objectbarrier);
IRaster pOutRaster;
pOutRaster = rasDataset as IRaster;
IRasterLayer pOutRasLayer;
pOutRasLayer = new RasterLayerClass();
pOutRasLayer.CreateFromRaster(pOutRaster);
this.axMapControl1.AddLayer(pOutRasLayer, 0);// 输出结果至axMapControl1,并放置在第一层
axMapControl1.ActiveView.Refresh();//刷新视图
DialogResult dr = MessageBox.Show(“插值成功,请选择是否进行分级渲染”, “分级渲染选择”,
MessageBoxButtons.OKCancel, MessageBoxIcon.Information);//建立一个文件对话框的实例,用于接收用户指令。
如果用户选择进行分级渲染,则进入一下代码模块:
if (dr == DialogResult.OK)
{
//用户选择确认的操作(分级渲染)
IRasterLayer pRasterLayer = axMapControl1.get_Layer(i) as IRasterLayer;//获取需要分级渲染的栅格图层,i为图层在axMapControl中的索引i
int number = Convert.ToInt32(Interaction.InputBox(“请输入栅格影像分类数量(默认为10)”,
“字符串”, “”, 500, 250)); //调用VB的输入框获取用户需要的分级层数,默认为10
if (number == 0)
number = 10;
funColorForRaster_Classify(pRasterLayer, number);
axMapControl1.ActiveView.Refresh();
}
funColorForRaster_Classify(IRasterLayer pRasterLayer,int number)是一个自定义的方法,用于为输入的栅格图层按照指定的分级数量进行分级渲染,其代码如下:
IRasterClassifyColorRampRenderer pRClassRend = new
RasterClassifyColorRampRenderer() as IRasterClassifyColorRampRenderer;
IRasterRenderer pRRend = pRClassRend as IRasterRenderer;
IRaster pRaster = pRasterLayer.Raster;//栅格图层转换为栅格图
IRasterBandCollection pRBandCol = pRaster as IRasterBandCollection;
IRasterBand pRBand = pRBandCol.Item(0);
if (pRBand.Histogram == null)
{
pRBand.ComputeStatsAndHist();
}
pRRend.Raster = pRaster;
pRClassRend.ClassCount = number;//设置分级层次,默认为10
pRRend.Update();
//设置初始渐变色和结束渐变色
MessageBox.Show(“请选择初始颜色”, “颜色选择”, MessageBoxButtons.OK, MessageBoxIcon.Information);
IRgbColor pFromColor = new RgbColor() as IRgbColor;
selectColor(pFromColor);//自定义的一个方法,用于打开颜色选取对话框,为IRgbColor类的实例赋值
MessageBox.Show(“请选择结束颜色”, “颜色选择”, MessageBoxButtons.OK, MessageBoxIcon.Information);
IRgbColor pToColor = new RgbColor() as IRgbColor;
selectColor(pToColor);
//进行栅格渲染并输出至axMapControl
IAlgorithmicColorRamp colorRamp = new AlgorithmicColorRamp() as
IAlgorithmicColorRamp;
colorRamp.Size = 10;
colorRamp.FromColor = pFromColor;
colorRamp.ToColor = pToColor;
bool createColorRamp;
colorRamp.CreateRamp(out createColorRamp);
IFillSymbol fillSymbol = new SimpleFillSymbol() as IFillSymbol;
for (int i = 0; i < pRClassRend.ClassCount; i++)
{
fillSymbol.Color = colorRamp.get_Color(i);
pRClassRend.set_Symbol(i, fillSymbol as ISymbol);
pRClassRend.set_Label(i,
pRClassRend.get_Break(i).ToString(“0.00”));
}
pRasterLayer.Renderer = pRRend;
IActiveView activeview = this.axMapControl1.ActiveView;
activeview.ContentsChanged();
//刷新窗口
activeview.PartialRefresh(esriViewDrawPhase.esriViewGeography, null,
null);
selectColor(IRgbColor)的代码如下:
//打开颜色选择对话框 并未IRGB类的三原色赋值
private void selectColor(IRgbColor selColor)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.AllowFullOpen = true;
colorDialog.FullOpen = true;
colorDialog.ShowHelp = true;
colorDialog.Color = Color.Black; //获取或设置用户所选定的颜色
colorDialog.ShowDialog();
selColor.Red = colorDialog.Color.R;
selColor.Green = colorDialog.Color.G;
selColor.Blue = colorDialog.Color.B;
MessageBox.Show(“您选择的各颜色分量值分别为” + “\nR:” + colorDialog.Color.R.ToString()
+ “\nG:” + colorDialog.Color.G.ToString() + “\nB:” + colorDialog.Color.B.ToString());
}
本次分享到此结束,希望对有相关需要的小伙伴们有所帮助。
转载自:https://blog.csdn.net/lemon_tttea/article/details/80545741