See c# code below.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Geometry;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Framework;
namespace HansenAddin
{
public class PickGeoTransButton : ESRI.ArcGIS.Desktop.AddIns.Button
{
public PickGeoTransButton()
{
}
protected override void OnClick()
{
try
{
// let user choose a transformation
var sr1 = ArcMap.Document.FocusMap.SpatialReference;
var featClass = ((IFeatureLayer)ArcMap.Document.FocusMap.get_Layer(0)).FeatureClass;
var sr2 = ((IGeoDataset)featClass).SpatialReference;
var list = GetTransformations(sr1, sr2);
if (list.Count == 0)
{
MessageBox.Show(String.Format("No geotransforms to go between {0} and {1}", sr1.Name, sr2.Name));
return;
}
IListDialog dlg = new ListDialogClass();
foreach (IGeoTransformation geoTrans in list)
dlg.AddString(geoTrans.Name);
if (dlg.DoModal("Choose a Geotransformation", 0, ArcMap.Application.hWnd))
{
var geoTrans = list[dlg.Choice];
var direction = GetDir(geoTrans, sr1, sr2);
MessageBox.Show(String.Format("{0} \n{1} \n{2} \n{3}", geoTrans.Name, sr1.Name,sr2.Name,direction ));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static List<IGeoTransformation> GetTransformations(ISpatialReference sr1, ISpatialReference sr2)
{
int factCode1 = GetGCSFactoryCode(sr1);
int factCode2 = GetGCSFactoryCode(sr2);
var outList = new List<IGeoTransformation>();
// Use activator to instantiate arcobjects singletons ...
var type = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
var srf = Activator.CreateInstance(type) as ISpatialReferenceFactory2;
var gtSet = srf.CreatePredefinedGeographicTransformations();
gtSet.Reset();
for (int i = 0; i < gtSet.Count; i++)
{
ISpatialReference fromSR;
ISpatialReference toSR;
var geoTrans = (IGeoTransformation)gtSet.Next();
geoTrans.GetSpatialReferences(out fromSR, out toSR);
if ((fromSR.FactoryCode == factCode1 && toSR.FactoryCode == factCode2) ||
(fromSR.FactoryCode == factCode2 && toSR.FactoryCode == factCode1))
{
outList.Add(geoTrans);
}
}
return outList;
}
private static esriTransformDirection GetDir(IGeoTransformation geoTrans, ISpatialReference sr1, ISpatialReference sr2)
{
int code1 = GetGCSFactoryCode(sr1);
int code2 = GetGCSFactoryCode(sr2);
ISpatialReference fromSR;
ISpatialReference toSR;
geoTrans.GetSpatialReferences(out fromSR, out toSR);
if (fromSR.FactoryCode == code1 && toSR.FactoryCode == code2)
return esriTransformDirection.esriTransformForward;
else if (fromSR.FactoryCode == code2 && toSR.FactoryCode == code1)
return esriTransformDirection.esriTransformReverse;
else
throw new Exception(String.Format("{0} does not support going between {1} and {2}",
geoTrans.Name, sr1.Name, sr2.Name));
}
private static int GetGCSFactoryCode(ISpatialReference sr)
{
if (sr is IProjectedCoordinateSystem)
return ((IProjectedCoordinateSystem)sr).GeographicCoordinateSystem.FactoryCode;
else if (sr is IGeographicCoordinateSystem)
return ((IGeographicCoordinateSystem)sr).FactoryCode;
else
throw new Exception("unsupported spatialref type");
}
protected override void OnUpdate()
{
}
}
}