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);
}

 

 

 

 
参考文章:
 
 
 
 
posted @ 2017-05-10 21:54  wenglabs  阅读(1955)  评论(0编辑  收藏  举报