/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

import java.io.FileNotFoundException;
import java.io.IOException;

import ilog.views.IlvGraphic;
import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.maps.IlvFeatureRenderer;
import ilog.views.maps.IlvMapFeature;
import ilog.views.maps.IlvMapRenderException;
import ilog.views.maps.format.IlvMapFormatException;
import ilog.views.maps.format.dted.IlvDTEDLayer;
import ilog.views.maps.format.dted.IlvDTEDReader;
import ilog.views.maps.srs.coordsys.IlvGeographicCoordinateSystem;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformation;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformationException;
import ilog.views.util.IlvProductUtil;

/**
 * Example of how to use the DTED reader package. 
 */
public class DTEDReader {
  IlvManager manager = new IlvManager();
  IlvManagerView view = new IlvManagerView(manager);
  
  /**
   * Loads a single DTED tile.
   */
  public IlvGraphic loadSingleFrame(String fileName) 
  throws IlvMapFormatException, 
         FileNotFoundException, 
         IOException, 
         IlvMapRenderException, 
         IlvCoordinateTransformationException {
    // Instantiate the reader.
    IlvDTEDReader reader = new IlvDTEDReader(fileName);
    // Retreive the unique feature.
    IlvMapFeature feature = reader.getNextFeature();
    // Retreive the image renderer.
    IlvFeatureRenderer renderer = reader.getDefaultFeatureRenderer();
    // Transformation.
    IlvGeographicCoordinateSystem gcs = IlvGeographicCoordinateSystem.WGS84;
    IlvCoordinateTransformation tr = 
      IlvCoordinateTransformation.CreateTransformation(feature.getCoordinateSystem(),
                                                       gcs);
    // Make the graphic object to be inserted in a IlvManager. 
    IlvGraphic graphic = renderer.makeGraphic(feature, tr);
    // Returns it.
    return graphic;
  }
  
  private int level = 0;
  
  /**
   * Create a Load on Demand DTED layer providing the directory name. 
   */
  public void makeLODLayer(String dirName) {
    // New DTED layer.
    IlvDTEDLayer layer = new IlvDTEDLayer(dirName, level);
    // Add it to the manager.
    manager.addLayer(layer, -1);
    // Center on tile (0, 0).
    layer.fitTransformerToTile(view, 0, 0);
  }
  
  public static void main(final String a[]) {
    // This sample uses JViews Maps features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Rogue Wave JViews Maps Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(
        IlvProductUtil.JViews_Maps_Deployment);

    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    javax.swing.SwingUtilities.invokeLater(
      new Runnable() {
        public void run() {
          DTEDReader reader = new DTEDReader();
          try {
            reader.loadSingleFrame(a[0]);
          } catch (IlvMapFormatException e) {
            e.printStackTrace();
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          } catch (IlvMapRenderException e) {
            e.printStackTrace();
          } catch (IlvCoordinateTransformationException e) {
            e.printStackTrace();
          }
        }
      }
      );
  }
}