Geotransformation
DatumShiftMGRStoDD.cpp
// 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.
// 


#include <stdio.h>
#include <ArcSDK.h>
#include <olb/esridefensesolutions.h>

#if defined(ESRI_UNIX)
  // implemented in libDefenseSolutionsSDK.so
  void DsInitialize();
#endif


int main (int argc, char **argv)
{
    USES_CONVERSION;

    ::AoInitialize (NULL);

#if defined(ESRI_UNIX)
    ::DsInitialize();
#endif
    {
        IAoInitializePtr ipInit (CLSID_AoInitialize);
        esriLicenseStatus status;
        ipInit->Initialize (esriLicenseProductCodeEngine, &status);
        if ( status != esriLicenseCheckedOut )
        {
            printf ("Invalid Licensing.\n");
            ::AoUninitialize();
            AoExit (0);
        }
    }

    // input a geolocation as an MGRS string in the "Indian 1960" datum
    // get back normalized MGRS string referenced to the "Indian 1960" datum
    // get back normalized DMS and DD strings referenced to the "WGS 1984" datum
    ISpatialReferenceFactoryPtr    ipSRF          (CLSID_SpatialReferenceEnvironment);
    IGeographicCoordinateSystemPtr ipIndian1960;
    ICoordinatePtr                 ipCoordMGRS    (CLSID_MGRSCoordinate);
    ICoordinatePtr                 ipCoordDMS     (CLSID_DMSCoordinate);
    ICoordinatePtr                 ipCoordDD      (CLSID_DDCoordinate);
    CComBSTR                       normalizedMGRS;
    CComBSTR                       normalizedDMS;
    CComBSTR                       normalizedDD;
    double                         xMGRS, yMGRS;
    double                         xDMS,  yDMS;
    double                         xDD,   yDD;

    // Data:  Hue, Vietnam (approx) (WGS 1984):     16.459999 107.699997  = 48QYD8829721745 = 16 27 36.0N 107 42 0.0E   all derived values
    //                              (Indian 1960):  16.45835N 107.70427E  = 48QYD8871621421 = 16 27 30.1N 107 42 15.4E  are from GeoTrans
    const char *mgrsInputString ("48 Q YD 8871621421");

    // set the coordinate precision for each converter
    ipCoordMGRS->put_Precision (esriCPOneCentimeter);
    ipCoordDMS ->put_Precision (esriCPOneTenThousandthSecond);
    ipCoordDD  ->put_Precision (esriCPOneTenthThousandthDegree);

    // forward the output from MGRS to the DMS and DD coordinates
    ipCoordMGRS->AddOutputCoordinate (ipCoordDMS);
    ipCoordMGRS->AddOutputCoordinate (ipCoordDD);

    // set up the spatial references
    ipSRF->CreateGeographicCoordinateSystem (esriSRGeoCS_Indian1960, &ipIndian1960);
    ipCoordMGRS->put_InputSpatialReference (ipIndian1960);
    ipCoordMGRS->put_OutputSpatialReference (ipIndian1960);

    // forwarded coordinates get their input spatial reference from the calling coordinate
    // but they need to have their output spatial reference specified, or else it defaults to WGS 1984
    // if you wanted you could specify DMS and DD output in Indian 1960, with the following code
    //ipCoordDMS->put_OutputSpatialReference (ipIndian1960);
    //ipCoordDD ->put_OutputSpatialReference (ipIndian1960);

    // the following put_String call performs all of the conversions
    ipCoordMGRS->put_String (CComBSTR(mgrsInputString));
    ipCoordMGRS->GetCoords  (&xMGRS, &yMGRS);
    ipCoordDMS ->GetCoords  (&xDMS,  &yDMS);
    ipCoordDD  ->GetCoords  (&xDD,   &yDD);
    ipCoordMGRS->get_String (&normalizedMGRS);
    ipCoordDMS ->get_String (&normalizedDMS);
    ipCoordDD  ->get_String (&normalizedDD);

    // display the results
    printf ("input = '%s'\n", mgrsInputString);
    printf ("MGRS normalized string = '%s'\n", OLE2A(normalizedMGRS));
    printf ("MGRS binary DD         = (%f, %f)\n", xMGRS, yMGRS);
    printf (" DMS normalized string = '%s'\n", OLE2A(normalizedDMS));
    printf (" DMS binary DD         = (%f, %f)\n", xDMS, yDMS);
    printf ("  DD normalized string = '%s'\n", OLE2A(normalizedDD));
    printf ("  DD binary DD         = (%f, %f)\n", xDD, yDD);

    return 0;

    // results:
    // input = '48 Q YD 8871621421'
    // MGRS normalized string = '48QYD8871621421'
    // MGRS binary DD         = (107.704267, 16.458342)
    //  DMS normalized string = '16 27 35.9816N 107 41 59.9709E'
    //  DMS binary DD         = (107.699992, 16.459995)
    //   DD normalized string = '16.4600N 107.7000E'
    //   DD binary DD         = (107.699992, 16.459995)
}