Wednesday, September 16, 2015

Read a raster file into an Array using C# and GDAL

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

Code snippet: Read raster into an Array using GDAL and C#

 //Register all drivers  
 //Read dataset  
 Dataset rasterDataset = Gdal.Open("rasterName.tif", Access.GA_ReadOnly);  
 if (rasterDataset == null)  
      Console.WriteLine("Unable to read input raster..");  
 //raster bands  
 int bandCount = rasterDataset.RasterCount;  
 if (bandCount > 1)  
      Console.WriteLine("Input error, please provide single band raster image only..");  
 //raster size  
 int rasterCols = rasterDataset.RasterXSize;  
 int rasterRows = rasterDataset.RasterYSize;  
 //Extract geotransform  
 double[] geotransform = new double[6];  
 //Get raster bounding box  
 double originX = geotransform[0];  
 double originY = geotransform[3];  
 double pixelWidth = geotransform[1];  
 double pixelHeight = geotransform[5];  
 //Read 1st band from raster  
 Band band = rasterDataset.GetRasterBand(1);  
 int rastWidth = rasterCols;  
 int rastHeight = rasterRows;  
 //It works but needed to find out algorithm to read memory block by block instead of reading a big chunk of data  
 double[] rasterValues;  
 rasterValues = new double[rastWidth * rastHeight];  
 band.ReadRaster(0, 0, rastWidth, rastHeight, rasterValues, rastWidth, rastHeight, 0, 0);  

Read raster block by block or row by row : read raster block using GDAL

C# , GDAL , Raster


Post a Comment


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

About Me