栅格数据集的创建过程
创建栅格数据工作空间工厂类,使用API:IRasterWorkspaceFactory myWorkFact = new IRasterWorkspaceFactoryClass();
创建栅格数据工作空间:IRasterWorkspace myWorkspace = (IRasterWorkspace )myWorkFact .OpenFromFile(srcFileFolder, 0);//指定为目录项
工作空间导入栅格数据集:IRasterDataset rasterDataset = (IRasterDataset)myWorkspace.OpenRasterDataset(srcFileName);
创建栅格图层:
创建对象:IRasterLayer rasterLayer = new RasterLayerClass();
从栅格数据集中得到栅格图层:rasterLayer.CreateFromDataset(rasterDataset );
由栅格图层生成栅格属性表:IRasterProps rasterProps = rasterLayer.Raster as IRasterProps ;
由栅格数据集构建栅格波段集合:IRasterBandCollection pRasterBandCollection = rasterDataset as IRasterBandCollection;
栅格数据集的读取过程
单波段栅格数据读取过程(如果用于多波段,设置循环读取多次即可)
IRasterBand pRasterBand= pRasterBandCollection.Item(bandIndex);
创建元数据像素块:IRawPixels pRawPixel = pRasterBand as IRawPixels;
像素块设置:IPixelBlock pixelBlock; //这里定义的是一维像素块
像素块的尺寸:IPnt pixelBlockSize = new PntClass(); pixelBlockSize.SetCoords(rasterProps.width, rasterProps.Height);
设置读取起始位置:IPnt pOrignPt = new PntClass(); pOrignPt.SetCoords(0,0);//指定起始行列数
像素读取:pRawPixel.Read(pOrignPt, pixelBlock); //起始位置,像素块
给System.Array赋值:System.Array[] srcPixelArray = new System.Array[3]; srcPixelArray[bandIndex] = (pixelBlock as IPixelBlock3).get_PixelData(0);
注意:get_PixelData(0)前的像素块如果是继承字单个波段,则为一个二维像素块;如果继承自三个波段的栅格影像,则为三个二维像素块。因此,此处得到的数组srcPixelArray等价于==>int[] arraySize = {rasterProps.width, rasterProps.Height};System.Array srcArray = System.Array.CreateInstance(typeof(int), arraySize);
对于单/三波段栅格数据,像素值被读取并存储在srcPixelArray中;此处应注意在进行数据批量处理时,由于数据包含三个波段,需对数组的每个维度进行释放,释放完成后对数组的整体进行释放;示例代码:
for(int i = 0; i < 3; i++) { srcPixelArray[i] = null; } srcPixelArray = null;
三波段栅格数据读取过程(光标法获取三波段像素值)
设置读取起始位置:IPnt pOrignPt = new PntClass(); pOrignPt.SetCoords(0,0);
创建栅格数据:IRaster2 pRaster2 = rasterLayer.Raster as IRaster2;
创建栅格光标:IRasterCursor rasterCursor = pRaster2.CreateCursorEx(pOrignPt);//指定光标起始位置
创建栅格像素块:IPixelBlock3 pixelBlock3 = rasterCursor.PixelBlock as IPixelBlock3;
得到单波段像素数据:System.Array srcPixelArray = (pixelBlock as IPixelBlock3).get_PixelData(0) as System.Array;
栅格数据集的输出过程
创建栅格数据工作空间工厂类,使用API:IRasterWorkspaceFactory ouWorkFact = new IRasterWorkspaceFactoryClass();
创建栅格数据工作空间:IRasterWorkspace ouWorkspace = (IRasterWorkspace )myWorkFact .OpenFromFile(dstFileFolder, 0);//指定为目录项
指定起始像素:IPoint ouPointOrgin = new PointClass(); ouPointOrgin.PutCoords(rasterProps.Extend.LowerLeft.X, rasterProps.Extend.LowerLeft.Y);
创建输出栅格数据集
IRasterDataset2 ouRasterDataset = (IRasterDataset2)ouWorkspace.CreateRasterDataset(dstFileName, dataFormat, ouPointOrgin, rasterProps.width, rasterProps.Height, rasterProps.MeanCellSize().X, rasterProps.MeanCellSize().Y, bandCnt, rstPixelType.PT_UCHAR, new unkonwnCoordinateSystemClass(), true);
创建全波段栅格数据:IRaster ouRaster = ouRasterDataset.CreateFullRaster();
创建输出栅格图层:IRasterLayer ouRasterLayer = new RasterLayerClass(); ouRasterLayer .CreateFromDataset(ouRasterDataset);
创建输出像素块:IPixelBlock3 ouPixelBlock = ouRaster.CreatePixelBlock(pixelBlockSize) as IPixelBlock3; //需指定像素块的尺寸
为输出像素块赋值:ouPixelBlock.set_PixelData(bandIndex, srcPixelArray[bandIndex]);//指定参数为波段索引和该波段对应的像素数组
输出栅格数据
IRasterEdit rasterEdit = (IRasterEdit)ouRaster;
指定左上角起始位置:IPnt leftUp= new PntClass(); leftUp.SetCoords(0,0);
写入:rasterEdit.Write(leftUp, (IPixelBlock3 )ouPixelBlock);
数据清理:System.Runtime.InteropServices.Marshal.ReleaseComObject(IRasterEdit);