Thursday, September 24, 2015

Calculate zonal-statistics using ESRI ArcObjects and C#

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)  
     {  
   
       ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);  
       ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.Desktop);  
   
       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;   
   
   
       if (extensionConfig.State != esriExtensionState.esriESUnavailable)  
       {  
         extensionConfig.State = esriExtensionState.esriESEnabled;  
   
         Geoprocessor geoprocessor = new Geoprocessor();  
         geoprocessor.OverwriteOutput = true;  
   
         var zonalStatistics = new ZonalStatisticsAsTable  
                                 {  
                                   in_value_raster = valueRaster,  
                                   zone_field = zoneField,  
                                   in_zone_data = feature,  
                                   out_table = outputTable  
                                 };  
         try  
         {  
           geoprocessor.Execute(zonalStatistics, null);  
         }  
         catch (Exception ex)  
         {  
           object level = 0;  
           Console.WriteLine(geoprocessor.GetMessages(ref level));  
           Console.WriteLine(ex.Message);  
         }  
       }  
       else  
       {  
         Console.WriteLine("No Spatial Analyst License available");  
       }  
   
     }  
   
 //Then call the function, provide parameters with full path    
 ComputeZonalStatisticsUsingEsriArcobjects("featurepath","zoneField", "valueRasterPath", "outputTablePath")  
   
   

The ZonalStatisticsAsTable object can accept inputs in various format such as -
in_zone_data => inFeatureName(string) or inLayerName(ILayer)
zone_field => "zone_name", must be string
in_value_raster =>  inRasterDataset(IRasterDataset) or inRasterPath(string)
out_table => Path(string)+table_name.dbf (ex- D:\temp.dbf , no spaces are allowed in the path)











arcobject , C# , zonal-statistics

0 comments :

Post a Comment

 

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

About Me