MapObjects Connection

 

Using the Transform method in VC++

On the Transform method, the last two parameters are densification tolerance and a geotransformation object.  These are optional, which is why in VB you don't need to pass them.  Unfortunately, in C++, "optional" parameters are needed and are passed in a VARIANT structure.  If you look at the C++ wrapper files found in: \..\Samples\MFC\Common, and in: MoProjCoordSys.cpp, you'll see the Transform function declared as:

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

So, you'll need to set up two extra VARIANTs.  Here's some code to put within the OnMouseMove event handler that demonstrates this.

NB. I recommend that if you are switching in between datums then you do 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;
aProjCoordSys.CreateDispatch(TEXT("MapObjects2.ProjCoordSys"));
aProjCoordSys.SetType(moProjCS_WGS1984UTM_33N);
 
// define a geographic coordsys
CMoGeoCoordSys aGeogCoordSys;
aGeogCoordSys.CreateDispatch(TEXT("MapObjects2.GeoCoordSys"));
aGeogCoordSys.SetType(moGeoCS_WGS1984);
 
// 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
VariantInit(&va);
va.vt = VT_NULL;
 
VARIANT vax; // for the densification tolerance - this should be passing a double
VariantInit(&vax);
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
aProjCoordSys.ReleaseDispatch();
aGeogCoordSys.ReleaseDispatch();

Back to MapObjects Connection