How to perform an affine transformation


Summary
This article shows how to use an affine transformation to transform a digitized geometry into ground (projected) coordinates.

Performing an affine transformation

The following code example first uses an affine transformation to transform a digitized geometry into ground (projected) coordinates. The same transformation is then applied to an array of double precision, raw coordinate values. You may be interested in the latter approach when transforming large numbers of coordinates coming from a text file, binary file, or some other large source of raw coordinates. This avoids the processing overhead of creating Point objects for every coordinate.
[Java]
static void affineTransformation2D_Example()throws Exception{
    //Define control points.
    IPoint[] controlFromPoints = new IPoint[4];
    //To point.
    IPoint[] controlToPoints = new IPoint[4];
    for (int i = 0; i < 4; i++){
        controlFromPoints[i] = new Point();
        controlToPoints[i] = new Point();
    }
    //From points.
    controlFromPoints[0].putCoords(0, 0);
    controlFromPoints[1].putCoords(0, 1);
    controlFromPoints[2].putCoords(1, 0);
    controlFromPoints[3].putCoords(1, 1);
    //To points.
    controlToPoints[0].putCoords(5, 5);
    controlToPoints[1].putCoords(5, 6);
    controlToPoints[2].putCoords(6, 5);
    controlToPoints[3].putCoords(6, 6);

    //Create an AffineTransformation2D object.
    //You have to use the IAffineTransformation2D3GEN because DefineFromControlPoints
    // uses C-Style Arrays in IAffineTransformation2D, which are not usable in .NET.
    IAffineTransformation2D3GEN affineTransformation2D = new AffineTransformation2D()
        ;
    affineTransformation2D.defineFromControlPoints(controlFromPoints,
        controlToPoints);

    double fromError[] = {
        0
    };
    double toError[] = {
        0
    };
    affineTransformation2D.getRMSError(fromError, toError);
    if (fromError[0] > 0.05)
        System.out.println(
            "RMS error is too large; please redigitize control points");


    //Create a polygon.
    ISegmentCollection tranformPolygon = new Polygon();
    IEnvelope envelope = new Envelope();
    envelope.putCoords(0, 0, 10, 10);
    tranformPolygon.setRectangle(envelope);

    ITransform2D transformee = (ITransform2D)tranformPolygon;
    transformee.transform(esriTransformDirection.esriTransformForward, 
        (ITransformation)affineTransformation2D);

    //DigitizedGeometry is now in the destination coordinate system and should be assigned a 
    //spatial reference.
    //Now you will apply the same transformation directly to an array of double precision values 
    //representing (x,y) points.
    //The x,y values are assumed to be interleaved in the array: fromPoints(0) is the x-coordinate 
    //for the first point, fromPoints(1) is the y-coordinate, etc.
    int length = 50;
    double[][] fromPoints = new double[1][length];
    for (int i = 0; i < length; i++)
        fromPoints[0][i] = (i + 1) * 2;
    double[][] toPoints = new double[1][length];

    affineTransformation2D.transformPointsFF
        (esriTransformDirection.esriTransformForward, fromPoints, toPoints);
    //Print results.
    String report = "";
    for (int i = 0; i < length; i++)
        report = report + "Coordinate before = " + fromPoints[0][i] + 
            " and after transformation = " + toPoints[0][i] + "\n";

    System.out.println(report);

}






Additional Requirements
  • This article assumes your project includes references to the ESRI Geometry assembly.

Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine