arcgissamples\geoprocessing\CreateFeatureClass.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Create FeatureClass
arcgissamples\geoprocessing\CreateFeatureClass.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.geoprocessing;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.geoprocessing.GeoProcessor;
import com.esri.arcgis.geoprocessing.tools.datamanagementtools.AddField;
import com.esri.arcgis.geoprocessing.tools.datamanagementtools.CreateFeatureclass;
import com.esri.arcgis.geometry.IProjectedCoordinateSystem;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.geometry.esriSRProjCSType;
import com.esri.arcgis.interop.AutomationException;
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 CreateFeatureClass
{
  private GeoProcessor gp = null;
  
  public CreateFeatureClass()
  {
    try
    {
      gp = new GeoProcessor();
      gp.setOverwriteOutput(true);
    }
    catch (UnknownHostException e)
    {
      e.printStackTrace();
    }
    catch (AutomationException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Creates a feature class using specified path and name
   * @param shapefilePath
   * @param shapefileName
   */
  public void createFeatureClass(String shapefilePath, String shapefileName)
  {
    try
    {
      //instantiate the CreateFeatureClass GP tool
      CreateFeatureclass createFCTool = new CreateFeatureclass(shapefilePath, shapefileName);
      createFCTool.setGeometryType("point");

      // create a spatial reference to assign to the FeatureClass - we will use
      // use UTM - Zone 11 North WGS84
      SpatialReferenceEnvironment spatRefEnv = new SpatialReferenceEnvironment();
      IProjectedCoordinateSystem projectedCoordSystem = spatRefEnv.createProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_WGS1984UTM_11N);

      // set the coordinate system on the new feature class
      createFCTool.setSpatialReference(projectedCoordSystem);

      gp.execute(createFCTool, null);

      System.out.println("Made the FeatureClass");

      // Now we are going to add some fields to the featureclass
      AddField newField = new AddField(shapefilePath + File.separator + shapefileName, "textField", "text");
      newField.setFieldLength(40);

      gp.execute(newField, null);
      System.out.println("Made the text field");

      newField.setFieldName("longField");
      newField.setFieldType("long");

      gp.execute(newField, null);
      System.out.println("Made the Long field");

      newField.setFieldName("doubField");
      newField.setFieldType("double");

      newField.setFieldScale(2);
      newField.setFieldPrecision(20);

      gp.execute(newField, null);
      System.out.println("Made the double field and finished");

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

  }

  public static void main(String[] args)
  {
    System.out.println("Starting CreateFeatureClass - An ArcObjects Java SDK Developer Sample");
    
    try
    {
      // Initialize the engine and licenses.
      EngineInitializer.initializeEngine();

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

      //Create output workspace path      
      String outPath = getOutputDir();
      String outName = "newshapefile.shp";

      // call our method
      CreateFeatureClass ourApplication = new CreateFeatureClass();
      ourApplication.createFeatureClass(outPath, outName);
      
      aoInit.shutdown();
    }
    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();
    }
  }    

  private static String getOutputDir()
  {
    String userDir;
    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;

  }
}