Transform 2D
arcgissamples\geometry\Transform2D.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.geometry;

import com.esri.arcgis.geometry.*;
import com.esri.arcgis.system.*;

/**
 * Description : The code in this class demonstrates how to perform a geographic transformation for a collection of
 * points
 */
public class Transform2D
{
  public Transform2D()
  {
    
  }

  public static void main(String[] args)
  {
    System.out.println("Starting ClipByEnvelope - An ArcObjects Java SDK Developer Sample");
    try
    {
      // Initialize the engine and licenses.
      EngineInitializer.initializeEngine();

      AoInitialize aoInit = new AoInitialize();
      initializeArcGISLicenses(aoInit);

      Transform2D t = new Transform2D();
      t.transform();
    
      aoInit.shutdown();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  
  /**
   * This sample demonstrates how to perform a geographic transformation for a collection of points
   * (latitude/longitude) from OSGB1936 to WGS1984.
   */
  private void transform() throws Exception
  {
    // Create a spatial reference factory
    SpatialReferenceEnvironment pSpatRefFact = new SpatialReferenceEnvironment();

    // Create a GeocentricTranslation object for OSGB1936_to_WGS1984 using the factory.
    GeocentricTranslation pGT = (GeocentricTranslation) pSpatRefFact
        .createGeoTransformation(esriSRGeoTransformationType.esriSRGeoTransformation_OSGB1936_To_WGS1984_1);

    // How to get the parameters from the Geotransformation object
    double[] dx = new double[1];
    double[] dy = new double[1];
    double[] dz = new double[1];
    pGT.getParameters(dx, dy, dz);
    
    // How to get the from/to SpatialReferences that are supported by this geotransformation.
    ISpatialReference pFromGCS[] = new ISpatialReference[1];
    ISpatialReference pToGCS[] = new ISpatialReference[1];
    pGT.getSpatialReferences(pFromGCS, pToGCS);

    // Now use the Transform method - create a points collection
    Multipoint pPoints = new Multipoint();

    // Create point objects
    Point pPt1 = new Point();
    Point pPt2 = new Point();

    // Give XY coordinates to the points //First point
    pPt1.putCoords(1, 55); // Lon/Lat
    pPoints.addPoint(pPt1, null, null);

    // Give XY coordinates to the points //Second point
    pPt2.putCoords(2, 56); // Lon/Lat
    pPoints.addPoint(pPt2, null, null);

    for (int i = 0; i < pPoints.getPointCount(); i++)
      System.out.println("LON " + pPoints.getPoint(i).getX() + " LAT " + pPoints.getPoint(i).getY());

    // Make the call - note the direction and the geotrans object are all that are needed
    pPoints.transform(esriTransformDirection.esriTransformForward, pGT);
    
    // Output the results
    System.out.println("Results using Transform...");
    for (int i = 0; i < pPoints.getPointCount(); i++)
      System.out.println("LON " + pPoints.getPoint(i).getX() + " LAT " + pPoints.getPoint(i).getY());
  }  
  
  /**
   * 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();
    }
  }    
  
}