How to perform a 2D geographic transformation


This sample demonstrates how to use the Transform method on ITransform2D to perform a geographic transformation for a collection of points (latitude/longitude) from OSGB1936 to WGS1984. The ISpatialReferenceFactory is used to create a suitable geographic transformation object that implements the IGeocentricTranslation interface. The ITransform2D interface is QI'd directly from the points collection.

How to use

  1. Start ArcMap.
  2. Add a new ArcMap Macro called Geotransformation and in the VBA editor, paste the following code into it.
  3. Run the Macro. You will see in the VBA Immediate window the results of the geotransformation.
[VBA]
Sub Geotransformation()
    ' Create a spatial reference factory
    Dim pSpatRefFact As ISpatialReferenceFactory2
    Set pSpatRefFact = New SpatialReferenceEnvironment
    
    ' Create a GeocentricTranslation object for OSGB1936_to_WGS1984 using the factory.
    Dim pGT As IGeocentricTranslation
    Set pGT = pSpatRefFact.CreateGeoTransformation(esriSRGeoTransformation_OSGB1936_To_WGS1984_1)
    
    ' How to get the parameters from the Geotransformation object.
    Dim dx As Double
    Dim dy As Double
    Dim dz As Double
    pGT.GetParameters dx, dy, dz
    Debug.Print dx, dy, dz
    
    ' How to get the from/to SpatialReferences that are supported by this geotransformation.
    Dim pFromGCS As IGeographicCoordinateSystem
    Dim pToGCS As IGeographicCoordinateSystem
    pGT.GetSpatialReferences pFromGCS, pToGCS
    Debug.Print pFromGCS.Name, pToGCS.Name
    
    ' Now use the ITransform2D::Transform method - create a points collection.
    Dim pPoints As IPointCollection
    Set pPoints = New Multipoint
    
    ' Create a point object
    Dim pPt As IPoint
    Set pPt = New Point
    
    ' Give XY coordinates to the points
    ' First point
    pPt.PutCoords 1, 55 ' Longitude/Latitude
    pPoints.AddPoint pPt
    
    'Second Point
    Set pPt = Nothing
    Set pPt = New Point
    pPt.PutCoords 2, 56 ' Longitude/Latitude
    pPoints.AddPoint pPt
    
    ' Get a Transform2D by QIing from the points collection object
    Dim pTransform As ITransform2D
    Set pTransform = pPoints
    
    ' Make the call - note the direction and the geotrans object are all that are needed
    pTransform.Transform esriTransformForward, pGT
    
    ' Output the results
    Debug.Print "Results using Transform..."
    Dim i As Integer
    For i = 0 To pPoints.PointCount - 1
        Debug.Print "LON " & pPoints.Point(i).X & " LAT " & pPoints.Point(i).Y
    Next i
    
End Sub