arcgissamples\geodatabase\AddFeature.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Add feature
arcgissamples\geodatabase\AddFeature.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.geodatabase;

import java.io.File;
import java.io.IOException;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.Feature;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.GeometryDef;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.Workspace;
import com.esri.arcgis.geodatabase.esriFeatureType;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.Polygon;
import com.esri.arcgis.geometry.UnknownCoordinateSystem;
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 AddFeature {
  
  public AddFeature(){
    
  }
  
  /**
   * Main Method - The console application entry point.
   * 
   * @param args String[] Command line argument
   */
  public static void main(String[] args) {
    System.out.println("Starting AddFeature - An ArcObjects SDK Developer Sample");
    
    try{
      //Initialize engine console application
      EngineInitializer.initializeEngine();
      
      //Initialize ArcGIS license
      AoInitialize aoInit = new AoInitialize();
      initializeArcGISLicenses(aoInit);
      
      //Get DEVKITHOME Home
      String devKitHome = System.getenv("AGSDEVKITJAVA");
      
      //Data access setup
      String inPath = devKitHome + "java" + File.separator + "samples" + File.separator 
                     + "data" + File.separator + "usa";
      String inName = "states.shp";
      
      //Data output setup
      String outPath = getOutputDir() + File.separator + "addfeature";
      String outName = "addshp.shp";

      File outDataDir = new File(outPath);
      outDataDir.mkdir();
      
      File outShapefile = new File(outPath, outName);
      
      if (outShapefile.exists()) {
        System.out.println("Output datafile already exists: " + outShapefile.getAbsolutePath());
        System.out.println("Delete it (plus .shx and .dbf files) and rerun");
        System.exit(-1);
      }
      
      AddFeature addFeature = new AddFeature();
      addFeature.addFeaturesToShapefile(inPath, inName, outPath, outName);
      
      System.out.println("Done.  Output shapefile created in " + outShapefile.getAbsolutePath());      
      
      //Ensure any ESRI libraries are unloaded in the correct order
      aoInit.shutdown();
    }catch(Exception e){
      System.out.println("Error: " + e.getMessage());
      System.out.println("Sample failed.  Exiting...");
      e.printStackTrace();
      System.exit(-1);
    }
  }
  
  /**
   * Checks to see if an ArcGIS Engine Runtime license or an Basic License
   * is available. If so, then the appropriate ArcGIS License is initialized.
   * 
   * @param aoInit The AoInitialize object instantiated in the main method.
   */
  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();}
  }

  /**
   * Get the corresponding feature class instance variable for the input shapefile.
   *
   * @param inShapefilePath path to input shapefile.
   * @param inShapefileName name of input shapefile.
   * @return IFeatureClass the feature class for the input shapefile and name
   * @throws IOException if could not obtain the input shapefile's feature class, or if the shapefile doesn't hold polygon features.
   */
  private FeatureClass getInputFeatureClass(String inShapefilePath, String inShapefileName) throws IOException {
    FeatureClass featureClass = null;
    
    try {
      ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();
      
      Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(inShapefilePath, 0));
      
      featureClass = new FeatureClass(workspace.openFeatureClass(inShapefileName));
      
      // Insure this shapefile holds polygon information.
      if (featureClass.getShapeType() != esriGeometryType.esriGeometryPolygon) {
        throw new IOException("This sample requires a polygon shapefile.");
      }
    }catch (IOException e) {
      System.out.println("Couldn't access feature class :" + inShapefileName + " in " + inShapefilePath);
      throw e;
    }
    
    return featureClass;
  }

  /**
   * Set the corresponding feature workspace and class instance variables for the output shapefile.
   * 
   * @param inFeatureClass The input feature class
   * @param outWorkspace The output workspace
   * @param outShapefilePath path to output shapefile
   * @param outShapefileName output shapefile's name
   * @return IFeatureClass The output feature class
   * @throws IOException if could not create the output shapefile or its feature class
   */
  private FeatureClass getOutputFeatureClass(IFeatureClass inFeatureClass, Workspace outWorkspace, String outShapefilePath, String outShapefileName) throws IOException {
    FeatureClass outFeatureClass = null;
    
    try {
      // Get the input shapefile's shape field name.
      String shapeFieldName = inFeatureClass.getShapeFieldName();

      // Create geometry shape field for the new shapefile.
      GeometryDef geomDef = new GeometryDef();
      geomDef.setGeometryType(esriGeometryType.esriGeometryPolygon);
      geomDef.setSpatialReferenceByRef(new UnknownCoordinateSystem());
      
      Field field = new Field();
      field.setName(shapeFieldName);
      field.setType(esriFieldType.esriFieldTypeGeometry);
      field.setGeometryDefByRef(geomDef);
      
      Fields fields = new Fields();
      fields.addField(field);

      // Create output shapefile's feature class, which also creates the shapefile.
      outFeatureClass = new FeatureClass(outWorkspace.createFeatureClass(outShapefileName, fields, null, null,
          esriFeatureType.esriFTSimple, shapeFieldName, ""));
    }catch (IOException e) {
      System.out.println("Could not create output shapefile " + outShapefileName + " in " + outShapefilePath + " (Already exists?)");
      throw e;
    }
    
    return outFeatureClass;
  }

  /**
   * Add circle polygon features to the output shapefile.
   *
   * @param inPath path to input shapefile
   * @param inName name of input shapefile
   * @param outPath path to output shapefile
   * @param outName name of output shapefile
   * @throws IOException if could not perform any one of a variety of ArcObjects tasks
   */
  private void addFeaturesToShapefile(String inPath, String inName, String outPath, String outName) throws IOException {
    try {
      //Create the output shapefile's feature workspace.
      ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();
      Workspace outWorkspace = new Workspace(shapefileWorkspaceFactory.openFromFile(outPath, 0));
      
      //Set the input and output feature class instance variables
      FeatureClass inFeatureClass = getInputFeatureClass(inPath, inName);
      FeatureClass outFeatureClass = getOutputFeatureClass(inFeatureClass, outWorkspace, outPath, outName);

      //Start/Open an edit session for creating output features.
      outWorkspace.startEditing(true);
      outWorkspace.startEditOperation();

      //For each (polygon) feature in the input shapefile, create a new (circle polygon)
      //feature based on the its centroid, and save that new feature to the
      //new shapefile.
      int count = inFeatureClass.featureCount(null);
      int nStoredFeatures = 0;

      
      for (int i = 0; i < count; i++) {
        //Get the feature's geometry as a polygon, get its centroid,
        //and convert the polygon to a circle of radius 2.
        Polygon polygon = (Polygon) inFeatureClass.getFeature(i).getShape();
        
        //Skip empty geometries, which can happen with editing.
        if (polygon.isEmpty()) {
          System.out.println("Skipping feature " + i);
          continue;
        }

        polygon.setCircle(polygon.getCentroid(), 2);

        //Create a new feature in the output feature class
        //and set its shape to be the polygon just created.
        Feature feature = (Feature) outFeatureClass.createFeature();
        feature.setShapeByRef(polygon);

        //Store the feature.
        feature.store();
        
        nStoredFeatures++;
      }

      //Close the edit session.
      outWorkspace.stopEditOperation();
      outWorkspace.stopEditing(true);
      
      System.out.println("Saved " + nStoredFeatures + " new features in output directory " +
          outWorkspace.getPathName());
    }catch (IOException e) {
      System.out.println("Could not store new features in output shapefile.");
      throw e;
    }
  }
  
  /**
   * Convenience method to generate an output directory based on the operating
   * system that the sample is being executed on. 
   * 
   * @return A path to the new directory is return
   */
  private static String getOutputDir() {
    String userDir;
    
    //Get the operating systems user profile or home location depending
    //on which operating system this sample is executed on.
    if(System.getProperty("os.name").toLowerCase().indexOf("win") > -1){
      userDir = System.getenv("UserProfile");
    }else{
      userDir = System.getenv("HOME");
    }
      
    String outputDir = userDir + File.separator + "arcgis_sample_output";
    
    System.out.println("Creating output directory - " + outputDir);
    
    new File(outputDir).mkdir();
    
    return outputDir;
  }
}