arcgissamples\geoprocessing\SelectFeaturesUsingIntersection.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Select Using Intersection tool
arcgissamples\geoprocessing\SelectFeaturesUsingIntersection.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 com.esri.arcgis.geoprocessing.GeoProcessor;
import com.esri.arcgis.geoprocessing.tools.datamanagementtools.CopyFeatures;
import com.esri.arcgis.geoprocessing.tools.datamanagementtools.MakeFeatureLayer;
import com.esri.arcgis.geoprocessing.tools.datamanagementtools.SelectLayerByLocation;
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 SelectFeaturesUsingIntersection
{
  private GeoProcessor gp = null;

  public SelectFeaturesUsingIntersection()
  {
    try
    {
      gp = new GeoProcessor();
      // Overwrite any previous output data
      gp.setOverwriteOutput(true);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public String selLayerByLocationAndAttribute(String inpath, String outpath)
  {
    try
    {
      // make a layer cities_lyr from cities feature class
      MakeFeatureLayer makeFeatureLayer_1 = new MakeFeatureLayer(inpath + File.separator + "mexico.gdb" + File.separator + "cities", "cities_lyr");
      // pass the MakeFeatureLayer object to GeoProcessor object to execute
      gp.execute(makeFeatureLayer_1, null);

      // make another layer chihuahua_lyr from chihuahua feature class

      MakeFeatureLayer makeFeatureLayer_2 = new MakeFeatureLayer(inpath  + File.separator +  "mexico.gdb" + File.separator + "chihuahua", "chihuahua_lyr");
      // make the second layer
      gp.execute(makeFeatureLayer_2, null);

      // select only those cities in chihuahua
      SelectLayerByLocation selByLocation = new SelectLayerByLocation("cities_lyr");
      selByLocation.setOverlapType("intersect");
      selByLocation.setSelectFeatures("chihuahua_lyr");
      // execute the selection by passing the selByLocation object
      gp.execute(selByLocation, null);

      // write the selected features to a new feature class chihuahua_select.shp
      // should only be 1 city
      CopyFeatures cpFeature = new CopyFeatures(makeFeatureLayer_1.getOutLayer(), outpath
          + File.separator + "chihuahua_select.shp");

      gp.execute(cpFeature, null);

    }
    catch (Exception e)
    {
      e.printStackTrace();
      return "Failure";
    }
    // if everything is fine then return true
    return "Success";
  }

  public static void main(String[] args)
  {
    System.out.println("Select Using Intersection -- An ArcObjects SDK Developer Sample");
    
    AoInitialize aoInit = null;
    try
    {
      // Initialize the engine and licenses.
      EngineInitializer.initializeEngine();

      aoInit = new AoInitialize();
      initializeArcGISLicenses(aoInit);
      
      //Get DEVKITHOME Home
      String devKitHome = System.getenv("AGSDEVKITJAVA");
    
      String inpath = devKitHome + File.separator + "java" + File.separator + 
      "samples" + File.separator + 
      "data" + File.separator + 
      "geoprocessing";
      
      //Create output workspace path
      String outpath = getOutputDir();

      SelectFeaturesUsingIntersection selectlayer = new SelectFeaturesUsingIntersection();
      System.out.println(selectlayer.selLayerByLocationAndAttribute(inpath, outpath));
      
      try
      {
        aoInit.shutdown();
      }
      catch (Exception e)
      {
      }      
    }
    catch (Exception e)
    {
      try
      {
        aoInit.shutdown();
      }
      catch (Exception e1)
      {
        e1.printStackTrace();
      }
      
      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();
    }
  }  

  /**
   * Get the output directory
   * @return
   */
  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;

  }  
  
}