arcgissamples\analyst3d\CreateNewTin.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Create new TIN
arcgissamples\analyst3d\CreateNewTin.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.analyst3d;

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

import com.esri.arcgis.geodatabase.Tin;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseExtensionCode;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

public class CreateNewTin {
  
  public CreateNewTin(){
    
  }

  /**
   * Main Method - The console application entry point.
   * 
   * @param args String[] Command line argument
   */
  public static void main(String args[]) {
    System.out.println("Starting CreateNewTin - An ArcObjects SDK Developer Sample");

    try{
      //Initialize engine console application
      EngineInitializer.initializeEngine();
      
      //Initialize ArcGIS license
      AoInitialize aoInit = new AoInitialize();
      initializeArcGISLicenses(aoInit);
      
      //Get DEVKITHOME Home
      String devKitHome = System.getenv("AGSDEVKITJAVA");
      
      //Data output setup
      String outTinPath = devKitHome + File.separator + "java" + File.separator + "samples" 
                       + File.separator + "data" + File.separator + "site1" 
                       + File.separator + "dtm_tin";      
      String outTin = getOutputDir() + File.separator + "newtin";
      
      File outTinFile = new File(outTin);
      if (outTinFile.exists()) {
        System.out.println("Output Tin already exists: " + outTinFile.getAbsolutePath());
        System.out.println("Delete it and rerun");
        System.exit(-1);
      }
      
      CreateNewTin createNewTin = new CreateNewTin();
      createNewTin.createNewTin(outTinPath, outTin);

      System.out.println("Done.  Output file created in " + outTinFile.getAbsolutePath());
      
      //Ensure any ESRI libraries are unloaded in the correct order
      aoInit.checkInExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
      aoInit.shutdown();
    }catch(Exception e){
      System.out.println("Error: " + e.getMessage());
      e.printStackTrace();
      System.exit(-1);
    }
  }
  
  /**
   * Checks to see if an ArcGIS Engine Runtime license or an Basic License
   * is available. If so, then the appropriate ArcGIS License is initialized.
   * 
   * @param aoInit The AoInitialize object instantiated in the main method.
   */
  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);
      }
      
      aoInit.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
    } catch (Exception e) {e.printStackTrace();}
  }
  
  /**
   * Create a new tin based on the structure of an existing tin.
   * 
   * @param outTinPath Path to the new output tin
   * @param outTin Name of the new tin
   * @throws IOException If the tin cannot be created an input/output
   *              exception is thrown
   */
  public void createNewTin(String outTinPath, String outTin) throws IOException {
    try {
      //Get a reference to the existing tin
      Tin tin = new Tin();
      tin.init(outTinPath);
      
      //Use its extent for the new tin's extent
      Envelope extent = (Envelope) tin.getFullExtent();
      
      Envelope newExtent = new Envelope();
      newExtent.setXMax(extent.getXMin() + extent.getWidth()/2);
      newExtent.setXMin(extent.getXMin());
      newExtent.setYMax(extent.getYMin() + extent.getHeight()/2);
      newExtent.setYMin(extent.getYMin());
      
      //Set the new extent to the existing tin's spatial extent
      newExtent.setSpatialReferenceByRef(tin.getSpatialReference());
      
      //Create the new tin
      Tin newTin = new Tin();
      newTin.initNew(newExtent);
      newTin.saveAs(outTin, new Boolean(false));
      newTin.setEmpty();
      
      tin.setEmpty();
    }catch(Exception e){e.printStackTrace();}
  }
  
  /**
   * Convenience method to generate an output directory based on the operating
   * system that the sample is being executed on. 
   * 
   * @return A path to the new directory is return
   */
  private static String getOutputDir() {
    String userDir;
    
    //Get the operating systems user profile or home location depending
    //on which operating system this sample is executed on.
    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;
  }
}