博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ArcEngine和GDAL读写栅格数据机制对比(二)—— IPixelBlock读写栅格
阅读量:6257 次
发布时间:2019-06-22

本文共 8799 字,大约阅读时间需要 29 分钟。

以下是设定一个矩形框,用IPixelBlock将256*256瓦片tile拼接成一个整块影像的代码,row1, col1, row2, col2是一个矩形框行列号范围。level是瓦片的金字塔等级。这里的瓦片已经下载完毕,位于domSavePath文件夹下。

1             //选择的Google瓦块的行列号范围 2             int row1, col1, row2, col2; 3             int nTileSize = 256; 4             row1 = topLeft.Row; 5             col1 = topLeft.Col; 6             row2 = bottomRight.Row; 7             col2 = bottomRight.Col; 8             //拼接影像大小 9             int nImgSizeX = (col2 - col1 + 1) * nTileSize;10             int nImgSizeY = (row2 - row1 + 1) * nTileSize;11 12             double leftlon = (((col1 * 20037508.343) * 2.0) / (Math.Pow(2.0, (double)level - 1.0))) - 20037508.343;13             double toplat = 20037508.343 - (((row1 * 20037508.343) * 2.0) / (Math.Pow(2.0, (double)level - 1.0)));14             double pixel = 40075016.686 / (nTileSize * Math.Pow(2.0, (double)level - 1.0));15             //拼接图像的左上角点,WebMecator投影16             IPoint origin = new PointClass();17             origin.PutCoords(leftlon, toplat);18             //创建拼接图像19             IRasterDataset mergeRasterDs = CreateRasterDataset(domSavePath, "Full.tif", origin, nImgSizeX, nImgSizeY, pixel, pixel, 3);20 21             for (int ii = row1; ii <= row2; ii++)22             {23                 for (int jj = col1; jj <= col2; jj++)24                 {25                     //瓦片的名称26                     string tileName = ii.ToString().PadLeft(8, '0') + "_" + jj.ToString().PadLeft(8, '0') + "." + _fileEndExtent;27                     string FilePath = domSavePath + @"\" + tileName;28                     if (!File.Exists(FilePath))29                     {30                         continue;31                     }32                     //读取瓦片数据集33                     IRasterDataset tileRasterDs = OpenFileRasterDataset(domSavePath, tileName);34                     IRasterDataset2 tileRasterDs2 = tileRasterDs as IRasterDataset2;35                     IRaster tileRaster = tileRasterDs2.CreateFullRaster();36                     //设置瓦片像素快大小37                     IPnt tileBlockSize = new PntClass();38                     tileBlockSize.SetCoords(256, 256);39                     IPixelBlock3 readPixelblock = tileRaster.CreatePixelBlock(tileBlockSize) as IPixelBlock3;40                     //瓦块的左上角点41                     IPnt tileTopleftCorner = new PntClass();42                     tileTopleftCorner.SetCoords(0, 0);43                     tileRaster.Read(tileTopleftCorner, readPixelblock as IPixelBlock);44 45                     //If you need to set NoData for some of the pixels, you need to set it on band 46                     //to get the raster band.47                     //IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;48                     //IRasterBand rasterBand;49                     //IRasterProps rasterProps;50                     //rasterBand = rasterBands.Item(0);51                     //rasterProps = (IRasterProps)rasterBand;52                     //Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band.53                     //rasterProps.NoDataValue = 255;54 55                     //从数据集中读取IRaster56                     IRasterDataset2 mergeRasterDs2 = mergeRasterDs as IRasterDataset2;57                     IRaster mergeRaster = mergeRasterDs2.CreateFullRaster();58 59                     //Create a pixel block using the weight and height of the raster dataset. 60                     //If the raster dataset is large, a smaller pixel block should be used. 61                     //Refer to the topic "How to access pixel data using a raster cursor".62                     IPnt blocksize2 = new PntClass();63                     blocksize2.SetCoords(256, 256);64                     IPixelBlock3 writePixelblock = mergeRaster.CreatePixelBlock(tileBlockSize) as IPixelBlock3;65 66                     System.Array pixelsTarget;67                     System.Array pixelsOrigin;//瓦块的像素坐标68                     for (int iplane = 0; iplane < 3; iplane++)69                     {70                         pixelsOrigin = (System.Array)readPixelblock.get_PixelData(iplane);71                         pixelsTarget = (System.Array)writePixelblock.get_PixelData(iplane);72                         for (int i = 0; i < 256; i++)73                         {74                             for (int j = 0; j < 256; j++)75                             {76                                 object obj = pixelsOrigin.GetValue(i, j);77                                 pixelsTarget.SetValue(obj, i, j);78                             }79                         }80                         writePixelblock.set_PixelData(iplane, (System.Array)pixelsOrigin);81                     }82                     //瓦块偏移左上角的像素值83                     int nOffsetX = (jj - col1) * nTileSize;84                     int nOffsetY = (ii - row1) * nTileSize;85                     //定义pixel block左上角点坐标,执行写入.86                     IPnt upperLeft = new PntClass();87                     upperLeft.SetCoords(nOffsetX, nOffsetY);88 89                     //写入拼接影像中90                     IRasterEdit mergeRasterEdit = (IRasterEdit)mergeRaster;91                     mergeRasterEdit.Write(upperLeft, (IPixelBlock)writePixelblock);92 93                     //释放mergeRasterEdit引用.94                     System.Runtime.InteropServices.Marshal.ReleaseComObject(mergeRasterEdit);95                 }96             }
调用的CreateRasterDataset方法的代码如下:(这里注意:上面调用的时候出现了一个错误,Origin是左下角点坐标)
1  public static IRasterDataset CreateRasterDataset(string path, string fileName, IPoint origin, int width, int height, double xCell, double yCell, int NumBand) 2         { 3             try 4             { 5                 IRasterWorkspace2 rasterWs = OpenRasterWorkspace(path); 6                 //定义空间参考 7                 string prj = "PROJCS[\"Popular Visualisation CRS / Mercator\",GEOGCS[\"Popular Visualisation CRS\",DATUM[\"Popular_Visualisation_Datum\",SPHEROID[\"Popular_Visualisation_Sphere\",6378137.0,0.0]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mercator_1SP\"],PARAMETER[\"false_easting\",0.0],PARAMETER[\"false_northing\",0.0],PARAMETER[\"central_meridian\",0.0],PARAMETER[\"scale_factor\",1.0],UNIT[\"Meter\",1.0]]"; 8  9                 ISpatialReference sr = CreateWebMector();10                 if (sr == null)11                 {12                     sr = new UnknownCoordinateSystemClass();13                 }14                 IRasterDataset rasterDataset = null;15                 if (!File.Exists(string.Format(@"{0}\{1}", path, fileName)))16                 {17                     //创建TIFF格式栅格数据.18                     rasterDataset = rasterWs.CreateRasterDataset(fileName, "TIFF",19                     origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr,20                     true);21                 }22                 else23                 {24                     throw new ArgumentException("栅格数据已经存在");25                 }26                 return rasterDataset;27             }28             catch (Exception ex)29             {30                 System.Diagnostics.Debug.WriteLine(ex.Message);31                 return null;32             }33         }
CreateRasterDataset

 调用的OpenRasterWorkspace方法代码:

1  public static IRasterWorkspace2 OpenRasterWorkspace(string PathName) 2         { 3             //This function opens a raster workspace. 4             try 5             { 6                 IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass(); 7                 return workspaceFact.OpenFromFile(PathName, 0) as IRasterWorkspace2; 8             } 9             catch (Exception ex)10             {11                 System.Diagnostics.Debug.WriteLine(ex.Message);12                 return null;13             }14         }
View Code

总结:

  • IRaster.CreatePixelBlock()  Allocates a PixelBlock of requested size.用这个获取特定大小的块
  • IRaster.CreateCursor Allocates a Raster Cursor for fast raster scanning.

The IRasterCursor interface controls enumeration through the PixelBlocks in a Raster. It is useful for rasters that are too large to be brought into 

memory at once.The RasterCursor divides the Raster into blocks 128 pixels high that span the full width of the raster. Each successive PixelBlock 

is read128 lines below the previous PixelBlock.To create a RasterCursor, use the IRaster::CreateCursor or IRaster2::CreateCursorEx method. 

RasterCursor 是AE默认的块大小读取
  • RawBlocks Raster pixels can be accessed through the IRasterEdit and IPixelBlock3 interfaces. These interfaces read and edit pixels on raster objects. The RawBlocks object, new at ArcGIS 10, works with pixels on a raster band. It reads pixels using an internal tiling structure and loops through the pixel blocks without resampling.  是有Tile结构在里面

 

转载地址:http://hmjsa.baihongyu.com/

你可能感兴趣的文章
selenium采用find_element_by方法识别页面元素
查看>>
***七牛跨域上传图片JS SDK
查看>>
LinqSelect
查看>>
如何解决"应用程序无法启动,因为应用程序的并行配置不正确"问题(转载)
查看>>
几种深度学习框架的使用和对比
查看>>
phpcms内容页替换
查看>>
黑客?普通程序员?有什么区别?
查看>>
视图和路由
查看>>
优酷新版播放器站外调用代码详解
查看>>
Hdoj 2059
查看>>
1077. Travelling Tours
查看>>
Python之操作符优先级
查看>>
【学习笔记】PHP会话控制
查看>>
面试题 17:合并两个排序的链表
查看>>
Linux命令--链接文件的那些事
查看>>
您对无法重新创建的表进行了更改或者启用了“阻止保存要求重新创建表的更改”选项...
查看>>
《梦断代码》读后感02
查看>>
java面试资料总结
查看>>
@SuppressWarnings
查看>>
oracle 表空间文件异常offline
查看>>