Transforming data

This section is based on a simple example that illustrates the basic operations required to define the coordinate systems, and shows how to use the coordinate transformations back and forth.
The complete source code of the example on which this section is based can be found in the following file:

Example overview

The Sample1.java file contains a very simple program that shows how to convert coordinates from geographic coordinates to a projected coordinate system, using a Mercator projection.
This class has only a static main() method, in which the coordinate systems are instantiated and the coordinate is transformed.

Choosing a source and a destination coordinate system

As our example uses coordinate systems and transformation, the relevant packages must be imported, as well as the projection package for the Mercator projection definition:
import ilog.views.maps.*;
import ilog.views.maps.srs.coordsys.*;
import ilog.views.maps.srs.coordtrans.*;
import ilog.views.maps.projection.*;
The first important step is to define the source and the target coordinate systems.
For the source coordinate system, some latitude and longitude coordinates expressed in degrees are needed. This is the kind of coordinate defined in a geographic coordinate system. In this example, the WGS84 coordinate is used. The WGS84 geographic coordinate system defines ellipsoidal coordinates over the standard WGS84 ellipsoid.
IlvCoordinateSystem sourceCS = IlvGeographicCoordinateSystem.WGS84;
Now these coordinates must be changed to Mercator coordinates. Then you can create a new projected coordinate system using the Mercator projection to express coordinates. Note that the Mercator projection should use exactly the same geodetic parameters as the WGS84 geographic coordinate system. The latter is passed as the geographic coordinate system of the projected coordinate system.
IlvCoordinateSystem targetCS = 
   new IlvProjectedCoordinateSystem("Mercator",
                                    IlvGeographicCoordinateSystem.WGS84,
                             new IlvMercatorProjection(),
                             IlvLinearUnit.METER,
                             "X",   // The X axis name
                             "Y");  // The Y axis name

Transforming coordinates

In order to transform the coordinates, you need an IlvCoordinateTransformation. This is performed by calling the automatic transformation creation method:
IlvCoordinateTransformation CT =
      IlvCoordinateTransformation.CreateTransformation(sourceCS, targetCS);
To convert coordinates, you just have to store the coordinates in an IlvCoordinate, and then call the transform() method of the coordinate transformation.
// The coordinate to convert : 45W, 30N
IlvCoordinate coord = new IlvCoordinate(-45D, 30D);
   
try {
   coord = CT.transform(coord,
                        coord); // put the result in coord
}
catch (IlvCoordinateTransformationException e) {
   System.out.println("Transformation exception for this data");
}
The transform() method takes two parameters: the first one is the source coordinate to transform, the second one is an IlvCoordinate to hold the result of the transformation. When this second parameter is null, a new IlvCoordinate is allocated and used. The method returns the result coordinate.
Of course, as in this example, it is possible to use the same IlvCoordinate as source and destination.
Note that the IlvCoordinateTransformation.transform() method may throw different kinds of exception if the transformation process leads to mathematical errors or overflows. Most of the time, when those methods are thrown, the transformation is not defined at the specified point.

Displaying the result

The result of the transformation is now stored in the coord variable. In the following code example, the result is expressed in meters, which is the default measurement unit.
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");
Note
It does not make much sense to interpret these values as distances since the center of the projection is far from the projected point, and projections do not maintain distances on a large scale

Getting the inverse transformation

At this point, the methodology to convert from geographic coordinates to Mercator coordinates is available.
To convert coordinates from Mercator back to geographic, use the getInverse method, which returns the inverse transformation, if any.
// 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");
}

Printing geographic coordinates

To print geographic coordinates, you can use the toDMS conversion method from the IlvAngularUnit class. This method converts an angle specified by a double value in a unit to DMS encoding.
For example, IlvAngularUnit.DEGREE.toDMS() converts an angle specified in degrees to DMS.
System.out.println("The inverse projection is " 
                + IlvAngularUnit.DEGREE.toDMS(coord.x,false) 
                + " "
                + IlvAngularUnit.DEGREE.toDMS(coord.y,true));