Using the IAffineTransformation2D project and MoveOrigin methods
Do the following steps to use the IAffineTransformation2D project and MoveOrigin methods:
- Create two spatial references, one for the geographic coordinate system (GCS) and one for the projected coordinate system (PCS).
- Create a point.
- Project the point from the GCS to the PCS.
- Query interface (QI) the point to ITransform2D.
- Set up an AffineTransformation.
- Call Transform on ITransform2D.
The following code example shows how to use the IAffineTransformation2D project and MoveOrigin method:
[C#] public void Driver()
{
Test( - 122, 37);
// MoveOrigin is at a slightly different location: Yields slightly inaccurate results.
Test( - 125, 35);
}
private void Test(double originX, double originY)
{
//Set up spatial references.
Type factoryType=Type.GetTypeFromProgID(
"esriGeometry.SpatialReferenceEnvironment");
System.Object obj=Activator.CreateInstance(factoryType);
ISpatialReferenceFactory3 spatialReferenceFactory=obj as
ISpatialReferenceFactory3;
ISpatialReference fromSpatialReference =
spatialReferenceFactory.CreateGeographicCoordinateSystem((int)
esriSRGeoCSType.esriSRGeoCS_NAD1983);
ISpatialReference toSpatialReference =
spatialReferenceFactory.CreateProjectedCoordinateSystem((int)
esriSRProjCSType.esriSRProjCS_NAD1983UTM_10N);
//Set up a point for transforming.
IPoint point=new ESRI.ArcGIS.Geometry.Point();
point.PutCoords( - 122, 37);
point.SpatialReference=fromSpatialReference;
point.Project(toSpatialReference);
ITransform2D transformator=point as ITransform2D;
//Set up the comparison point.
//This is the point the transformation should result in.
IPoint comparisonPoint=new ESRI.ArcGIS.Geometry.Point();
comparisonPoint.PutCoords( - 121, 42);
comparisonPoint.SpatialReference=fromSpatialReference;
comparisonPoint.Project(toSpatialReference);
//Transform.
//Set up the affine transformation.
//MoveOrigin is at the same location as the Point to be transformed: Yields accurate results.
IAffineTransformation2D2 affineTranformation=new AffineTransformation2D()as
IAffineTransformation2D2;
affineTranformation.Move(1, 5);
ESRI.ArcGIS.Geometry.Point origin=new ESRI.ArcGIS.Geometry.Point();
origin.PutCoords(originX, originY);
affineTranformation.MoveOrigin=origin;
affineTranformation.SpatialReference=fromSpatialReference;
affineTranformation.Project(toSpatialReference);
//Transform and report results.
System.Windows.Forms.MessageBox.Show("A2D XTrans: " +
affineTranformation.XTranslation + ", YTrans: " +
affineTranformation.YTranslation + "\n" + "A2D MoveOrigin X: " +
affineTranformation.MoveOrigin_2.X + ", Y: " +
affineTranformation.MoveOrigin_2.Y + "\n" + "Before Trans, Point X:" +
point.X + ", Y:" + point.Z);
transformator.Transform(esriTransformDirection.esriTransformForward,
affineTranformation);
System.Windows.Forms.MessageBox.Show("After Trans, Point X:" + point.X + ", Y:"
+ point.Y + "\n" + "Comparison, Point X:" + comparisonPoint.X + ", Y:" +
comparisonPoint.Y + "\n" + "Did X coordinates match? " + (point.X ==
comparisonPoint.X) + "\n" + "Did Y coordinates match? " + (point.Y ==
comparisonPoint.Y));
}
[VB.NET] Private Sub TestMove()
'Set up spatial references.
Dim t As Type=Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment")
Dim obj As System.Object=Activator.CreateInstance(t)
Dim pSpatialReferenceEnv As ESRI.ArcGIS.Geometry.ISpatialReferenceFactory3=obj
Dim pFromSR As ISpatialReference
pFromSR=pSpatialReferenceEnv.CreateGeographicCoordinateSystem(esriSRGeoCSType.esriSRGeoCS_NAD1983)
Dim pToSR As ISpatialReference
pToSR=pSpatialReferenceEnv.CreateProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_NAD1983UTM_10N)
'Set up a point for transforming.
Dim pPt As IPoint
Dim pT2D As ITransform2D
pPt=New Point
pPt.PutCoords( -122, 37)
pPt.SpatialReference=pFromSR
pPt.Project(pToSR)
pT2D=pPt
'Set up the comparison point.
'This is the point the transformation should result in.
Dim pPt2 As IPoint
pPt2=New Point
pPt2.PutCoords( -121, 42)
pPt2.SpatialReference=pFromSR
pPt2.Project(pToSR)
'Transform.
Dim pA2D2 As IAffineTransformation2D2
Dim pA2D As IAffineTransformation2D2
Dim pOrigin As IPoint
' Set up the affine transformation.
' MoveOrigin is at the same location as the point to be transformed: Yields accurate results.
pA2D2=New AffineTransformation2D
pA2D2.Move(1, 5)
pA2D=pA2D2
pOrigin=New Point
pOrigin.PutCoords( -122, 37)
pA2D.MoveOrigin=pOrigin
pA2D.SpatialReference=pFromSR
pA2D.Project(pToSR)
' Transform and report results.
Debug.Print("----Using MoveOrigin at the same location as the Point to be transformed----")
Debug.Print("A2D XTrans: " & pA2D.XTranslation & ", YTrans: " & pA2D.YTranslation)
Debug.Print("A2D MoveOrigin X: " & pA2D2.MoveOrigin_2.X & ", Y: " & pA2D2.MoveOrigin_2.Y)
Debug.Print("Before Trans, Point X:" & pPt.X & ", Y:" & pPt.Y)
pT2D.Transform(esriTransformDirection.esriTransformForward, pA2D)
Debug.Print("After Trans, Point X:" & pPt.X & ", Y:" & pPt.Y)
Debug.Print("Comparison, Point X:" & pPt2.X & ", Y:" & pPt2.Y)
Debug.Print("Did X coordinates match? " & (pPt.X=pPt2.X))
Debug.Print("Did Y coordinates match? " & (pPt.Y=pPt2.Y))
' Set up the affine transformation.
' MoveOrigin is at a slightly different location: Yields slightly inaccurate results.
pA2D2=New AffineTransformation2D
pA2D2.Move(1, 5)
pA2D=pA2D2
pOrigin=New Point
pOrigin.PutCoords( -125, 35)
pA2D.MoveOrigin=pOrigin
pA2D.SpatialReference=pFromSR
pA2D.Project(pToSR)
' Set up a point for transforming.
pPt=New Point
pPt.PutCoords( -122, 37)
pPt.SpatialReference=pFromSR
pPt.Project(pToSR)
pT2D=pPt
' Transform and report results.
Debug.Print("----Using MoveOrigin at a slightly different location----")
Debug.Print("A2D XTrans: " & pA2D.XTranslation & ", YTrans: " & pA2D.YTranslation)
Debug.Print("A2D MoveOrigin X: " & pA2D2.MoveOrigin_2.X & ", Y: " & pA2D2.MoveOrigin_2.Y)
Debug.Print("Before Trans, Point X:" & pPt.X & ", Y:" & pPt.Y)
pT2D.Transform(esriTransformDirection.esriTransformForward, pA2D)
Debug.Print("After Trans, Point X:" & pPt.X & ", Y:" & pPt.Y)
Debug.Print("Comparison, Point X:" & pPt2.X & ", Y:" & pPt2.Y)
Debug.Print("Did X coordinates match? " & (pPt.X=pPt2.X))
Debug.Print("Did Y coordinates match? " & (pPt.Y=pPt2.Y))
End Sub
To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing | Deployment licensing |
---|---|
ArcGIS for Desktop Basic | ArcGIS for Desktop Basic |
ArcGIS for Desktop Standard | ArcGIS for Desktop Standard |
ArcGIS for Desktop Advanced | ArcGIS for Desktop Advanced |
Engine Developer Kit | Engine |