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 ...
随机推荐
- debug阶段工作期站立会议1
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- NOJ 1641 错误的算法(模拟)
[1641] 错误的算法 时间限制: 5000 ms 内存限制: 65535 K 问题描述 有道题目是这样的: 输入一个 n 行 m 列网格,找一个格子,使得它所在的行和列中所有格子的数之和最大.如果 ...
- optimize table table_name myisam mysql自动清除删除过留下的空记录
optimize table table_name 这个可以清除你表里面的空记录,每次清除的时候记得锁表 lock tables table_name write|read; unlock tabl ...
- Apache Spark源码走读之10 -- 在YARN上运行SparkPi
y欢迎转载,转载请注明出处,徽沪一郎. 概要 “spark已经比较头痛了,还要将其运行在yarn上,yarn是什么,我一点概念都没有哎,再怎么办啊.不要跟我讲什么原理了,能不能直接告诉我怎么将spar ...
- SharedPreferences封装类SPUtils
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.uti ...
- CentOS安装JAVA后JAVA版本不对的问题
今天用CentOS安装JDK,发觉在安装完成后,输入java命令来验证是否安装成功时,出现 Usage: gij [OPTION] ... CLASS [ARGS] ... to i ...
- startActivityForResult的用法和demo
[转]startActivityForResult的用法和demo 博客分类: java 移动开发 有时候我们需要把A activity提交数据给B activity处理,然后把结果返回给A 这种 ...
- 【转】Warning: mysql_connect(): mysqlnd cannot connect to MySQL 4.1+ using the old insecure authenticat
Warning: mysql_connect(): mysqlnd cannot connect to MySQL 4.1+ using the old insecure authenticat 当m ...
- your project contains error(s),please fix them before running your application.错误总结
Android开发中的问题总是多种多样,今天我来总结一个浪费了我一个晚上的错误T-T:your project contains error(s),please fix them b ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(2)
相关下载:http://download.csdn.net/detail/xgbing/9565708 首先看的示例是心率计一个示例程序:<KEIL path> \ARM\Device\N ...