Tuesday, September 22, 2015

Read raster block by block or row by row using GDAL and C#

If you are creating a new project- set up GDAL & C# environment as described here


Code Snippet: Read raster block by block using GDAL and C#
 private static void ReadRasterBlocks(ref Dataset valueRaster)  
 {  
   Band bandValueRaster = valueRaster.GetRasterBand(1);  
     
   int rasterRows = valueRaster.RasterYSize;  
   int rasterCols = valueRaster.RasterXSize;  
     
   const int blockSize = 1024;  
   
   for(int row=0; row<rasterRows; row += blockSize)  
   {  
      int rowProcess;  
      if(row + blockSize < rasterRows)  
      {  
        rowProcess = blockSize;  
      }  
      else  
      {  
        rowProcess = rasterRows - row;  
      }  
   
      for(int col=0; col < rasterCols; col += blockSize)  
      {  
        int colProcess;  
        if(col + blockSize < rasterCols)  
        {  
           colProcess = blockSize;  
        }  
        else  
        {  
           colProcess = rasterCols - col;  
        }  
   
        double[] valueRasterValues = new double[rowProcess*colProcess];  
          
        bandValueRaster.ReadRaster(col, row, colProcess, rowProcess, valueRasterValues, colProcess,rowProcess, 0, 0);          
         
      }  
   }  
   
 }  

Code Snippet: Read raster row by row using GDAL and C#

 private static void ReadRasterRows(ref Dataset valueRaster)  
 {  
   Band bandValueRaster = valueRaster.GetRasterBand(1);  
     
   int rasterRows = valueRaster.RasterYSize;  
   int rasterCols = valueRaster.RasterXSize;  
   
   for(int row=0; row <rasterRows; row++)  
   {  
      double[] valueRasterValues = new double[rasterCols];  
      double[] zoneRasterValues = new double[rasterCols];  
   
      bandValueRaster.ReadRaster(0, row, rasterCols, 1, valueRasterValues, rasterCols, 1, 0, 0);  
        
   }  
 }  

C# , GDAL

0 comments :

Post a Comment

 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me