/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © 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 ilog.views.maps.IlvAngularUnit;
import ilog.views.maps.IlvCoordinate;
import ilog.views.maps.IlvLinearUnit;
import ilog.views.maps.projection.IlvMercatorProjection;
import ilog.views.maps.srs.coordsys.IlvCoordinateSystem;
import ilog.views.maps.srs.coordsys.IlvGeographicCoordinateSystem;
import ilog.views.maps.srs.coordsys.IlvProjectedCoordinateSystem;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformation;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformationException;
import ilog.views.util.IlvProductUtil;

/**
 * This is a very simple sample code showing how to convert coordinates between
 * two coordinate systems. Basically, we will convert between geographic
 * coordinates (lat,lon) and some projected coordinates (here, to a Mercator
 * projection coordinate system).
 */
public class Sample1 {
  static public void main(String argv[]) {
    // This sample uses JViews Maps features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Maps Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.JViews_Maps_Deployment);

    // The source coordinate system : the default geographic one is ok.
    IlvCoordinateSystem sourceCS = IlvGeographicCoordinateSystem.WGS84;

    // The target coordinate system : a projected coordinate system with
    // a mercator projection
    IlvCoordinateSystem targetCS = new IlvProjectedCoordinateSystem("Mercator", IlvGeographicCoordinateSystem.WGS84,
        new IlvMercatorProjection(), IlvLinearUnit.METER, "X", // The X axis
                                                               // name
        "Y"); // The Y axis name

    // The coordinate to convert : 45W, 30N
    IlvCoordinate coord = new IlvCoordinate(-45D, 30D);

    // The transformation
    IlvCoordinateTransformation CT = IlvCoordinateTransformation.CreateTransformation(sourceCS, targetCS);

    // Transform a point
    try {
      coord = CT.transform(coord, coord); // put the result in coord
    } catch (IlvCoordinateTransformationException e) {
      System.out.println("Transformation exception for this data");
    }

    System.out.println("The Mercator coordinates of 45W 30N is ");
    System.out.println("x = " + (int) coord.x + " m");
    System.out.println("y = " + (int) coord.y + " m");

    // Get the inverse transformation
    IlvCoordinateTransformation invCT = CT.getInverse();

    // Transform the point
    try {
      coord = invCT.transform(coord, coord); // put the result in coord
    } catch (IlvCoordinateTransformationException e) {
      System.out.println("TransformationException exception for this data");
    }

    System.out.println("The inverse projection is " + IlvAngularUnit.DEGREE.toDMS(coord.x, false) + " "
        + IlvAngularUnit.DEGREE.toDMS(coord.y, true));

  }

}