AE 遍历栅格实现栅格重分类(C#实现)

 

下面要讲的种重分类方法,网上很多。但是好像 System.Array pSafeArray = pPixelBlock.get_SafeArray(0) as System.Array;这一句一直报下面的错误。我还没有解决。

不过还是将这种方法整理一下,转载自此。

作者本人的初步的解决方法为:

pSafeArray.GetValue(x, y) 替换为 pPixelBlock.GetVal(0, c_x, r_y)。 同时避免了“System.Array pSafeArray = pPixelBlock.get_SafeArray(0) as System.Array”的异常问题。该问题很有可能为内部调用 MemoryStream 的 set_Capacity 时,在申请新内存时失败,可能是需要存储到ViewState中的内容太过庞大,或者可用内存太少。导致尝试将数据序列化写入ViewState时内存溢出。

 

 

 

下面为正文

--------------------------

栅格重分类方法很多,在AE中有多种方式可以实现,使用地图代数(在RasterModel中实现),或者IReclassOp,或者Geoprocessor的方式都可以,甚至可以遍历栅格来实现,这是最原始的方式,不过也可能是最实用的。这里使用的是最原始的遍历栅格的方式。

private void reclass(IRaster pRaster, float weight)  
{  
    IRasterProps rasterProps = (IRasterProps)pRaster;  
  
    //设置栅格数据起始点  
    IPnt pBlockSize = new Pnt();  
    pBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);  
  
    //选取整个范围  
    IPixelBlock pPixelBlock = pRaster.CreatePixelBlock(pBlockSize);  
  
    //左上点坐标  
    IPnt tlp = new Pnt();  
    tlp.SetCoords(0, 0);  
  
    //读入栅格  
    IRasterBandCollection pRasterBands = pRaster as  IRasterBandCollection;  
    IRasterBand pRasterBand = pRasterBands.Item(0);  
    IRawPixels pRawRixels = pRasterBands.Item(0) as IRawPixels;  
    pRawRixels.Read(tlp, pPixelBlock);  
  
    //将PixBlock的值组成数组  
    System.Array pSafeArray = pPixelBlock.get_SafeArray(0) as System.Array;  
    for (int y = 0; y < rasterProps.Height; y++)  
    {  
        for (int x = 0; x < rasterProps.Width; x++)  
        {  
            //int value = Convert.ToInt32(pSafeArray.GetValue(x, y));  
            Byte value = Convert.ToByte(pSafeArray.GetValue(x, y));  
            if (value != 0)  
                pSafeArray.SetValue((Byte)(value * weight), x, y);  
        }  
    }  
  
    pPixelBlock.set_SafeArray(0, pSafeArray);  
  
    //编辑raster,将更新的值写入raster中  
    IRasterEdit rasterEdit = pRaster as IRasterEdit;  
    rasterEdit.Write(tlp, pPixelBlock);  
    rasterEdit.Refresh();  
}  

 

改变RasterLayer中DEM的值

public void ChangePixelValue(double xMax, double xMin, double yMax, double yMin,double[,] PixelChanged)
{
    IRaster pRaster = thisRasterLayer.Raster;
    IRaster2 pRaster2 = pRaster as IRaster2;       
      
    //地图坐标转换为图中行列值
    rowMax = pRaster2.ToPixelRow(yMin);
    rowMin = pRaster2.ToPixelRow(yMax);
    columnMin = pRaster2.ToPixelColumn(xMin);
    columnMax = pRaster2.ToPixelColumn(xMax);
      
    int Height = rowMax - rowMin + 1;
    int Width = columnMax - columnMin + 1;
      
    //按照需要的大小建立一个空的PixelBlock3
    IPnt blocksize = new PntClass();
    blocksize.SetCoords(Width, Height);
      
    IPixelBlock3 pPixelBlock3 = pRaster.CreatePixelBlock(blocksize) as IPixelBlock3;
      
    System.Array pixels = (System.Array)pPixelBlock3.get_PixelData(0);
      
    //为新建的PixelBlock赋值
    try
    {
        for (int i = 0; i < Height; i++)
        {
            for (int j = 0; j < Width; j++)
            {
                pixels.SetValue(Convert.ToByte(PixelChanged[i,j]), j, i);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
      
    //把像素值赋予新建的PixelBlock3
    pPixelBlock3.set_PixelData(0, pixels);
      
    //PixelBlock3应在的位置
    blocksize.SetCoords(columnMin, rowMin);
      
    //改变的像素值写入图层
    IRasterEdit pRasterEdit = pRaster as IRasterEdit;
    pRasterEdit.Write(blocksize, (IPixelBlock)pPixelBlock3);
    pRasterEdit.Refresh();
      
    System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit);    
}
View Code

 

及将IRasterLayer存储起来的方法

public static void SaveRasterLayerTofile(IRasterLayer pRasterLayer, string fileName, string strFileExtension="TIFF")
 {

            IRaster pRaster = pRasterLayer.Raster;
            IRaster2 pRaster2 = pRaster as IRaster2;

            ISaveAs pSaveAs = pRaster2 as ISaveAs;
            pSaveAs.SaveAs(fileName, null, strFileExtension);
}

 

 

 

 
参考文章:
 
 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值