首页 > 其他分享 >ArcObjects SDK开发 011 RasterLayer

ArcObjects SDK开发 011 RasterLayer

时间:2022-12-05 23:34:50浏览次数:45  
标签:raster 011 RasterLayer 接口 dataset 栅格数据 ArcObjects pixel

1、RasterLayer的结构

图层的话,除了FeatureLayer外,用的最多的就是RasterLayer了。较FeatureLayer而言,RasterLayer比较简单,这点可以从栅格图层的属性对话框中可以看出。

image1.png

其中General选项卡对应着RasterLayer继承实现的ILayerGeneralProperties接口,Source选项卡对应IRasterLayer的Ratser属性,Display选项卡对应着ILayerEffects接口,Symbology选项卡对应着IRasterLayer的Renderer属性。

2、IRaster接口

IRasterLayer的Raster属性返回的是IRaster接口类型。其指的是实际的栅格数据源,我们一般存储为tif或者img文件。Raster类实现了IRaster接口,同时Raster还继承了以下接口。

image2.png

其中通过IRaster可以分块读取栅格数据的像元信息,并可以设置重采样方式。IRaster2接口提供了一些像素与地图坐标相互转换的函数。IRasterBandCollection接口可以读取栅格数据包含的波段信息。IRasterEdit接口提供了修改栅格数据像元值的功能。IRasterProps提供了栅格数据的属性信息。ISaveAs接口提供了栅格数据的保存功能。

不过我们一般很少用IRaser接口去修改栅格数据,主要还是靠调用ArcToolbox里面的工具来处理。

3、IRasterRenderer接口

该接口为栅格图层渲染接口,通过RasterLayer的Renderer可以获取或设置。继承该接口的类如下图所示。

image3.png

其中我们常用的有RasterClassifyColorRampRenderer,分级别渲染,例如显示某个区域内的人口密度数据的栅格数据文件,就可以按照不同的颜色进行分段显示。

RasterUniqueValueRenderer,唯一值渲染,例如土地分类数据,就可以把耕地、林地、草地、城市用地等按照不同的颜色显示。

RasterStretchColorRampRenderer,拉伸渲染,一般我们显示Dem数据的时候都会使用这种渲染方式。

每种渲染方式如何设置,都可以参考ArcMap中的参数设置界面以及SDK帮助。

4、打开栅格数据

我们常用的栅格数据主要有tif和img格式。打开栅格数据有多种方法,我习惯用RasterLayer的CreateFromFilePath函数打开栅格数据,这这种方式比较简单。代码如下。

var myRasterLayer = new RasterLayerClass();
myRasterLayer.CreateFromFilePath(this.DEMFilePath);

5、创建栅格数据文件

我们可以使用IWorkSpace来创建栅格数据文件。

public static IRasterDataset CreateRasterDataset(string Path, string FileName)
{
    try
    {
        IRasterWorkspace2 rasterWs = OpenRasterWorkspace(Path);
        //Define the spatial reference of the raster dataset.
        ISpatialReference sr = new UnknownCoordinateSystemClass();
        //Define the origin for the raster dataset, which is the lower left corner of the raster.
        IPoint origin = new PointClass();
        origin.PutCoords(15.0, 15.0);
        //Define the dimensions of the raster dataset.
        int width = 100; //This is the width of the raster dataset.
        int height = 100; //This is the height of the raster dataset.
        double xCell = 30; //This is the cell size in x direction.
        double yCell = 30; //This is the cell size in y direction.
        int NumBand = 1; // This is the number of bands the raster dataset contains.
        //Create a raster dataset in TIFF format.
        IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(FileName, "TIFF",
            origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr,
            true);
        //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;
        //Create a raster from the dataset.
        IRaster raster = rasterDataset.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 blocksize = new PntClass();
        blocksize.SetCoords(width, height);
        IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize)as IPixelBlock3;
        //Populate some pixel values to the pixel block.
        System.Array pixels;
        pixels = (System.Array)pixelblock.get_PixelData(0);
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
                if (i == j)
                    pixels.SetValue(Convert.ToByte(255), i, j);
                else
                    pixels.SetValue(Convert.ToByte((i * j) / 255), i, j);

        pixelblock.set_PixelData(0, (System.Array)pixels);
        //Define the location that the upper left corner of the pixel block is to write.
        IPnt upperLeft = new PntClass();
        upperLeft.SetCoords(0, 0);
        //Write the pixel block.
        IRasterEdit rasterEdit = (IRasterEdit)raster;
        rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock);
        //Release rasterEdit explicitly.
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
        return rasterDataset;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
        return null;
    }
}
public static IRasterWorkspace2 OpenRasterWorkspace(string PathName)
{
    //This function opens a raster workspace.
    try
    {
        IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass();
        return workspaceFact.OpenFromFile(PathName, 0)as IRasterWorkspace2;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
        return null;
    }
}

我们一般创建的时候,会调用ArcToolbox中的工具去创建。

标签:raster,011,RasterLayer,接口,dataset,栅格数据,ArcObjects,pixel
From: https://www.cnblogs.com/mytudousi/p/16953891.html

相关文章

  • 顺序表-00011-按位置删除元素,remove
    顺序表结构定义typedefintseqType; //定义顺序表数据类型//定义顺序表的结构体typedefstructt_sList{ seqType*pbase; //表基址 intcapacity; //表......
  • Ynoi2011题解
    d1t1初始化题意一个长度为\(n\)的序列,\(q\)次操作。询问\([l,r]\)的区间和。将所有\(\bmodx=y\)的下标位置加\(z\)。\(n,q\leq2\times10^5\)。题解......
  • Windows Live Messenger 2011,离线安装、多开、去广告……
    WindowsLiveMessenger2011,离线安装、多开、去广告……WindowsLiveEssentials2011发布之后,我就马上喜欢上了其中的​​WindowsLiveMesh​​​和​​WindowsLiveWri......
  • SAIF MBA2011年学费与资助政策发布公告
    亲爱的申请人:SAIFMBA2011年学费与资助政策现已通过学院网站发布。谨此公告。我院采用国际化办学模式,强调专业视角,MBA课程以“造就国际金融中心建设中坚,铺就迈向高端金......
  • ArcObjects SDK开发 009 Map-Layer的结构
    1、Map-Layer主干结构一个mxd文件可以包含多个地图,但我们常用的大部分都是包含一个地图。一个地图可以包含多个图层组和图层,而图层指向的则是实际数据。图层可以控制数据......
  • error: rpmdb: BDB0113 Thread/process 14536/140712790841152 failed: BDB1507 Threa
    yumremovedockererror:rpmdb:BDB0113Thread/process14536/140712790841152failed:BDB1507ThreaddiedinBerkeleyDBlibraryerror:db5error(-30973)fromdb......
  • 局域网无法连接打印机报错0x000011b解决办法
    局域网无法连接打印机报错0x000011b解决方法网上大部分都是说把KB50055XX的更新卸载掉,但是对于我来说好像没什么用(因为找不到这些更新补丁,所以没法卸载),经过实际操作解决方......
  • 每日食词—day011
    languagestandardn.语言标准correspondv.相当、相符、相符合、类似于.clickv. n.点击、单击logn. v.日志、记录.barriern.障碍、栅栏、屏障.......
  • 我的IT之路2011(一)
    2011对我来说注定是不平凡的一年,在这一年里我经历了挑战、失败、成功、离别、选择、犹豫、坚定......总之2011年在我身上发生了太多的事情,发生了重大的改变。包括我周围的......
  • ArcObjects SDK开发 008 从mxd地图文件说起
    1、Mxd文件介绍ArcGIS的地图文件为.mxd扩展名。Mxd文件的是有版本的,和ArcGIS的版本对应。可以在ArcMap中的File-SaveACopy,保存一个地图拷贝的时候选择Mxd文件的版本,支持......