arcgissamples\raster\MosaicRasters.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Mosaic rasters
arcgissamples\raster\MosaicRasters.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.
* 
*/
/* 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.raster;

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

import com.esri.arcgis.datasourcesraster.Raster;
import com.esri.arcgis.datasourcesraster.RasterDataset;
import com.esri.arcgis.datasourcesraster.RasterGeometryProc;
import com.esri.arcgis.datasourcesraster.RasterWorkspace;
import com.esri.arcgis.datasourcesraster.RasterWorkspaceFactory;
import com.esri.arcgis.geometry.ISpatialReference;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;


public class MosaicRasters
{

  public MosaicRasters()
  {

  }

  public static void main(String[] args)
  {
    System.out.println("Starting MosaicRasters - 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");

      // Change the following lines if you want to use different data
      String inRasterPath = devKitHome + "java" + File.separator + 
        "samples" + File.separator + 
        "data" + File.separator + 
        "raster"  + File.separator + 
        "airphotos";;
      String inRasterDatasetName1 = "seattle1.tif";
      String inRasterDatasetName2 = "seattle2.tif";
      String outRasterPath = getOutputDir() + File.separator + "rastermosaic";
      String outRasterName = "OutputRasterMosaic";

      File outRasterDir = new File(outRasterPath);
      outRasterDir.mkdir();

      File outRasterFile = new File(outRasterPath, outRasterName + ".aux");
      if (outRasterFile.exists())
      {
        System.out.println("Output raster already exists in " + outRasterDir.getAbsolutePath());
        System.out.println("Delete the directory or the files in it, and rerun");
        System.exit(0);
      }

      MosaicRasters convert = new MosaicRasters();
      convert.mosaicRasters(inRasterPath, inRasterDatasetName1, inRasterDatasetName2, outRasterPath,
          outRasterName);

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

  /**
   * Mosaic two raster images
   * @param inPath1
   * @param inDatasetName1
   * @param inPath2
   * @param inDatasetName2
   * @param outPath
   * @param outDataset
   * @throws IOException
   */
  private void mosaicRasters(String inPath1, String inDatasetName1, String inDatasetName2, String outPath,
      String outDataset) throws IOException
  {
    try
    {
      //create a raster workspace factory
      RasterWorkspaceFactory rasterWorkspaceFactory = new RasterWorkspaceFactory();
      
      // Get the input raster workspace and datasets      
      RasterWorkspace inRasterWorkspace = new RasterWorkspace(rasterWorkspaceFactory.openFromFile(inPath1, 0));
      if (!inRasterWorkspace.isWorkspace(inPath1))
      {
        throw new IOException("Input path 1 - " + inPath1 + " does not hold raster workspace data.");
      }
      RasterDataset inRasterDataset1 = (RasterDataset) inRasterWorkspace.openRasterDataset(inDatasetName1);
      RasterDataset inRasterDataset2 = (RasterDataset) inRasterWorkspace.openRasterDataset(inDatasetName2);
      
      //Get the output raster workspace
      RasterWorkspace outRasterWorkspace = new RasterWorkspace(rasterWorkspaceFactory.openFromFile(outPath, 0));

      // Create a Raster object and append to it the first rasterbands of each dataset,
      // to be used in the mosaicing process.
      Raster bandsRaster = new Raster();
      bandsRaster.appendBand(inRasterDataset1.item(0));
      bandsRaster.appendBand(inRasterDataset2.item(0));

      // Get the cell size and spatial reference properties of the first input raster to
      // be copied into the mosaiced raster.
      Raster raster = new Raster(inRasterDataset1.createDefaultRaster());
      double cellSize = (raster.meanCellSize().getX() + raster.meanCellSize().getY()) / 2;
      ISpatialReference spatialReference = raster.getSpatialReference();

      // Perform the mosaicing. The mosaicing requires a raster geometry processor object
      // and properties that match the input rasters. Supported raster types are
      // 'IMAGINE Image', 'TIFF', and 'GRID'. We use TIFF here.
      System.out.print("Mosaicing ...");
      RasterGeometryProc rasterGeometryProc = new RasterGeometryProc();
      rasterGeometryProc.mosaic(outDataset, outRasterWorkspace, "TIFF", cellSize, spatialReference, bandsRaster);
      System.out.println("done.");
    }
    catch (IOException e)
    {
      System.out.println("Couldn't create mosaic.");
      throw e;
    }
    
    System.out.println("Please check " + outPath + File.separator + outDataset + ".");
  }

  /**
   * 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.esriLicenseProductCodeBasic) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeBasic);
      else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeStandard) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeStandard);
      else if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeAdvanced) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeAdvanced);
      else
      {
        System.err.println("Could not initialize an Engine, Basic, Standard, or Advanced License. Exiting application.");
        System.exit(-1);
      }  
      ao.checkOutExtension(com.esri.arcgis.system.esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
    } 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;
  }

}