Thursday 24 November 2016

c# - ArcObjects 10.1 .NET Convert Point from WGS1984 to OSGB1936


Please can someone help me convert a point in WGS1984 to OSGB1936, I have followed all the documentation (or so i think...) and yet have not made any headway into this problem. This is my code :



Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
System.Object obj = Activator.CreateInstance(factoryType);
ISpatialReferenceFactory3 spacialReferenceFactory3 = obj as ISpatialReferenceFactory3;

// Create Transformation from WGS84 to OSGB86
IGeoTransformation geoTrans = spacialReferenceFactory3.CreateGeoTransformation((int)esriSRGeoTransformationType.esriSRGeoTransformation_OSGB1936_To_WGS1984Petrol) as IGeoTransformation;

ISpatialReference fromSpatialReference;
ISpatialReference toSpatialReference;
geoTrans.GetSpatialReferences(out fromSpatialReference, out toSpatialReference);


IGeometry5 geometry;
IPoint point = new PointClass();
point.PutCoords(51.465615, -3.159875);
geometry = point as IGeometry5;
geometry.SpatialReference = toSpatialReference;
geometry.ProjectEx(fromSpatialReference, esriTransformDirection.esriTransformForward, geoTrans, false, 0.0, 0.0);
point = new PointClass();
point = geometry as IPoint;


My point is being returned from the Geometry object as 51.46200954514034, -3.1549783406850165. I have tried using the reverse transformation as specified in geometry.ProjectEx(), and all i get is the same X and Y as the original point. I have also tried different combinations of reverse / forward and also swapping the fromSpatialReference and toSpatialReference.



Answer



This prints: 319524.804848596 174709.885049006.


private static void TestProjection()
{
Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
System.Object obj = Activator.CreateInstance(factoryType);
var srf = obj as ISpatialReferenceFactory3;

// Create Transformation from WGS84 to OSGB86

var geoTrans = srf.CreateGeoTransformation((int)esriSRGeoTransformationType.esriSRGeoTransformation_OSGB1936_To_WGS1984Petrol) as IGeoTransformation;

ISpatialReference fromSpatialReference;
ISpatialReference toSpatialReference;
geoTrans.GetSpatialReferences(out fromSpatialReference, out toSpatialReference);

var wgs84GCS = srf.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
var bngPCS = srf.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_BritishNationalGrid);
if((wgs84GCS.FactoryCode != toSpatialReference.FactoryCode)
|| (bngPCS.GeographicCoordinateSystem.FactoryCode != fromSpatialReference.FactoryCode))

{
throw new Exception("invalid geotransformation");
}

IGeometry5 geometry;
IPoint point = new PointClass();
point.PutCoords(-3.159875, 51.465615);
geometry = point as IGeometry5;
geometry.SpatialReference =wgs84GCS;


geometry.ProjectEx(bngPCS, esriTransformDirection.esriTransformReverse, geoTrans, false, 0.0, 0.0);
point = geometry as IPoint;
Debug.Print("{0} {1}", point.X, point.Y);
}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...