HOW TO

Use the Transform method in VC++

Last Published: April 25, 2020

Summary

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)

Procedure

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.


Code:
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();


Article ID:000004247

Software:
  • Legacy Products

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic