博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ArcEngine中Raster数据的打开、属性读取等相关操作(完整版)
阅读量:3899 次
发布时间:2019-05-23

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

1、根据文件名添加栅格数据
根据文件名添加栅格数据主要是使用IRasterLayer接口,通过IRasterLayer接口的CreateFromFilePath方法从已知Raster数据的文件路径来创建一个IRasterLayer,然后将该对象添加到Map中即可。如下代码:
private void 添加栅格数据_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFile = new OpenFileDialog();
string fileName;
openFile.Title = "添加栅格数据";
openFile.Filter = "IMG图像(*.img)|*.img|TIFF图像(*.tif)|*.tif|所有文件(*.*)|*.*";
openFile.ShowDialog();
fileName = openFile.FileName;
IRasterLayer rasterLayer = new RasterLayerClass();
rasterLayer.CreateFromFilePath(fileName);
axMapControl1.AddLayer(rasterLayer, 0);
}
catch
{
MessageBox.Show("添加栅格数据错误!");
}
}
2、从数据集中添加
ArcGIS中最常用的文件型有GRID、TIFF、ERDAS IMAGE等,这几种栅格数据的工作空间也是所在的文件夹。打开栅格数据时需要使用栅格工作空间工厂(RasterWorkspaceFactory),然后再使用IRasterWorkspace接口的打开栅格数据集方法即可打开一个栅格数据集。在打开栅格数据集时,如果数据格式为是ESRI GRID,那么OpenRasterDataset()方法的参数为栅格要素集的名称,如果数据格式为TIFF格式,那么该方法的参数为完整的文件名,即要加上.tif扩展名,例如OpenRasterDataset("hillshade.tif")。下面代码为打开GRID格式的栅格数据:
private void 从数据集中添加Raster_Click(object sender, EventArgs e)
{
IWorkspaceFactory rasterWorkspaceFactory = new RasterWorkspaceFactoryClass();
IRasterWorkspace rasterWorkspace = (IRasterWorkspace)rasterWorkspaceFactory.OpenFromFile(@"E:/雅砻江/Data", 0);
IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset("newdem");
IRasterLayer rasterLayer = new RasterLayerClass();
rasterLayer.CreateFromDataset(rasterDataset);
axMapControl1.AddLayer(rasterLayer, 0);
}
3、获取Raster范围
本例中主要用到IRasterProps接口的Extent属性,如下图所示:
由于接口IRasterProps与接口IRaster都可以通过Raster类实现,Raster类所包含的接口如下如所示:
而IRaster又可以通过IRasterLayer接口的Raster属性来获取Raster对象,所以只需先通过IRasterLayer接口指定Raster图层。如下代码:
private void 获取Raster范围_Click(object sender, EventArgs e)
{
ILayer pLayer = axMapControl1.get_Layer(0);//假定第一个图层为Raster数据
IRasterLayer rasterLayer = (IRasterLayer)pLayer;
IRaster pRaster = rasterLayer.Raster;
IRasterProps pRasterProps = (IRasterProps)pRaster;
MessageBox.Show("Raster数据的范围为:/nXmax=" + pRasterProps.Extent.XMax.ToString()
+ "/nXmin=" + pRasterProps.Extent.XMin.ToString()
+ "/nYmax=" + pRasterProps.Extent.YMax.ToString()
+ "/nYmin=" + pRasterProps.Extent.YMin.ToString());
}
运行程序,其结果如下图所示:
4、读取Raster像素值
读取Raster像素值主要通过IRaster接口的Read方法在Raster上读取指定位置的像素块(PixelBlock),然后通过像素块的GetVal方法获取指定Band中位置的像素值。
首先我们来看一下IPixelBlock接口的GetVal方法,其语法如下:
public object GetVal( int plane, int X, int Y);
第一个参数为指定的Band序号,如果该Raster数据只有一个Band,则该处值为0;第二个参数和第三个参数分别用来指定获取像素块中哪一个位置的像素值,如果都为0,则表示获取像素块左上角处的像素值。如IPixelBlock.GetVal(0,0,0)。
IRaster接口具有下列方法和属性:
其中,CreateCursor方法是用来创建一个游标,CreatePixelBlock方法是根据指定像素块大小来创建一个像素块,Read方法则用于读取Raster中指定位置的像素块。
IRaster接口的Read方法具有两个参数,如下:
public void Read (IPnt tlc, IPixelBlock block);
第一个参数用于指定获取像素块的位置,第二个参数为像素块。值得注意的是,此处使用的指定获取像素块位置,其值的范围为0到Raster的行数和列数,而不是地图范围。举例说明,如一个Raster数据是由6000*6000个像素块组成,它所表示的地图范围为E95°~E100°、N30°~N35°,即每个像素块的大小为0.00008333°*0.00008333°,通过IRasterProps接口的MeanCellSize方法可以获取每个像素单元的大小。使用IRaster接口的Read方法时,指定获取像素块的位置是从0~6000,而不是E95°~E100°或N30°~N35°,所以,如果要获取指定位置的像素值大小,还需要将地图坐标转换成Raster数据中像素块的相对坐标。
如下代码:
private void 读取Raster像素值_Click(object sender, EventArgs e)
{
ILayer pLayer = axMapControl1.get_Layer(0);//假定第一个图层为Raster数据
IRasterLayer rasterLayer = (IRasterLayer)pLayer;
IRaster pRaster = rasterLayer.Raster;
IRasterProps pRasterProps = (IRasterProps)pRaster;
IPnt pnt = new PntClass();
pnt.SetCoords(10, 5);
IPnt pntSize = new PntClass();
pntSize.SetCoords(1, 1);
IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);
pRaster.Read(pnt, pixelBlock);
object obj = pixelBlock.GetVal(0, 0, 0);
MessageBox.Show(Convert.ToUInt32(obj).ToString());
}
其中,获取的像素块位置为(10, 5)。若要通过地图坐标转换成Raster相对坐标,首先需要确定Raster的插入点位置,在此以Raster的左上角为插入点,该点对应的地图坐标应为(IRasterProps.Extent.XMin,IRasterProps.Extent.YMax),其在Raster相对坐标为(0,0)。对于任一地图坐标(mapX,mapY),像素块大小为IRasterProps.MeanCellSize().X和IRasterProps.MeanCellSize().Y,由于地图坐标的Y方向与Raster相对坐标的方向相反,所以该地图坐标(mapX,mapY)对应于Raster中的相对坐标为:(mapX -IRasterProps.Extent.XMin)/IRasterProps.MeanCellSize().X和(IRasterProps.Extent.YMax - mapY)/IRasterProps.MeanCellSize().Y。如下代码,在Map控件的点击时间中添加获取Raster像素值的功能:
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1)
{
ILayer pLayer = axMapControl1.get_Layer(0);//假定第一个图层为Raster数据
IRasterLayer rasterLayer = (IRasterLayer)pLayer;
IRaster pRaster = rasterLayer.Raster;
IRasterProps pRasterProps = (IRasterProps)pRaster;
double mapPntX, mapPntY;
mapPntX = pRasterProps.Extent.XMin;
mapPntY = pRasterProps.Extent.YMax;
double cellSizeX, cellSizeY;
cellSizeX = pRasterProps.MeanCellSize().X;
cellSizeY = pRasterProps.MeanCellSize().Y;
IPnt pnt = new PntClass();
pnt.SetCoords((e.mapX - mapPntX) / cellSizeX, (mapPntY - e.mapY) / cellSizeY);
IPnt pntSize = new PntClass();
pntSize.SetCoords(1, 1);
IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);
pRaster.Read(pnt, pixelBlock);
object obj = pixelBlock.GetVal(0, 0, 0);
MessageBox.Show(Convert.ToUInt32(obj).ToString());

}

1、根据文件名添加栅格数据

根据文件名添加栅格数据主要是使用IRasterLayer接口,通过IRasterLayer接口的CreateFromFilePath方法从已知Raster数据的文件路径来创建一个IRasterLayer,然后将该对象添加到Map中即可。如下代码:
private void 添加栅格数据_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFile = new OpenFileDialog();
string fileName;
openFile.Title = "添加栅格数据";
openFile.Filter = "IMG图像(*.img)|*.img|TIFF图像(*.tif)|*.tif|所有文件(*.*)|*.*";
openFile.ShowDialog();
fileName = openFile.FileName;
IRasterLayer rasterLayer = new RasterLayerClass();
rasterLayer.CreateFromFilePath(fileName);
axMapControl1.AddLayer(rasterLayer, 0);
}
catch
{
MessageBox.Show("添加栅格数据错误!");
}
}
2、从数据集中添加
ArcGIS中最常用的文件型有GRID、TIFF、ERDAS IMAGE等,这几种栅格数据的工作空间也是所在的文件夹。打开栅格数据时需要使用栅格工作空间工厂(RasterWorkspaceFactory),然后再使用IRasterWorkspace接口的打开栅格数据集方法即可打开一个栅格数据集。在打开栅格数据集时,如果数据格式为是ESRI GRID,那么OpenRasterDataset()方法的参数为栅格要素集的名称,如果数据格式为TIFF格式,那么该方法的参数为完整的文件名,即要加上.tif扩展名,例如OpenRasterDataset("hillshade.tif")。下面代码为打开GRID格式的栅格数据:
private void 从数据集中添加Raster_Click(object sender, EventArgs e)
{
IWorkspaceFactory rasterWorkspaceFactory = new RasterWorkspaceFactoryClass();
IRasterWorkspace rasterWorkspace = (IRasterWorkspace)rasterWorkspaceFactory.OpenFromFile(@"E:/雅砻江/Data", 0);
IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset("newdem");
IRasterLayer rasterLayer = new RasterLayerClass();
rasterLayer.CreateFromDataset(rasterDataset);
axMapControl1.AddLayer(rasterLayer, 0);
}
3、获取Raster范围
本例中主要用到IRasterProps接口的Extent属性,如下图所示:
由于接口IRasterProps与接口IRaster都可以通过Raster类实现,Raster类所包含的接口如下如所示:
而IRaster又可以通过IRasterLayer接口的Raster属性来获取Raster对象,所以只需先通过IRasterLayer接口指定Raster图层。如下代码:
private void 获取Raster范围_Click(object sender, EventArgs e)
{
ILayer pLayer = axMapControl1.get_Layer(0);//假定第一个图层为Raster数据
IRasterLayer rasterLayer = (IRasterLayer)pLayer;
IRaster pRaster = rasterLayer.Raster;
IRasterProps pRasterProps = (IRasterProps)pRaster;
MessageBox.Show("Raster数据的范围为:/nXmax=" + pRasterProps.Extent.XMax.ToString()
+ "/nXmin=" + pRasterProps.Extent.XMin.ToString()
+ "/nYmax=" + pRasterProps.Extent.YMax.ToString()
+ "/nYmin=" + pRasterProps.Extent.YMin.ToString());
}
运行程序,其结果如下图所示:
4、读取Raster像素值
读取Raster像素值主要通过IRaster接口的Read方法在Raster上读取指定位置的像素块(PixelBlock),然后通过像素块的GetVal方法获取指定Band中位置的像素值。
首先我们来看一下IPixelBlock接口的GetVal方法,其语法如下:
public object GetVal( int plane, int X, int Y);
第一个参数为指定的Band序号,如果该Raster数据只有一个Band,则该处值为0;第二个参数和第三个参数分别用来指定获取像素块中哪一个位置的像素值,如果都为0,则表示获取像素块左上角处的像素值。如IPixelBlock.GetVal(0,0,0)。
IRaster接口具有下列方法和属性:
其中,CreateCursor方法是用来创建一个游标,CreatePixelBlock方法是根据指定像素块大小来创建一个像素块,Read方法则用于读取Raster中指定位置的像素块。
IRaster接口的Read方法具有两个参数,如下:
public void Read (IPnt tlc, IPixelBlock block);
第一个参数用于指定获取像素块的位置,第二个参数为像素块。值得注意的是,此处使用的指定获取像素块位置,其值的范围为0到Raster的行数和列数,而不是地图范围。举例说明,如一个Raster数据是由6000*6000个像素块组成,它所表示的地图范围为E95°~E100°、N30°~N35°,即每个像素块的大小为0.00008333°*0.00008333°,通过IRasterProps接口的MeanCellSize方法可以获取每个像素单元的大小。使用IRaster接口的Read方法时,指定获取像素块的位置是从0~6000,而不是E95°~E100°或N30°~N35°,所以,如果要获取指定位置的像素值大小,还需要将地图坐标转换成Raster数据中像素块的相对坐标。
如下代码:
private void 读取Raster像素值_Click(object sender, EventArgs e)
{
ILayer pLayer = axMapControl1.get_Layer(0);//假定第一个图层为Raster数据
IRasterLayer rasterLayer = (IRasterLayer)pLayer;
IRaster pRaster = rasterLayer.Raster;
IRasterProps pRasterProps = (IRasterProps)pRaster;
IPnt pnt = new PntClass();
pnt.SetCoords(10, 5);
IPnt pntSize = new PntClass();
pntSize.SetCoords(1, 1);
IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);
pRaster.Read(pnt, pixelBlock);
object obj = pixelBlock.GetVal(0, 0, 0);
MessageBox.Show(Convert.ToUInt32(obj).ToString());
}
其中,获取的像素块位置为(10, 5)。若要通过地图坐标转换成Raster相对坐标,首先需要确定Raster的插入点位置,在此以Raster的左上角为插入点,该点对应的地图坐标应为(IRasterProps.Extent.XMin,IRasterProps.Extent.YMax),其在Raster相对坐标为(0,0)。对于任一地图坐标(mapX,mapY),像素块大小为IRasterProps.MeanCellSize().X和IRasterProps.MeanCellSize().Y,由于地图坐标的Y方向与Raster相对坐标的方向相反,所以该地图坐标(mapX,mapY)对应于Raster中的相对坐标为:(mapX -IRasterProps.Extent.XMin)/IRasterProps.MeanCellSize().X和(IRasterProps.Extent.YMax - mapY)/IRasterProps.MeanCellSize().Y。如下代码,在Map控件的点击时间中添加获取Raster像素值的功能:
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)    
{
if (e.button ==
1)
{
ILayer pLayer = axMapControl1.get_Layer(
0);
//假定第一个图层为Raster数据
IRasterLayer rasterLayer = (IRasterLayer)pLayer;
IRaster pRaster = rasterLayer.Raster;
IRasterProps pRasterProps = (IRasterProps)pRaster;
double mapPntX, mapPntY;
mapPntX = pRasterProps.Extent.XMin;
mapPntY = pRasterProps.Extent.YMax;
double cellSizeX, cellSizeY;
cellSizeX = pRasterProps.MeanCellSize().X;
cellSizeY = pRasterProps.MeanCellSize().Y;
IPnt pnt =
new PntClass();
pnt.SetCoords((e.mapX - mapPntX) / cellSizeX, (mapPntY - e.mapY) / cellSizeY);
IPnt pntSize =
new PntClass();
pntSize.SetCoords(
1,
1);
IPixelBlock pixelBlock = pRaster.CreatePixelBlock(pntSize);
pRaster.Read(pnt, pixelBlock);
object obj = pixelBlock.GetVal(
0,
0,
0);
MessageBox.Show(Convert.ToUInt32(obj).ToString());
}
}
}
5、导出指定范围的Raster
导出指定范围的Raster主要通过IRasterLayerExport接口或者IExtractionOp接口来实现。下面先以IRasterLayerExport接口为例来介绍:
IRasterLayerExport接口中的属性和方法如下图所示:
其中,Export方法就是将Raster导出的函数,Extent为导出的Raster的范围,RasterLayer为要导出的Raster图层,SpatialReference为导出Raster的空间参考。
首先来看一下Export方法,如下所示:
public IRasterDataset Export (IWorkspace pWorkspace, string newname, string Format);
其中第一个参数pWorkspace为导出Raster所在的工作空间(即导出文件所在路径);第二个参数newname为导出Raster的文件名,该参数中必须包括导出数据类型的扩展名,如导出格式为img的Raster,它的名称为RasterOut,则参数newname应该为“RasterOut. img”;第三个参数为导出Raster的数据格式,如下:
格式
使用的名称(Format)
Imagine
"IMAGINE Image"TIFF"TIFF"GRID"GRID"JPEG"JPG"JP2000"JP2""BMP"PNG"PNG"GIF"GIF"PCI Raster"PIX"X11 Pixmap"XPM"PCRaster"MAP"Memory Raster"MEM"HDF4"HDF4"BIL"BIL"BIP"BIP"BSQ"BSQ"Idrisi Raster Format"RST"Geodatabase Raster"GDB"如下代码:
private      
void 导出指定范围的Raster_Click(
object sender, EventArgs e)
{
ILayer pLayer = axMapControl1.get_Layer(
0);
//假定第一个图层为Raster数据
IRasterLayer rasterLayer = (IRasterLayer)pLayer;
IRaster pRaster = rasterLayer.Raster;
IRasterProps pRasterProps = (IRasterProps)pRaster;
IRasterLayerExport rLayerExport =
new RasterLayerExportClass();
rLayerExport.RasterLayer = rasterLayer;
rLayerExport.Extent = pRasterProps.Extent;
//设置提取栅格数据的范围即为Raster数据的范围
rLayerExport.SpatialReference = pRasterProps.SpatialReference;
// 设置当前栅格数据的投影信息
IWorkspaceFactory pWF =
new RasterWorkspaceFactoryClass();
SaveFileDialog saveDG =
new SaveFileDialog();
saveDG.Title =
"导出Raster数据";
saveDG.Filter =
"Imagine(*.img)|*.img|"
+
"TIFF(*.tif)|*.tif|"
+
"GRID(*.*)|*.*|"
+
"JPEG(*.jpg)|*.jpg|"
+
"BMP(*.bmp)|*.bmp|"
+
"PNG(*.png)|*.png|"
+
"GIF(*.gif)|*.gif|"
+
"Geodatabase Raster(*.gdb)|*.gdb";
saveDG.ShowDialog();
string filePath =
"", fileName =
"", outputFormat=
"";
filePath = saveDG.FileName;
fileName = Microsoft.VisualBasic.Strings.Right(filePath, filePath.Length - filePath.LastIndexOf(
@"/") -
1);
filePath = Microsoft.VisualBasic.Strings.Left(filePath, filePath.LastIndexOf(
@"/"));
IWorkspace pRasterWorkspace = pWF.OpenFromFile(filePath,
0);
switch (saveDG.FilterIndex)
{
case
1:
outputFormat =
"IMAGINE Image";
break;
case
2:
outputFormat =
"TIFF";
break;
case
3:
outputFormat =
"GRID";
break;
case
4:
outputFormat =
"JPG";
break;
case
5:
outputFormat =
"BMP";
break;
case
6:
outputFormat =
"PNG";
break;
case
7:
outputFormat =
"GIF";
break;
case
8:
outputFormat =
"GDB";
break;
}
IRasterDataset outGeoDataset = rLayerExport.Export(pRasterWorkspace, fileName, outputFormat);
}
上面设置导出Raster的范围为Raster数据的范围,用IRasterLayerExport接口导出Raster数据时要求导出的范围(即Extent)为矩形。使用IExtractionOp接口可以导出圆形、多边形、矩形等范围的Raster数据,其属性和方法如下所示:

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

你可能感兴趣的文章
pytorch数据增强的具体细节
查看>>
pytorch专题 --- load模型
查看>>
VSCode编写C++代码从零开始
查看>>
ESC ubuntu16.04 ipv6配置
查看>>
visual studio 创建 C/C++静态库和动态库
查看>>
2021-05-26
查看>>
ubuntu中配置环境变量
查看>>
ubuntu安装weditor
查看>>
Ubuntu安装NVIDIA显卡驱动
查看>>
vue-cli中实现dolist
查看>>
sass的安装
查看>>
Vue-cli中路由配置
查看>>
豆瓣高分JAVA书籍,你都读过吗?
查看>>
java图书管理系统
查看>>
C#图书管理系统
查看>>
C#酒店管理系统
查看>>
你对ArrayList了解多少?
查看>>
《从Paxos到ZooKeeper分布式一致性原理与实践》学习知识导图
查看>>
Java基础面试题(一) (2020持续更新)
查看>>
JAVA人事管理系统
查看>>