Thursday, September 24, 2015

Calculate zonal-statistics using ESRI ArcObjects and C#

Be The First To Comment
Import the following references in the project.

using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.SpatialAnalystTools;
using ESRI.ArcGIS.esriSystem;

This solution is only works for single band raster. For multi-band raster zonal statistics I will make a separate post soon.

 public static void ComputeZonalStatisticsFromEsri(string feature, string zoneField, string valueRaster, string outputTable)  
       UID pUid = new UIDClass();  
       pUid.Value = "esriSpatialAnalystUI.SAExtension";  
       // Add Spatial Analyst extension to the license manager.  
       object v = null;  
       IExtensionManagerAdmin extensionManagerAdmin = new ExtensionManagerClass();  
       extensionManagerAdmin.AddExtension(pUid, ref v);  
       // Enable the license.  
       IExtensionManager extensionManager = (IExtensionManager)extensionManagerAdmin;  
       IExtension extension = extensionManager.FindExtension(pUid);  
       IExtensionConfig extensionConfig = (IExtensionConfig)extension;   

Tuesday, September 22, 2015

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

Be The First To Comment
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;  
        rowProcess = rasterRows - row;  

Thursday, September 17, 2015

Tutorial: Getting started with ArcGIS Application Development using ArcObjects and Vb.NET

Be The First To Comment
This will be a series of videos on Programming with ArcObjects by Hussein Nasser where he features a fictional project called Bestaurants and gradually added functions to the project from scratch using ArcObjects and Vb.NET programming on top of ArcGIS. An excellent tutorial for beginners to learn how to use ArcObjects to build an ArcGIS application.

Wednesday, September 16, 2015

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

Be The First To Comment
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..");  

Feature to Raster Conversion Using C# and ArcGIS

Be The First To Comment
Import ArcGIS.ConversionTools and ArcGIS.Geoprocessor then pass the file paths as param into following function you will get the resulting raster out of shapefile.

 using ESRI.ArcGIS.ConversionTools;  
 using ESRI.ArcGIS.Geoprocessor;  
 public static void Rasterize(string inputFeature, string outRaster, string fieldName, int cellSize)  
         //Runtime manager to find the ESRI product installed in the system  
         Geoprocessor geoprocessor = new Geoprocessor();  
         geoprocessor.OverwriteOutput = true;  
         FeatureToRaster featureToRaster = new FeatureToRaster();  
         featureToRaster.cell_size = cellSize;  
         featureToRaster.in_features = inputFeature;  
         featureToRaster.out_raster = outRaster;  
         featureToRaster.field = fieldName;  
         geoprocessor.Execute(featureToRaster, null);  

Tuesday, September 15, 2015

Clip a raster with shapefile using C# and Gdal

Be The First To Comment

Over the week, I stumbled on problem clipping raster with feature using C# and Gdal and come up with following solution, which clips the features and fills no data value for missing raster  values inside the extent. If you are creating a new project- set up GDAL & C# environment as described here

Saturday, September 12, 2015

FIXED: Shape file import error in Postgis AddGeometry column doesn't exist

Last week I have installed PostgreSql/PostGis and PgAdmin. Once I have tired to import shape file from PgAdmin using "PostGIS Shape file and DBF Loader 2.1" as below.

But it throws an error complaining "File import error in PostGis AddGeometry column doesn't exist" as shown in figure below 

Then, I found out that the spatial extension is not enabled in my database. The following image shows the steps to enabled the spatial extension in the database. 

Open new extension and name it postgis.


Then import the shape file again... Voila it works

Friday, September 11, 2015

Fetching environmental data from Meso West API version 2

Be The First To Comment
In few months the MesoWest group will depreciated their MesoWest data API version 1 and fully moving over version 2. The Meso West data API 2 have been in the production for most of this year. The API v2 has significantly faster and more useful than version 1 and well as increased response time and availability including accumulated precipitation, simple station statistics, and climatological data requests.

Here is the code snippet to extract data variables using jQuery in JSON format. 
           county:'Los Angeles,San Diego,',  
           within:15, //Observations with in past 15 min  
      function (data)  
        for(var i=0;i<data.STATION.length;i++)  
                //check if all stations contain all the observation we need  
                if((data.STATION[i].OBSERVATIONS.hasOwnProperty("air_temp_value_1"))&& (data.STATION[i].OBSERVATIONS.hasOwnProperty("wind_speed_value_1"))&&  
                     var stn = data.STATION[i];  
                     var dat = stn.OBSERVATIONS;  
                     var stnInfo =stn.NAME.toUpperCase();  
                     var elev=parseInt(stn.ELEVATION);                                                         
                     //displaying air temperature from all stations   
      alert("Could not access the MesoWest Data!");  

Vector to Raster Conversion using GDAL & C#

Be The First To Comment
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 
(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;

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

About Me