Friday, September 11, 2015

Vector to Raster Conversion using GDAL & C#

I have been working on a .Net based project that requires feature to raster conversion without using ESRI ArcObjects. The GDAL seem's an obvious solution to us and wrote a small snippet for rasterize layer using Gdal and C#. Hope it will help someone someday...

Step 1: Setup the environmental variables for GDAL 
OR 
(Copied here for future reference - )

The Geospatial Data Abstraction Library (GDAL) is great if you want to process raster data, especially regarding format conversion. I want to use GDAL for a biodiversity modelling project, so I had a look at the C#-bindings of GDAL. The described steps work both with VS 2010 as well as VS 2012, what you need to do is:
  • Download the latest version of the precompiled GDAL binaries from here. Choose the ones that suit your system (32bit or 64bit). Extract the contents from the zip file to a location on your hard disk e.g. C:\Program Files\GDAL (I ran into a number of AccessViolationExceptions when I used the binaries from FWTools 2.4.7).
  • Include both the path to C:\Program Files\GDAL\bin\gdal\csharp as well as C:\Program Files\GDAL\bin in your PATH system variable.
Setting of the PATH variable
Creating a Console Application (VS 2010)
  • Add four of.the dll-files that can can be found at C:\Program Files\GDAL\bin\gdal\csharp (or wherever your installation path of the current binaries is) to your project references: gdal_csharp.dllgdalconst_csharp.dllogr_csharp.dll and osr_csharp.
Adding necessary references (VS 2010)
  • Build the solution. If you are on a 64bit system and you are using the 64bit GDAL binaries you have to make sure you actually build for 64bit (you will get errors otherwise when you try to run the program).
Setting of the platform target for 64bit (VS 2012)
  • Now you can run the program with some data. Include a reference to one of the raster files in the Command line arguments field of your Debug options (in the properties of your GDALInfo project; don't forget to put the path inside double quotes of it includes blanks).
  • Run the program (Ctrl-F5). It should show you something similar to the following:
Output of GDALInfo
That's it. Now you can use the other example C#-programs to open, copy, manipulate raster data in variety of file formats. For some of the other example files it is necessary to add some additional references (e.g. System.Drawing).
(End copy)

Step 2: Open visual studio and create C# console project

Import references 
  • gdal_csharp
  • gdalconst_csharp
  • ogr_csharp
  • osr_csharp
Import Namespaces

using OSGeo.GDAL;
using OSGeo.OGR;
using OSGeo.OSR;
Step 3: Code snippet
 public void Rasterize(string inputFeature, string outRaster, string fieldName, int cellSize)  
     {  
       // Define pixel_size and NoData value of new raster  
       int rasterCellSize = cellSize;  
       const double noDataValue = -9999;  
       string outputRasterFile = outRaster;  
       //Register the vector drivers  
       Ogr.RegisterAll();  
       //Reading the vector data  
       DataSource dataSource = Ogr.Open(inputFeature, 0);  
       Layer layer = dataSource.GetLayerByIndex(0);  
       Envelope envelope = new Envelope();  
       layer.GetExtent(envelope, 0);  
       //Compute the out raster cell resolutions  
       int x_res = Convert.ToInt32((envelope.MaxX - envelope.MinX) / rasterCellSize);  
       int y_res = Convert.ToInt32((envelope.MaxY - envelope.MinY) / rasterCellSize);  
       Console.WriteLine("Extent: " + envelope.MaxX + " " + envelope.MinX + " " + envelope.MaxY + " " + envelope.MinY);  
       Console.WriteLine("X resolution: " + x_res);  
       Console.WriteLine("X resolution: " + y_res);  
       //Register the raster drivers  
       Gdal.AllRegister();  
       //Check if output raster exists & delete (optional)  
       if (File.Exists(outputRasterFile))  
       {  
         File.Delete(outputRasterFile);  
       }  
       //Create new tiff   
       OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff");  
       Dataset outputDataset = outputDriver.Create(outputRasterFile, x_res, y_res, 1, DataType.GDT_Float64, null);  
       //Extrac srs from input feature   
       string inputShapeSrs;  
       SpatialReference spatialRefrence = layer.GetSpatialRef();  
       spatialRefrence.ExportToWkt(out inputShapeSrs);  
       //Assign input feature srs to outpur raster  
       outputDataset.SetProjection(inputShapeSrs);  
       //Geotransform  
       double[] argin = new double[] { envelope.MinX, rasterCellSize, 0, envelope.MaxY, 0, -rasterCellSize };  
       outputDataset.SetGeoTransform(argin);  
       //Set no data  
       Band band = outputDataset.GetRasterBand(1);  
       band.SetNoDataValue(noDataValue);  
       //close tiff  
       outputDataset.FlushCache();  
       outputDataset.Dispose();  
       //Feature to raster rasterize layer options  
       //No of bands (1)  
       int[] bandlist = new int[] { 1 };  
       //Values to be burn on raster (10.0)  
       double[] burnValues = new double[] { 10.0 };  
       Dataset myDataset = Gdal.Open(outputRasterFile, Access.GA_Update);  
       //additional options  
       string[] rasterizeOptions;  
       //rasterizeOptions = new string[] { "ALL_TOUCHED=TRUE", "ATTRIBUTE=" + fieldName }; //To set all touched pixels into raster pixel  
       rasterizeOptions = new string[] { "ATTRIBUTE=" + fieldName };  
       //Rasterize layer  
       //Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, null, null, null); // To burn the given burn values instead of feature attributes  
       Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion");  
     }  
     private static int ProgressFunc(double complete, IntPtr message, IntPtr data)  
     {  
       Console.Write("Processing ... " + complete * 100 + "% Completed.");  
          if (message != IntPtr.Zero)  
          {  
         Console.Write(" Message:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(message));  
          }  
          if (data != IntPtr.Zero)  
          {  
         Console.Write(" Data:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(data));  
          }  
       Console.WriteLine("");  
       return 1;  
     }  

Step 4: Method call

 Rasterize("inputShapeFileName", "outRasterName", "fieldName_to_display_values_on_raster", rasterCellSize)  

ArcGIS , C# , GDAL

0 comments :

Post a Comment

 

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

About Me