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 ...
随机推荐
- 2. Basic environment configuration
网卡设置: Controller Node # The loopback network interface auto lo iface lo inet loopback # The primar ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- easyui datagrid 列隐藏和显示
easyui datagrid 列隐藏和显示 用js怎么控制列的显示和隐藏? 最佳答案 $('#grid').datagrid('hideColumn','列field');把hideColu ...
- twitter storm源码走读之3--topology提交过程分析
概要 storm cluster可以想像成为一个工厂,nimbus主要负责从外部接收订单和任务分配.除了从外部接单,nimbus还要将这些外部订单转换成为内部工作分配,这个时候nimbus充当了调度室 ...
- 理解 Python 中的 *args 和 **kwargs
Python是支持可变参数的,最简单的方法莫过于使用默认参数,例如: def test_defargs(one, two = 2): print 'Required argument: ', one ...
- DNS:www.flickr.com
203.84.197.9 203.84.197.25 203.84.197.26 203.84.197.27
- NSQ部署
一. 简介 NSQ主要有三个主要程序和一个Web服务程序: nsqd:是守护进程,接收,缓存,并投递消息给客户端 nsqlookupd:是一个守护进程,为消费者提供运行时发现服务,来查找指定 ...
- web项目中 集合Spring&使用junit4测试Spring
web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...
- 使用第三方分页AspNetPager实现真正分页的SQL原理
AspNetPager是一个第三方分页第三方控件,可以和数据绑定控件(GridView等)方便的结合,实现真分页. 真分页:从数据库中获取符合要求的部分数目的记录.性能较高,数据量小,网络负载小,对数 ...
- VCL自带的TabControl真心不好用...
不是说功能, 而是指自绘能力, 开启OwnerDraw以后, 画是可以画了, 可是为啥每个Tab页头的边框不能变捏 只能是灰秃秃的, 感觉很不和谐 RZ的TabControl很强大, 可惜想用它需要带 ...