arcgissamples\scenario\AreaCalculatorMain.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Utility object
arcgissamples\scenario\AreaCalculatorMain.java
/* Copyright 2015 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions at <your ArcGIS install location>/DeveloperKit10.0/userestrictions.txt.
* 
*/
package arcgissamples.scenario;

import java.io.File;

import arcgissamples.scenario.extension.AreaCalculator;
import arcgissamples.scenario.extension.ICalculateArea;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.Workspace;
import com.esri.arcgis.geometry.Polygon;
import com.esri.arcgis.system.EngineContext;
import com.esri.arcgis.system.EngineInitializer;

public class AreaCalculatorMain {
  static String devKitHome = null;
  String workspacePath;
  double totalArea = 0;
  
  public AreaCalculatorMain(){
  }
    
  public static void main(String[] args) {
    System.out.println("Starting Utility Object - Area Calulator - An ArcObjects Java SDK Developer Sample \n");
    EngineInitializer.initializeVisualBeans();
    initializeArcGISLicenses();
    
    try {
      //Get DEVKITHOME Home
      devKitHome = System.getenv("AGSDEVKITJAVA");
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    if (!(new File(devKitHome).exists())) {
      System.out.println(devKitHome + " does not exist.\nExiting...");
      System.exit(-1);
    }
    AreaCalculatorMain thisClass = new AreaCalculatorMain();
    thisClass.clientCalculateArea_FineGrained();
    thisClass.clientCalculateArea_CoarseGrained();
  }
  static void initializeArcGISLicenses() {
    try {
      com.esri.arcgis.system.AoInitialize ao = new com.esri.arcgis.system.AoInitialize();
      if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
    public void clientCalculateArea_FineGrained(){
        try
        {
        // Change the following lines if you want to use different polygon data
        workspacePath = devKitHome + "/java/samples/data/usa";
          ShapefileWorkspaceFactory wsf = new ShapefileWorkspaceFactory();
      Workspace ws = (Workspace) wsf.openFromFile(workspacePath, 0);
      IFeatureClass fClass= ws.openFeatureClass("counties");        
      
      long start = System.currentTimeMillis();
        // Get the states layer
        int numFeatures = fClass.featureCount(null);
        IFeature feature = null;
        Polygon polygon = null;
        for(int i=0; i<numFeatures; i++)
        {
          feature = fClass.getFeature(i);
          polygon = (Polygon)feature.getShape();
          totalArea += polygon.getArea();
        }
        long end = System.currentTimeMillis();
        System.out.println("Total time of execution in milli sec for Fine Grained : "+ (end - start) + "\n");

        }
        catch(Exception e){
          e.printStackTrace();
        }
    }

  public void clientCalculateArea_CoarseGrained(){
        try
        {
        // Change the following lines if you want to use different polygon data
        workspacePath = devKitHome + "/java/samples/data/usa";
          ShapefileWorkspaceFactory wsf = new ShapefileWorkspaceFactory();
      Workspace ws = (Workspace) wsf.openFromFile(workspacePath, 0);
      IFeatureClass fClass= ws.openFeatureClass("counties");
      // Create an instance of the utility object and call the method calculateArea() on it.
      System.out.println("Creating an instance of utility object in Engine Context ....");
      ICalculateArea calcArea = (ICalculateArea) EngineContext.createObject(AreaCalculator.class);
        long start = System.currentTimeMillis();
        System.out.println("Calling method calculateArea() on the utility object ....\n");
      double area = calcArea.calculateArea(fClass);
      long end = System.currentTimeMillis();
      System.out.println("Total area: " + area);
      System.out.println("Total time of execution in milli sec for Coarse Grained : " + (end-start));
        }
        catch(Exception e){
          e.printStackTrace();
        }
    }

}