arcgissamples\geometry\RetrievePolygonCentroid.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Retrieve polygon centroid
arcgissamples\geometry\RetrievePolygonCentroid.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.
* 
*/
package arcgissamples.geometry;

import java.io.File;

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.Point;
import com.esri.arcgis.geometry.Polygon;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

public class RetrievePolygonCentroid
{
  public static void main(String[] args)
  {
    System.out.println("Starting RetrievePolygonCentroid - An ArcObjects Java SDK Developer Sample");

    try
    {
      // Initialize the engine and licenses.
      EngineInitializer.initializeEngine();

      AoInitialize aoInit = new AoInitialize();
      initializeArcGISLicenses(aoInit);

      //Get DEVKITHOME Home
      String devKitHome = System.getenv("AGSDEVKITJAVA");

      // Change the following lines if you want to use different data
      String workspacePath = devKitHome + "java" + File.separator + "samples" + File.separator + "data"
          + File.separator + "usa";
      String featureClassName = "states.shp";
      File shapefileFile = new File(workspacePath, featureClassName);
      if (!shapefileFile.exists())
      {
        System.out.println("Shapefile does not exist: " + shapefileFile.getAbsolutePath());
        System.exit(0);
      }

      // Get the feature workspace for the shapefile.
      ShapefileWorkspaceFactory shapeFileFactory = new ShapefileWorkspaceFactory();
      Workspace featureWorkspace = (Workspace) shapeFileFactory.openFromFile(workspacePath, 0);

      /*
       * Get the feature class from the feature workspace, and insure we are working with a Polygon feature class.
       */
      IFeatureClass featureClass = featureWorkspace.openFeatureClass(featureClassName);
      if (featureClass.getShapeType() != esriGeometryType.esriGeometryPolygon)
      {
        System.out.println("The shapefile's feature class is not of type Polygon.  Try another.");
        System.exit(1);
      }
      System.out.println("Shapefile: " + shapefileFile.getAbsolutePath());

      // Get each feature's shape and centroid from the feature class.
      System.out.println("Centroids:");
      int numFeatures = featureClass.featureCount(null);
      for (int i = 0; i < numFeatures; i++)
      {
        IFeature feature = featureClass.getFeature(i);
        Polygon polygon = (Polygon) feature.getShape();
        Point point = (Point) polygon.getCentroid();
        System.out.println((i + 1) + ": " + point.getX() + ", " + point.getY());
      }

      // Shutdown ArcObjects.
      aoInit.shutdown();
      System.out.println("Done.");
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Initializes the lowest available ArcGIS License
   */
  private static void initializeArcGISLicenses(AoInitialize aoInit)
  {
    try
    {
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable)
      {
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      }
      else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) == esriLicenseStatus.esriLicenseAvailable)
      {
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
      }
      else
      {
        System.err.println("Could not initialize an Engine or Basic License. Exiting application.");
        System.exit(-1);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

}