arcgissamples\geometry\ProjectPoint.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Project a point with ProjectEx
arcgissamples\geometry\ProjectPoint.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.geometry;

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

public class ProjectPoint
{

  public static void main(String[] args)
  {
    System.out.println("Starting ProjectPoint - An ArcObjects Java SDK Developer Sample");

    try
    {
      // Initialize the engine and licenses.
      EngineInitializer.initializeEngine();

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

      // Create a point with Geographic coordinates...
      Point point = new Point();
      point.putCoords(-100.0, 40.0);

      System.out.println("Original coordinates: " + point.getX() + "," + point.getY());

      // Create the SpatialReferenceEnvironment...
      SpatialReferenceEnvironment spatialReferenceEnvironment = new SpatialReferenceEnvironment();

      // Apply the initial spatial reference...
      ISpatialReference geographicCoordinateSystem = spatialReferenceEnvironment
          .createGeographicCoordinateSystem(esriSRGeoCSType.esriSRGeoCS_NAD1927);
      point.setSpatialReferenceByRef(geographicCoordinateSystem);

      // Create the output projected coordinate system...
      ISpatialReference projectedCoordinateSystem = spatialReferenceEnvironment
          .createProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_NAD1983UTM_13N);

      // Create the GeoTransformation...
      IGeoTransformation iGeoTransformation = (IGeoTransformation) spatialReferenceEnvironment
          .createGeoTransformation(esriSRGeoTransformationType.esriSRGeoTransformation_NAD1927_To_WGS1984_5);

      // Project the point...
      point.projectEx(projectedCoordinateSystem, esriTransformDirection.esriTransformForward, iGeoTransformation,
          false, 0.0, 0.0);
      System.out.println("Projected coordinates: " + point.getX() + " , " + point.getY());

      System.out.println("Done!");

      aoInit.shutdown();

    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }
  
  /**
   * 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.esriLicenseProductCodeBasic) == esriLicenseStatus.esriLicenseAvailable)
      {
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
      }
      else
      {
        System.err.println("Could not initialize an Engine or Basic License. Exiting application.");
        System.exit(-1);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }  
}