ArcEngine和GDAL读写栅格数据机制对比(二)—— IPixelBlock读写栅格
以下是设定一个矩形框,用IPixelBlock将256*256瓦片tile拼接成一个整块影像的代码,row1, col1, row2, col2是一个矩形框行列号范围。level是瓦片的金字塔等级。这里的瓦片已经下载完毕,位于domSavePath文件夹下。
//选择的Google瓦块的行列号范围
int row1, col1, row2, col2;
int nTileSize = ;
row1 = topLeft.Row;
col1 = topLeft.Col;
row2 = bottomRight.Row;
col2 = bottomRight.Col;
//拼接影像大小
int nImgSizeX = (col2 - col1 + ) * nTileSize;
int nImgSizeY = (row2 - row1 + ) * nTileSize; double leftlon = (((col1 * 20037508.343) * 2.0) / (Math.Pow(2.0, (double)level - 1.0))) - 20037508.343;
double toplat = 20037508.343 - (((row1 * 20037508.343) * 2.0) / (Math.Pow(2.0, (double)level - 1.0)));
double pixel = 40075016.686 / (nTileSize * Math.Pow(2.0, (double)level - 1.0));
//拼接图像的左上角点,WebMecator投影
IPoint origin = new PointClass();
origin.PutCoords(leftlon, toplat);
//创建拼接图像
IRasterDataset mergeRasterDs = CreateRasterDataset(domSavePath, "Full.tif", origin, nImgSizeX, nImgSizeY, pixel, pixel, ); for (int ii = row1; ii <= row2; ii++)
{
for (int jj = col1; jj <= col2; jj++)
{
//瓦片的名称
string tileName = ii.ToString().PadLeft(, '') + "_" + jj.ToString().PadLeft(, '') + "." + _fileEndExtent;
string FilePath = domSavePath + @"\" + tileName;
if (!File.Exists(FilePath))
{
continue;
}
//读取瓦片数据集
IRasterDataset tileRasterDs = OpenFileRasterDataset(domSavePath, tileName);
IRasterDataset2 tileRasterDs2 = tileRasterDs as IRasterDataset2;
IRaster tileRaster = tileRasterDs2.CreateFullRaster();
//设置瓦片像素快大小
IPnt tileBlockSize = new PntClass();
tileBlockSize.SetCoords(, );
IPixelBlock3 readPixelblock = tileRaster.CreatePixelBlock(tileBlockSize) as IPixelBlock3;
//瓦块的左上角点
IPnt tileTopleftCorner = new PntClass();
tileTopleftCorner.SetCoords(, );
tileRaster.Read(tileTopleftCorner, readPixelblock as IPixelBlock); //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; //从数据集中读取IRaster
IRasterDataset2 mergeRasterDs2 = mergeRasterDs as IRasterDataset2;
IRaster mergeRaster = mergeRasterDs2.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 blocksize2 = new PntClass();
blocksize2.SetCoords(, );
IPixelBlock3 writePixelblock = mergeRaster.CreatePixelBlock(tileBlockSize) as IPixelBlock3; System.Array pixelsTarget;
System.Array pixelsOrigin;//瓦块的像素坐标
for (int iplane = ; iplane < ; iplane++)
{
pixelsOrigin = (System.Array)readPixelblock.get_PixelData(iplane);
pixelsTarget = (System.Array)writePixelblock.get_PixelData(iplane);
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
object obj = pixelsOrigin.GetValue(i, j);
pixelsTarget.SetValue(obj, i, j);
}
}
writePixelblock.set_PixelData(iplane, (System.Array)pixelsOrigin);
}
//瓦块偏移左上角的像素值
int nOffsetX = (jj - col1) * nTileSize;
int nOffsetY = (ii - row1) * nTileSize;
//定义pixel block左上角点坐标,执行写入.
IPnt upperLeft = new PntClass();
upperLeft.SetCoords(nOffsetX, nOffsetY); //写入拼接影像中
IRasterEdit mergeRasterEdit = (IRasterEdit)mergeRaster;
mergeRasterEdit.Write(upperLeft, (IPixelBlock)writePixelblock); //释放mergeRasterEdit引用.
System.Runtime.InteropServices.Marshal.ReleaseComObject(mergeRasterEdit);
}
}
调用的CreateRasterDataset方法的代码如下:(这里注意:上面调用的时候出现了一个错误,Origin是左下角点坐标)
public static IRasterDataset CreateRasterDataset(string path, string fileName, IPoint origin, int width, int height, double xCell, double yCell, int NumBand)
{
try
{
IRasterWorkspace2 rasterWs = OpenRasterWorkspace(path);
//定义空间参考
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]]"; ISpatialReference sr = CreateWebMector();
if (sr == null)
{
sr = new UnknownCoordinateSystemClass();
}
IRasterDataset rasterDataset = null;
if (!File.Exists(string.Format(@"{0}\{1}", path, fileName)))
{
//创建TIFF格式栅格数据.
rasterDataset = rasterWs.CreateRasterDataset(fileName, "TIFF",
origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr,
true);
}
else
{
throw new ArgumentException("栅格数据已经存在");
}
return rasterDataset;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}
CreateRasterDataset
调用的OpenRasterWorkspace方法代码:
public static IRasterWorkspace2 OpenRasterWorkspace(string PathName)
{
//This function opens a raster workspace.
try
{
IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass();
return workspaceFact.OpenFromFile(PathName, ) as IRasterWorkspace2;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}
总结:
- 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.
- 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结构在里面
ArcEngine和GDAL读写栅格数据机制对比(二)—— IPixelBlock读写栅格的更多相关文章
- ArcEngine和GDAL读写栅格数据机制对比(一)
最近应用AE开发插值和栅格转等值线的程序,涉及到栅格读写的有关内容.联想到ArcGIS利用了GDAL的某些东西,从AE的OMD中也发现RasterDataset和RasterBand这些命名和GDAL ...
- [评测]低配环境下,PostgresQL和Mysql读写性能简单对比(欢迎大家提出Mysql优化意见)
[评测]低配环境下,PostgresQL和Mysql读写性能简单对比 原文链接:https://www.cnblogs.com/blog5277/p/10658426.html 原文作者:博客园--曲 ...
- 脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?
1.引言 本文接上篇<脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手>,继续脑残式的网络编程知识学习 ^_^. 套接字socket是大多数程序员都非常熟悉的概念,它是计算机 ...
- [转帖]脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?
脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么? http://www.52im.net/thread-1732-1-1.html 1.引言 本文接上篇<脑残式网 ...
- 基于Keepalived高可用集群的MariaDB读写分离机制实现
一 MariaDB读写分离机制 在实现读写分离机制之前先理解一下三种主从复制方式:1.异步复制:MariaDB默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给给客户端,并不关心从库 ...
- 浅谈:Redis持久化机制(二)AOF篇
浅谈:Redis持久化机制(二)AOF篇 上一篇我们提及到了redis的默认持久化方式RDB,是一种通过存储快照数据方式持久化的机制,它在宕机后会丢失掉最后一次更新RDB文件后的数据,这也是由于它 ...
- IM消息送达保证机制实现(二):保证离线消息的可靠投递
1.前言 本文的上篇<IM消息送达保证机制实现(一):保证在线实时消息的可靠投递>中,我们讨论了在线实时消息的投递可以通过应用层的确认.发送方的超时重传.接收方的去重等手段来保证业务层面消 ...
- Apache与Nginx对客户端请求的处理机制对比
Apache与Nginx对客户端请求的处理机制对比 模块 大致为四个模块,核心模块.HTTP模块.邮件模块,以及第三方模块 核心模块主要包含两类功能的支持,一类是主体功能,包括进程管理,权限管理,错误 ...
- Android Priority Job Queue (Job Manager):线程任务的容错重启机制(二)
Android Priority Job Queue (Job Manager):线程任务的容错重启机制(二) 附录文章4简单介绍了如何启动一个后台线程任务,Android Priority J ...
随机推荐
- php开源项目
论坛社区:Discuz.PHPWind.ThinkSAAS.phpBB CMS内容管理:DedeCMS.PHPCMS.帝国CMS.齐博CMS.Drupal 企业建站:CmsEasy.KingCMS.P ...
- JS Questions:Front-end Developer Interview Questions
Explain event delegation Event delegation allows us to attach a single event listener, to a parent e ...
- 页面静态化1 --- 概念(Apache内置压力测试工具使用方法)
三个概念 ①静态网址: http://127.0.0.1/regis.html ②动态网址:在请求页面时可以动态的传一些值进去. http://127.0.0.1/regis.php?id=45&am ...
- 浅谈XML
什么是 XML? · XML 指可扩展标记语言(EXtensible Markup Language) · XML 是一种标记语言,很类似 HTML · XML 的设计宗旨是传输数据,而非显示数据 · ...
- A BRIEF HISTORY OF COMPUTERS
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Vacuum Tubes Transi ...
- Support vector machine
https://en.wikipedia.org/wiki/Support_vector_machine In machine learning, support vector machines (S ...
- Java保留两位小数的几种做法
1. String类型数字始终保留两位小数 , RoundingMode.HALF_UP); return bd.toString(); } /** * 使用DecimalFormat,保留小数点后 ...
- Merge用法
Merge用来从一个表中选择一些数据更新或者插入到另一个表中.而最终是用更新还是用插入的方式取决于该语句中的条件. 下面我们简单的举一个例子: SQL> create table merge ...
- 转: PE rva to raw 虚拟偏移地址和文件物理偏移地址
+---------+---------+---------+---------+---------+---------+| 段名称 虚拟地址 虚拟大小 物理地址 物理大小 标志 |+-------- ...
- PE文件格式图示