Create contour list
arcgissamples\spatialanalyst\CreateContourList.java
/* 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.
* 
*/
/* 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.spatialanalyst;

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

import com.esri.arcgis.datasourcesraster.RasterDataset;
import com.esri.arcgis.datasourcesraster.RasterWorkspace;
import com.esri.arcgis.datasourcesraster.RasterWorkspaceFactory;
import com.esri.arcgis.geoanalyst.RasterSurfaceOp;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;


public class CreateContourList
{
  public CreateContourList()
  {

  }

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

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

      // Get ArcGIS Home
      //Get DEVKITHOME Home
      String devKitHome = System.getenv("AGSDEVKITJAVA");
      
      String inputPath = devKitHome + "java" + File.separator +
                        "samples" + File.separator + 
                        "data" + File.separator + 
                        "raster";
      String inputRaster = "dem1";

      String outputPath = getOutputDir() + File.separator + "contourlist";
      File outputDir = new File(outputPath);
      outputDir.mkdir();

      CreateContourList contourListCreator = new CreateContourList();
      contourListCreator.createContourList(inputPath, inputRaster, outputPath);
      System.out.println("Done.  Output shapefile created in " + outputDir.getAbsolutePath());

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

  /**
   * Creates a ContourList shapefile using specified raster 
   * @param pathName
   * @param fileName
   * @param outputPath
   * @throws IOException
   */
  private void createContourList(String pathName, String fileName, String outputPath) throws IOException
  {
    // The RasterSurfaceOp actually carries out a suite of spatial analysis
    RasterSurfaceOp surfaceOp = new RasterSurfaceOp();

    // get RasterDataSet from the appropriate WorkspaceFactory
    RasterWorkspaceFactory rasterWSFactory = new RasterWorkspaceFactory();
    RasterWorkspace rasterWorkspace = new RasterWorkspace(rasterWSFactory.openFromFile(pathName, 0));
    RasterDataset rasterDataset = (RasterDataset) rasterWorkspace.openRasterDataset(fileName);

    // prepare the output dataset
    RasterWorkspace outputRasterWS = new RasterWorkspace(rasterWSFactory.openFromFile(outputPath, 0));
    surfaceOp.setOutWorkspaceByRef(outputRasterWS);

    // By setting this line we will generate a shapefile with the beginning name CountourList
    // in the outputpath directory
    surfaceOp.setDefaultOutputVectorPrefix("ContourList");

    // This is the list of elevations at which to create contours
    double[] heights = { 500, 1000, 3000, 3500, 4000 };
    surfaceOp.contourList(rasterDataset, heights);
  }

  /**
   * Initializes the lowest available ArcGIS License
   */
  static void initializeArcGISLicenses(AoInitialize ao) {
    try {
      
      if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView);
      else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcEditor) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcEditor);
      else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcInfo) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcInfo);
      else
      {
        System.err.println("Could not initialize an Engine, ArcView, ArcEditor, or ArcInfo license. Exiting application.");
        System.exit(-1);
      }  
      ao.checkOutExtension(com.esri.arcgis.system.esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Returns 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;

  }
}