Create and mosaic a raster dataset in a file geodatabase
arcgissamples\geoprocessing\CreateMosaic.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.
* 
*/
package arcgissamples.geoprocessing;

import java.io.File;

import com.esri.arcgis.datasourcesGDB.FileGDBWorkspaceFactory;
import com.esri.arcgis.datasourcesraster.MosaicRaster;
import com.esri.arcgis.datasourcesraster.RasterWorkspaceFactory;
import com.esri.arcgis.datasourcesraster.rstMosaicColormapMode;
import com.esri.arcgis.datasourcesraster.rstMosaicOperatorType;
import com.esri.arcgis.geodatabase.RasterCatalog;
import com.esri.arcgis.geodatabase.Workspace;
import com.esri.arcgis.geoprocessing.GeoProcessor;
import com.esri.arcgis.geoprocessing.tools.datamanagementtools.CreateFileGDB;
import com.esri.arcgis.geoprocessing.tools.datamanagementtools.CreateRasterCatalog;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.VarArray;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

public class CreateMosaic
{
  public CreateMosaic()
  {

  }

  public static void main(String[] args)
  {
    System.out.println("Starting CreateMosaic - 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");
      
      String inputPath = devKitHome + File.separator + "java" + File.separator + "samples" + File.separator + "data"
      + File.separator + "raster" + File.separator + "airphotos";
      String outputPath = getOutputDir();

      CreateMosaic createMosaic = new CreateMosaic();
      createMosaic.createMosaic(inputPath, outputPath);

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

  /**
   * Creates a mosaic in Raster Catalog
   * 
   * @throws Exception
   */
  public void createMosaic(String inputDir, String outputDir) throws Exception
  {
    // set all names and paths
    String outputFGDBName = "CreateMosaic.gdb";
    String outputFGDBPath = outputDir + File.separator + outputFGDBName;
    String outputRasterName = "fileMosaic.img";
    String rasterCatalogName = "MosaicCatalog";
    String rasterCatalogPath = outputFGDBPath + File.separator + rasterCatalogName;

    //Create temporary unmanaged raster catalog in fgdb using GeoProcessing and load all rasters
    
    //init Geoprocessor
    System.out.print("\n**Creating GeoProcessor object...");
    GeoProcessor geoprocessor = new GeoProcessor();
    System.out.println("done.");

    // Create file GDB in the output directory
    System.out.print("\n**Creating FileGDB \"" + outputFGDBName + "\" in " + outputDir
        + " using Data Management/CreateFileGDB GP tool...");
    CreateFileGDB createFileGDB = new CreateFileGDB();
    createFileGDB.setOutFolderPath(outputDir);
    createFileGDB.setOutName(outputFGDBName);
    geoprocessor.execute(createFileGDB, null);
    System.out.println("done.");

    // Create an unmanaged raster catalog and loads all rasters to it
    System.out.print("\n**Creating RasterCatalog \"" + rasterCatalogName + "\" in " + outputFGDBName
        + " using Data Management/CreateRasterCatalog GP tool...");
    CreateRasterCatalog createRasterCatalog = new CreateRasterCatalog();
    createRasterCatalog.setOutPath(outputFGDBPath);
    createRasterCatalog.setOutName(rasterCatalogName);
    createRasterCatalog.setRasterManagementType("unmanaged");
    geoprocessor.execute(createRasterCatalog, null);
    System.out.println("done.");

    // Load rasters in the input folder and its subfolders to it
    System.out.print("\n**Loading all Raster Datasets from " + inputDir + " into " + rasterCatalogName
        + "\n using Data Management/WorkspaceToRasterCatalog GP tool...");
    VarArray parameters = new VarArray();
    parameters.add(inputDir);
    parameters.add(rasterCatalogPath);
    parameters.add("INCLUDE_SUBDIRECTORIES");
    geoprocessor.execute("WorkspaceToRasterCatalog_management", parameters, null);
    System.out.println("done.");

    /*
     * Mosaic datasets in raster catalog and save in IMG format in output directory
     */
    // Open raster catalog using IRasterWorkspaceEx methods, since Workspace implements IRasterWorkspaceEx
    FileGDBWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
    Workspace rasterWorkspaceEx = (Workspace)workspaceFactory.openFromFile(outputFGDBPath, 0);

    // RasterCatalog implements IRasterCatalog. Using
    // IRasterWorkspaceEx.openRasterCatalog() method implemented by Workspace
    RasterCatalog rasterCatalog = new RasterCatalog(rasterWorkspaceEx.openRasterCatalog(rasterCatalogName));

    // Mosaics all rasters in the raster catalog to an output raster dataset.
    // Using IMosaicRaster methods implemented by MosaicRaster.
    System.out.print("\n**Mosaicking all rasters in " + rasterCatalogName + "...");
    MosaicRaster mosaicRaster = new MosaicRaster();
    mosaicRaster.setRasterCatalogByRef(rasterCatalog);
    // Set mosaicking options, you may not need to set these for your data
    mosaicRaster.setMosaicColormapMode(rstMosaicColormapMode.MM_MATCH);
    mosaicRaster.setMosaicOperatorType(rstMosaicOperatorType.MT_LAST);
    System.out.println("done.");

    // Open output workspace
    System.out.print("\n**Saving mosaic to an IMG file in " + outputDir + "...");
    RasterWorkspaceFactory rasterWorkspaceFactory = new RasterWorkspaceFactory();
    Workspace workspace = (Workspace) rasterWorkspaceFactory.openFromFile(outputDir, 0);
    
    // Save out to a target raster dataset
    // It can be saved to IMG,TIFF,GRID,BMP,GIF,JPEG2000,JPEG,Geodatabase, ect.
    mosaicRaster.saveAs(outputRasterName, workspace, "IMAGINE Image");
    System.out.println("done.");

    System.out
        .println("\nCreated RasterCatalog in FGDB, loaded 2 rasters, mosaicked them and saved as IMAGINE Image(.img)."
            + "\nPlease check " + outputDir);
  }

  /**
   * 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.esriLicenseProductCodeArcView) 
          == esriLicenseStatus.esriLicenseAvailable)
      {
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView);
      }
      else
      {
        System.err.println("Could not initialize an Engine or ArcView license. Exiting application.");
        System.exit(-1);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Retrieves 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;
  }

}