zoukankan      html  css  js  c++  java
  • AE IRasterCursor 获取栅格图层像素值

    在编写使用栅格图层的代码时,常常要获取栅格图层的像素值(PixelValue)。如果想获取某一点的像素值,可以使用IRaster2中的getPixelValue方法。但如果想要获得的是图层中的某一块甚至整个图层,那么用getPixelValue方法就太过缓慢了。

    如果利用IRasterCursor、IPixelBlock3接口,从内存入手,速度就会加快很多。说一下我对他们的理解。首先应用IRaster2中的CreateCursorEx方法实现一个IRasterCursor接口。根据传入的参数,系统将为这个RasterCursor设置相应的PixelBlock。这就相当于将栅格图层分成了几个方块。接下来,只要应用IRasterCursor的next方法,便可以让这个方块移动,最后将整个图层遍历。这就是获取像素的过程了。

    另外,处理这部分数据时经常会弄混几个参数。不知道各位会不会有同样的经历。以下几个参数通常是想对应的:height, row, y, i;与其相对的是另外几个参数:width, column, x, j. 我是经常弄错,希望大家不会犯和我一样的错误。

    以下是我获取图层内像素值的代码。由于是DEM文件,只有一个波段。根据需要可以对其他波段或所有波段进行遍历。

     1 public Class_GetPixelValue(IRasterLayer pRasterLayer)
     2     {
     3         IRaster pRaster = pRasterLayer.Raster;
     4         IRaster2 pRaster2 = pRaster as IRaster2;
     5         IRasterProps pRasterProps = pRaster as IRasterProps;
     6    
     7         //获取图层的行列值   
     8         int Height = pRasterProps.Height;
     9         int Width = pRasterProps.Width;
    10    
    11         //定义并初始化数组,用于存储栅格内所有像员像素值
    12         PixelValue = new double[Height, Width];
    13         thisRasterLayer = pRasterLayer;
    14    
    15         System.Array pixels;
    16    
    17         //定义RasterCursor初始化,参数设为null,内部自动设置PixelBlock大小
    18         IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(null);
    19    
    20         //用于存储PixelBlock的长宽
    21         long blockwidth = 0;
    22         long blockheight = 0;
    23    
    24         IPixelBlock3 pPixelBlock3;
    25    
    26         try
    27         {
    28             do
    29             {
    30                 //获取Cursor的左上角坐标
    31                 int left = (int)pRasterCursor.TopLeft.X;
    32                 int top = (int)pRasterCursor.TopLeft.Y;
    33    
    34                 pPixelBlock3 = pRasterCursor.PixelBlock as IPixelBlock3;
    35    
    36                 blockheight = pPixelBlock3.Height;
    37                 blockwidth = pPixelBlock3.Width;
    38                 //pPixelBlock3.Mask(255);
    39    
    40                 pixels = (System.Array)pPixelBlock3.get_PixelData(0);
    41    
    42                 //获取该Cursor的PixelBlock中像素的值
    43                 for (int i = 0; i < blockheight; i++)
    44                 {
    45                     for (int j = 0; j < blockwidth; j++)
    46                     {
    47                         //一定要注意,pixels中的数组排序为[Width,Height]
    48                         PixelValue[top + i, left + j] = Convert.ToDouble(pixels.GetValue(j, i));
    49                     }
    50                 }
    51             }
    52             while (pRasterCursor.Next() == true);
    53    
    54             MessageBox.Show("完成遍历!");
    55         }
    56         catch(Exception ex)
    57         {
    58             MessageBox.Show(ex.Message);
    59         }            
    60     }
  • 相关阅读:
    EcShop二次开发学习方法
    [ 产品经理 ] 互联网产品经理常用软件及工作平台
    Tengine – Nginx衍生版
    把PHP大牛记下来,方便以后关注
    看了极光推送技术原理的几点思考
    centos磁盘满了,查找大文件并清理
    LNMP一键安装包 PHP自动升级脚本
    微信红包系统设计 & 优化
    程序员每天每周每月每年该做的事
    php中$_REQUEST、$_POST、$_GET的区别和联系小结
  • 原文地址:https://www.cnblogs.com/qiernonstop/p/3700590.html
Copyright © 2011-2022 走看看