Raster数据的相关开发(一)

 

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中最常用的文件型有GRIDTIFFERDAS 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);

第一个参数用于指定获取像素块的位置,第二个参数为像素块。值得注意的是,此处使用的指定获取像素块位置,其值的范围为0Raster的行数和列数,而不是地图范围。举例说明,如一个Raster数据是由6000*6000个像素块组成,它所表示的地图范围为E95°~E100°、N30°~N35°,即每个像素块的大小为0.00008333°*0.00008333°,通过IRasterProps接口的MeanCellSize方法可以获取每个像素单元的大小。使用IRaster接口的Read方法时,指定获取像素块的位置是从06000,而不是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.XMinIRasterProps.Extent.YMax),其在Raster相对坐标为(00)。对于任一地图坐标(mapX,mapY),像素块大小为IRasterProps.MeanCellSize().XIRasterProps.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());

            }

        }

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值