ArcGIS Engine实现IDW空间插值

由于工作需要,要求使用AE实现对某些气象观测要素(如气温、雨量)等进行IDW插值,经过这段时间的努力,基本功能已经实现。在此感谢一些网上的技术牛人,谢谢他们无私的分享(搜索是件快乐的事情),同时也要感谢自己付出的努力(查找资料、耐心、智慧)。实现过程如下:

  1. IDW插值
  2. 剪裁(单波段影像)
  3. 颜色渲染
  4. 出透明图

下面记录了实现的主要代码,毕竟好记性不如烂笔头。

(一)IDW空间插值的实现

 1 private ESRI.ArcGIS.Geodatabase.IGeoDataset CreateRaster(string filedName, string pPath)
 2         {
 3             IMap pMap = this._mapControl.Map;
 4             IInterpolationOp3 pInterpolationOp = new RasterInterpolationOpClass(); //IInterpolationOp目前已被IInterpolationOp3所取代
 5             //定义工作空间
 6             IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
 7             string pFolder = System.IO.Path.GetDirectoryName(pPath);
 8             string pFileName = System.IO.Path.GetFileName(pPath);
 9             IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFolder, 0);  //设定地理处理的工作空间
10             IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
11             IFeatureClass oFeatureClass = pFeatureWorkspace.OpenFeatureClass(pFileName);
12             IFeatureClassDescriptor pFCDescriptor = new FeatureClassDescriptorClass();
13             pFCDescriptor.Create(oFeatureClass, null, filedName);
14             IRasterRadius pRadius = new RasterRadiusClass();     //设置搜索半径
15
16             object objectMaxDistance = null;
17             object objectbarrier = null;
18             object missing = Type.Missing;
19             pRadius.SetVariable(12, ref missing);  //这里设置不同的值,出图的效果(如颜色的深浅等)就不同
20
21             //ESRI.ArcGIS.Geodatabase.IWorkspace pShpWorkspace = pWorkspaceFactory.OpenFromFile(tempPath, 0);
22
23             object dCellSize = 0.013;   //设置栅格精度(输出栅格像元大小)
24             object snapRasterData = Type.Missing;
25             IRasterAnalysisEnvironment pEnv = pInterpolationOp as IRasterAnalysisEnvironment;
26             pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref dCellSize);
27             pEnv.OutSpatialReference = pMap.SpatialReference;
28             //执行插置,产生结果(栅格数据集)
29             IGeoDataset poutGeoDataset = pInterpolationOp.IDW((IGeoDataset)pFCDescriptor, 2, pRadius, ref missing);  //这里参数2为默认的权重值
30             //控制周围点对于内插值的重要性的距离指数。幂值越高,远数据点的影响会越小。
31             IRaster pOutRaster = poutGeoDataset as IRaster;
32
33             IRasterLayer pOutRasLayer = new RasterLayer();
34             pOutRasLayer.CreateFromRaster(pOutRaster);  //生成栅格数据图层
35             this._mapControl.AddLayer(pOutRasLayer);   //添加生成的图层
36
37             return poutGeoDataset;  //返回一个GeoDataSet
38
39         }

(二)shp图层剪裁栅格文数据

 1 private IRaster ShpLayerClipRaster(IFeatureLayer featureLayer, IRaster raster)
 2         {
 3             IFeatureLayer pFeaLyr;
 4             IRasterAnalysisEnvironment pEnv;
 5             IGeoDataset pTempDS;
 6             pFeaLyr = featureLayer;
 7             pTempDS = pFeaLyr.FeatureClass as IGeoDataset;
 8             IConversionOp pConOp = new RasterConversionOpClass();
 9             pEnv = pConOp as IRasterAnalysisEnvironment;
10             IRasterProps pProp;
11             pProp = raster as IRasterProps;
12             object cellSize = 0.01;
13             pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSize);
14             IRasterConvertHelper rsh = new RasterConvertHelperClass();
15             IRaster tempRaster;
16             tempRaster = rsh.ToRaster1(pTempDS, "Grid", pEnv);
17
18             IRaster pOutRaster;
19             IExtractionOp pExtrOp = new RasterExtractionOpClass();
20             pOutRaster = (IRaster)pExtrOp.Raster((IGeoDataset)raster, (IGeoDataset)tempRaster);
21
22             //pTempDS = null;
23             //pConOp = null;
24             //pFeaLyr = null;
25
26             return pOutRaster;
27         }

(三)栅格数据颜色分级渲染

实现思路如下:

1.定义渲染的一系列接口

2.判断图像是否建立了直方图,如果没有则进行创建

3.定义颜色序列,为渲染提供渲染的方案(自定义色带颜色包括透明效果色以及范围值区间)

4.调用Render方法进行渲染

  1 private void classifyColorToRaster(IRasterLayer pRasterLayer, int classCount, int size)
  2         {
  3             IRasterClassifyColorRampRenderer classifyRender = new RasterClassifyColorRampRenderer();
  4             IRasterRenderer rasterRender = classifyRender as IRasterRenderer;
  5
  6             IRaster pRaster = pRasterLayer.Raster;
  7             IRasterBandCollection pRBandCol = pRaster as IRasterBandCollection;
  8             IRasterBand pRBand = pRBandCol.Item(0);
  9
 10             if (pRBand.Histogram == null)   //判断栅格是否计算直方图,没有的话要进行计算
 11             {
 12                 pRBand.ComputeStatsAndHist();
 13             }
 14             rasterRender.Raster = pRaster;
 15             classifyRender.ClassCount = classCount;
 16
 17             for (int i = 0; i < classCount; i++)
 18             {
 19                 if (i == 0)
 20                 {
 21                     classifyRender.set_Break(i, double.Parse("1"));
 22                 }
 23                 if (i == 1)
 24                 {
 25                     classifyRender.set_Break(i, double.Parse("251"));
 26                 }
 27                 if (i == 2)
 28                 {
 29                     classifyRender.set_Break(i, double.Parse("501"));
 30                 }
 31                 if (i == 3)
 32                 {
 33                     classifyRender.set_Break(i, double.Parse("751"));
 34                 }
 35                 if (i == 4)
 36                 {
 37                     classifyRender.set_Break(i, double.Parse("1001"));
 38                 }
 39                 if (i == 5)
 40                 {
 41                     classifyRender.set_Break(i, double.Parse("1251"));
 42                 }
 43                 if (i == 6)
 44                 {
 45                     classifyRender.set_Break(i, double.Parse("1501"));
 46                 }
 47                 if (i == 7)
 48                 {
 49                     classifyRender.set_Break(i, double.Parse("1751"));
 50                 }
 51                 if (i == 8)
 52                 {
 53                     classifyRender.set_Break(i, double.Parse("2001"));
 54                 }
 55                 if (i == 9)
 56                 {
 57                     classifyRender.set_Break(i, double.Parse("2251"));
 58                 }
 59             }
 60
 61             rasterRender.Update();
 62             /*
 63             IRgbColor pFromColor = new RgbColor() as IRgbColor;
 64             pFromColor.Red = formColorRed;
 65             pFromColor.Green = formColorGreen;
 66             pFromColor.Blue = formColorBlue;
 67             IRgbColor pToColor = new RgbColor() as IRgbColor;
 68             pToColor.Red = toColorRed;
 69             pToColor.Green = toColorGreen;
 70             pToColor.Blue = toColorBlue;
 71             */
 72             IAlgorithmicColorRamp colorRamp = new AlgorithmicColorRamp() as IAlgorithmicColorRamp;
 73             colorRamp.Size = size;
 74             //colorRamp.FromColor = pFromColor;
 75             //colorRamp.ToColor = pToColor;
 76             bool createColorRamp;
 77
 78             colorRamp.CreateRamp(out createColorRamp);
 79
 80             IFillSymbol fillSymbol = new SimpleFillSymbol() as IFillSymbol;
 81             for (int i = 0; i < classifyRender.ClassCount; i++)
 82             {
 83                 if(i == 0)
 84                 {
 85                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
 86                     tc.Red = 255;
 87                     tc.Green = 255;
 88                     tc.Blue = 128;
 89                     fillSymbol.Color = tc as IColor;
 90                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
 91                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "250");  //这里设置0是要整数显示
 92                 }
 93                 if (i == 1)
 94                 {
 95                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
 96                     tc.Red = 128;
 97                     tc.Green = 255;
 98                     tc.Blue = 128;
 99                     fillSymbol.Color = tc as IColor;
100                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
101                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "500");  //这里设置0是要整数显示
102                 }
103                 if (i == 2)
104                 {
105                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
106                     tc.Red = 128;
107                     tc.Green = 255;
108                     tc.Blue = 255;
109                     fillSymbol.Color = tc as IColor;
110                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
111                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "750");  //这里设置0是要整数显示
112                 }
113                 if (i == 3)
114                 {
115                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
116                     tc.Red = 0;
117                     tc.Green = 128;
118                     tc.Blue = 255;
119                     fillSymbol.Color = tc as IColor;
120                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
121                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "1000");  //这里设置0是要整数显示
122                 }
123                 if (i == 4)
124                 {
125                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
126                     tc.Red = 255;
127                     tc.Green = 128;
128                     tc.Blue = 192;
129                     fillSymbol.Color = tc as IColor;
130                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
131                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "1250");  //这里设置0是要整数显示
132                 }
133                 if (i == 5)
134                 {
135                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
136                     tc.Red = 255;
137                     tc.Green = 0;
138                     tc.Blue = 0;
139                     fillSymbol.Color = tc as IColor;
140                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
141                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "1500");  //这里设置0是要整数显示
142                 }
143                 if (i == 6)
144                 {
145                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
146                     tc.Red = 255;
147                     tc.Green = 0;
148                     tc.Blue = 255;
149                     fillSymbol.Color = tc as IColor;
150                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
151                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "1750");  //这里设置0是要整数显示
152                 }
153                 if (i == 7)
154                 {
155                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
156                     tc.Red = 255;
157                     tc.Green = 128;
158                     tc.Blue = 64;
159                     fillSymbol.Color = tc as IColor;
160                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
161                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "2000");  //这里设置0是要整数显示
162                 }
163                 if (i == 8)
164                 {
165                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
166                     tc.Red = 0;
167                     tc.Green = 64;
168                     tc.Blue = 128;
169                     fillSymbol.Color = tc as IColor;
170                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
171                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "2250");  //这里设置0是要整数显示
172                 }
173                 if (i == 9)
174                 {
175                     ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
176                     tc.Red = 255;
177                     tc.Green = 255;
178                     tc.Blue = 0;
179                     fillSymbol.Color = tc as IColor;
180                     classifyRender.set_Symbol(i, fillSymbol as ISymbol);
181                     classifyRender.set_Label(i, classifyRender.get_Break(i).ToString("0") + "-" + "2500");  //这里设置0是要整数显示
182                 }
183             }
184             pRasterLayer.Renderer = rasterRender;
185             //*********************************使栅格图层呈现透明效果*******************************************
186             //ILayerEffects pLayerEffects = pRasterLayer as ILayerEffects;
187             //pLayerEffects.Transparency = 50;
188             //*************************************************************************************************
189
190
191             this._mapControl.AddLayer(pRasterLayer);
192             this._mapControl.ActiveView.Refresh();
193         }

(四)导出透明效果图片(PNG格式)

 1 private void ExportMapByAbsPath(IActiveView activeView, string path)     //导出透明效果图片
 2         {
 3             IExport export = new ExportPNGClass();
 4             IExportImage exportImg;
 5             IExportPNG exportPNG;
 6             int resolution = 96;
 7             export.Resolution = resolution;
 8             exportImg = export as IExportImage;
 9             exportImg.ImageType = esriExportImageType.esriExportImageTypeTrueColor;  //定义输出图片颜色模式为24位真彩色
10             exportPNG = export as IExportPNG;
11
12             //exportImg.BackgroundColor.RGB = exportPNG.TransparentColor.RGB;
13             ESRI.ArcGIS.Display.IRgbColor tc = new ESRI.ArcGIS.Display.RgbColor();
14             tc.Red = 255;
15             tc.Green = 255;
16             tc.Blue = 0;
17             exportImg.BackgroundColor = tc as IColor;   //设置输出图片的背景色
18             exportPNG.TransparentColor = tc as IColor;  //设置输出图片的透明色
19             tagRECT exportRect = activeView.ExportFrame;
20             IEnvelope envelop = new EnvelopeClass();
21             envelop.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom);
22             export.PixelBounds = envelop;
23             export.ExportFileName = path;
24             int hDC = export.StartExporting();
25             IEnvelope pVisbounds = null;
26             ITrackCancel ptrac = null;
27             //导出
28             activeView.Output(hDC, (int)export.Resolution, ref exportRect, pVisbounds, ptrac);
29             //结束导出
30             export.FinishExporting();
31             //清理导出类
32             export.Cleanup();
33         }

(五)调用上述方法

 1 private void exportMapBtn_Click(object sender, EventArgs e)    //实现了shp文件剪裁插值结果(栅格)
 2         {
 3             string filedName = "RES1_4M0_I";
 4             string pPath = System.Windows.Forms.Application.StartupPath + @"\china_basic_map\省会城市.shp";
 5             CreateRaster(filedName, pPath);
 6             IRasterLayer pRasterLayer = this._mapControl.get_Layer(0) as RasterLayer;
 7             IFeatureLayer featureLayer = this._mapControl.get_Layer(2) as IFeatureLayer;
 8             IRaster raster = pRasterLayer.Raster;
 9             IRaster resultRaster = ShpLayerClipRaster(featureLayer, raster);
10             IRasterLayer resultRasterLayer = new RasterLayer();
11             resultRasterLayer.CreateFromRaster(resultRaster);
12             classifyColorToRaster(resultRasterLayer, 10, 10);
13             //***************************该删除方式有待改善*************************************
14             this._mapControl.DeleteLayer(1);
15             this._mapControl.DeleteLayer(1);
16             this._mapControl.DeleteLayer(1);
17             //*********************************************************************************
18             ExportMapByAbsPath(this._mapControl.ActiveView, "d:\\abc.png");  //输出png格式图片
19             this._mapControl.DeleteLayer(0);
20         }

(五)目前任然存在的问题以及下一步的需求,后续有待解决

1.License偶尔报错(概率很小)
   2.自定义色带时,初始值设置为0时,颜色渲染有问题,只要比0大便不会出现此问题
   3.剪裁时需先调整范围,才能使剪裁范围最全面,效果最优
   4.清理缓存,提高运行效率
   5.Janus Form为试用版,最好要找到破解版
   6.代码优化(组织思维、重构)
   7.数据接入方式
   8.定时出图(分钟级、小时级、日级、周级、月级、旬级、年级)

时间: 2024-10-25 11:41:32

ArcGIS Engine实现IDW空间插值的相关文章

关于IDW空间插值

空间插值一般都会用到IInterPolationOP接口等 首先是通过图层的名称获取图层的方法: private ILayer GetLayerByName(string name)        {            ILayer layer = null;            for (int i = 0; i < axmapcontrol.Map.LayerCount; i++)            {                if (name == axmapcontrol

ArcGIS Engine开发之空间查询

空间查询功能是通过用户选择的空间几何体以及该几何体与当前地图中要素之间的几何关系进行空间查找,从而得到查询结果的操作. 相关类与接口 空间查询相关的类主要是SpatialFilter类,其实现的接口主要为ISpatialFilter接口.SpatialFilter类是空间关系过滤类,ISpatialFilter接口的成员主要用于返回和修改数据过滤器所使用的空间关系.ISpatialFilter接口同时包含了空间和属性两种查询约束,它继承了IQueryFilter接口. 1.Geometry属性:

ArcGIS Engine中空间参照(地理坐标)相关方法总结转

ArcGIS Engine中空间参照(地理坐标)相关方法总结 来自:http://blog.csdn.net/u011170962/article/details/38776101 1.创建空间参考 [csharp] view plain copy print? /// <summary> /// 根据prj文件创建空间参考 /// </summary> /// <param name="strProFile">空间参照文件</param>

ArcGIS Engine开发之旅09--几何对象和空间参考

1.Geometry  Geometry 是 GIS 中使用最为广泛的对象集之一,用户在创建.删除.编辑和进行地理分析的时候,就是处理一个包含几何形体的矢量对象:除了显示要素意外,控件对象选择,要素符号化,标注要素,编辑要素都需要 Geometry 参与.在 ArcGIS Engine 中,几个对象都有严格的定义,比如我们所说的直线,多断线等,于此同时 ArcGIS Engine 提供了而一个几何对象的模型图,如下: 2.Geometry 集合接口 通过前边对于具体的 Geometry 对象的介

ArcGIS Engine开发前基础知识(1)

ArcGIS二次开发是当前gis领域的一项重要必不可少的技能.下面介绍它的基本功能 一.ArcGIS Engine功能 在使用之前首先安装和部署arcgis sdk,(在这里不在赘述相关知识)可以实现的功能主要有地图的基本操作.信息查询.专题地图制作.数据编辑.网络分析.空间统计分析.三维分析等. 1.地图的基本操作 地图的基本操作主要包括加载矢量.栅格数据,浏览缩放地图,保存地图,在地图上显示文本注记,绘制点线面几何体等. 2.信息查询 信息查询主要通过矩形圆形或多边形来选中地图上的要素,或者

ArcGIS Engine开发前基础知识(4)

ArcGIS不同开发方式的比较 关于GIS应用软件的开发,通常有三种方式:C/S架构.网络GIS和移动GIS.ArcGIS平台提供了对三种开发方式的支持,对于采用从C/S架构的大多数开发者来讲,首先想到的是ArcGIS Engine进行开发.实际上,并不是所有的系统都必须采用这种方式,上述的三种开发方式(VBA.DLL和Add-in)在很多的时候也可以考虑. 作为VB的子集,VBA方式采用Visual Basic语言规范,简单易学,开发者只需要关注自己需要而ArcGIS没有直接提供的功能.对于广

ArcGIS Engine 编辑- ITask

转自原文ArcGIS Engine 编辑- ITask 下面的代码是我们定制的一个工作流-给等高线赋值 namespace EngineApplication { [Guid("5b0c0692-eaf7-4d64-9cee-c8c1afaf06f4")] [ClassInterface(ClassInterfaceType.None)] [ProgId("EditeTest.ContourTask")] public class CalculateContour

ArcGIS Engine开发前基础知识(2)

ArcGIS基本控件简介 ArcGIS Engine控件是一组可视化的开发组件,每个ArcGIS Engine控件都是一个COM组件.这些组件包括MapControl,PageLayoutControl,TOCControl.ToolbarControl,GlobeControl,和SceneControl等 MapControl控件 MapControl控件对应于ARCMap中的数据视图,主要用于显示.操作和分析地理数据.它封装了Map对象,可以加载已有的地图文档(*mxd文档)或者直接添加矢

ArcGIS Engine中的8种数据访问 (转)

数据是GIS的基础, 访问数据也是进行任何复杂的空间分析及空间可视化表达的前提.ArcGIS支持的数据格式比较丰富,对不同的数据格式支持的程度也有很大差异.本文主要介绍一下以下八种数据格式在ArcGIS Engine中如何访问.对ArcGIS桌面应用有一定了解的读者更适合阅读本文.本文的示例代码是用C#编写. 1.Shapefile 2.Coverage 3.Personal Geodatabase 4.Enterprise Geodatabase 5.Tin 6.Raster 7.CAD 8.