ArcObjects SDK 011 RasterLayer
图层的话,除了FeatureLayer外,用的最多的就是RasterLayer了。较FeatureLayer而言,RasterLayer比较简单,这点可以从栅格图层的属性对话框中可以看出。
其中General选项卡对应着RasterLayer继承实现的ILayerGeneralProperties接口,Source选项卡对应IRasterLayer的Ratser属性,Display选项卡对应着ILayerEffects接口,Symbology选项卡对应着IRasterLayer的Renderer属性。
IRasterLayer的Raster属性返回的是IRaster接口类型。其指的是实际的栅格数据源,我们一般存储为tif或者img文件。Raster类实现了IRaster接口,同时Raster还继承了以下接口。
其中通过IRaster可以分块读取栅格数据的像元信息,并可以设置重采样方式。IRaster2接口提供了一些像素与地图坐标相互转换的函数。IRasterBandCollection接口可以读取栅格数据包含的波段信息。IRasterEdit接口提供了修改栅格数据像元值的功能。IRasterProps提供了栅格数据的属性信息。ISaveAs接口提供了栅格数据的保存功能。
不过我们一般很少用IRaser接口去修改栅格数据,主要还是靠调用ArcToolbox里面的工具来处理。
该接口为栅格图层渲染接口,通过RasterLayer的Renderer可以获取或设置。继承该接口的类如下图所示。
其中我们常用的有RasterClassifyColorRampRenderer,分级别渲染,例如显示某个区域内的人口密度数据的栅格数据文件,就可以按照不同的颜色进行分段显示。
RasterUniqueValueRenderer,唯一值渲染,例如土地分类数据,就可以把耕地、林地、草地、城市用地等按照不同的颜色显示。
RasterStretchColorRampRenderer,拉伸渲染,一般我们显示Dem数据的时候都会使用这种渲染方式。
每种渲染方式如何设置,都可以参考ArcMap中的参数设置界面以及SDK帮助。
我们常用的栅格数据主要有tif和img格式。打开栅格数据有多种方法,我习惯用RasterLayer的CreateFromFilePath函数打开栅格数据,这这种方式比较简单。代码如下。
var myRasterLayer = new RasterLayerClass(); myRasterLayer.CreateFromFilePath(this.DEMFilePath);
public static IRasterDataset CreateRasterDataset(string Path, string FileName) { try { IRasterWorkspace2 rasterWs = OpenRasterWorkspace(Path); //Define the spatial reference of the raster dataset. ISpatialReference sr = new UnknownCoordinateSystemClass(); //Define the origin for the raster dataset, which is the lower left corner of the raster. IPoint origin = new PointClass(); origin.PutCoords(15.0, 15.0); //Define the dimensions of the raster dataset. int width = 100; //This is the width of the raster dataset. int height = 100; //This is the height of the raster dataset. double xCell = 30; //This is the cell size in x direction. double yCell = 30; //This is the cell size in y direction. int NumBand = 1; // This is the number of bands the raster dataset contains. //Create a raster dataset in TIFF format. IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(FileName, "TIFF", origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr, true); //If you need to set NoData for some of the pixels, you need to set it on band //to get the raster band. IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset; IRasterBand rasterBand; IRasterProps rasterProps; rasterBand = rasterBands.Item(0); rasterProps = (IRasterProps)rasterBand; //Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band. rasterProps.NoDataValue = 255; //Create a raster from the dataset. IRaster raster = rasterDataset.CreateFullRaster(); //Create a pixel block using the weight and height of the raster dataset. //If the raster dataset is large, a smaller pixel block should be used. //Refer to the topic "How to access pixel data using a raster cursor". IPnt blocksize = new PntClass(); blocksize.SetCoords(width, height); IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize)as IPixelBlock3; //Populate some pixel values to the pixel block. System.Array pixels; pixels = (System.Array)pixelblock.get_PixelData(0); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) if (i == j) pixels.SetValue(Convert.ToByte(255), i, j); else pixels.SetValue(Convert.ToByte((i * j) / 255), i, j); pixelblock.set_PixelData(0, (System.Array)pixels); //Define the location that the upper left corner of the pixel block is to write. IPnt upperLeft = new PntClass(); upperLeft.SetCoords(0, 0); //Write the pixel block. IRasterEdit rasterEdit = (IRasterEdit)raster; rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock); //Release rasterEdit explicitly. System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit); return rasterDataset; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return null; } } public static IRasterWorkspace2 OpenRasterWorkspace(string PathName) { //This function opens a raster workspace. try { IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass(); return workspaceFact.OpenFromFile(PathName, 0)as IRasterWorkspace2; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return null; } }
相关文章
- 76小时动捕,最大规模数字人多模态数据集开源
- 为什么我要迁移 SpringBoot 到函数计算
- 综述:自动驾驶的协同感知技术
- VideoMAE:简单高效的视频自监督预训练新范式
- 美国量子专家警告:2023年将是量子计算重要转折点,企业必须做好准备
- 百年微分方程难题被解决!神经元相互作用方式有了解析解描述,作者:可以模拟大脑动力学了 | MIT
- 一文看懂DRIVE Replicator:合成数据生成加速自动驾驶汽车的开发和验证
- 如何正确定义测试阶段训练?顺序推理和域适应聚类方法
- ChiQA-一个基于20万个真实用户问题的图片问答数据集
- 搞透Kafka的存储架构,用起来直接起飞!
- 同时采用边缘和云的四个理由
- 光纤通信速率破纪录!每秒能传1.84Pbit,2倍于全球互联网总流量
- 量子CNN对数据集的测试准确率高,但存在局限性
- 未来AI计算的方向,是「水芯片」?
- Nature 盘点诺奖历史,多位得主靠「计算科学」获殊荣
- IEEE Fellow 李学龙:多模态认知计算是实现通用人工智能的关键
- 你可以怀疑自动驾驶的安全性,但大数据告诉你:它确实比你开得好
- 无需下游训练,Tip-Adapter大幅提升CLIP图像分类准确率
- 存储和操作n维数据的难题,谷歌用一个开源软件库解决了
- 消息中间件该如何实现高可用架构