How To: Use the Transform method in VC++


The last two parameters of the Transform method, densification tolerance and a geotransformation object, are optional, that is why in Visual Basic you don't need to pass them. In C++, however, optional parameters are needed and must be passed in a VARIANT structure.

If you look at the C++ wrapper files found in:
\..\Samples\MFC\Common, and in:
MoProjCoordSys.cpp, you will see the Transform function declared as:

LPDISPATCH CMoProjCoordSys::Transform (LPDISPATCH FromCoordSys, LPDISPATCH FromShape,const VARIANT& DensificationTolerance, const VARIANT& GeoTransformation)


You need to set up two extra VARIANTs. The following example demonstrates this. Put this code put within the OnMouseMove event handler.

If you are switching between datums, it is recommend you use geotransformation objects in all your projection work.

void CYourApplicationDlg::OnMouseMoveMap(short Button, short Shift, long X, long Y)

// Here, we'll demonstrate the Transform method whilst tracking the mouse
// coordinates in the debug window. We're assuming that our
// mapcontrol has already been set to project to UTM Grid Zone 33N.

CMoPoint aPoint(m_map.ToMapPoint((float)X, (float)Y));
TRACE2( "Projected mouse position %f\t%f\n", aPoint.GetX(), aPoint.GetY() );

// define a Projected Coordinate System or just get it from the mapcontrol!
CMoProjCoordSys aProjCoordSys;

// define a geographic coordsys
CMoGeoCoordSys aGeogCoordSys;

// create VARIANTs to be used for the transform method Note. If you did want
// to use the geotrans, the Variant should be set up to accept dispatch pointers:
// e.g. va.vt = VT_DISPATCH; and then va.pdispVal = myGeotrans.m_lpDispatch;
VARIANT va; // for the geotransformation object
va.vt = VT_NULL;

// for the densification tolerance - this should be passing a double
vax.vt = VT_R8;
vax.dblVal = 0.0;

// Now transform the point
CMoPoint aGeogPoint(aGeogCoordSys.Transform(aProjCoordSys.m_lpDispatch, aPoint.m_lpDispatch, vax, va));
TRACE2( "Geographic mouse position %f\t%f\n", aGeogPoint.GetX(), aGeogPoint.GetY() );
// and do some rubbish collection to recover memory