AnsweredAssumed Answered

WGS to OSGB and back again

Question asked by askltd on Sep 9, 2016

I am trying to write a couple of general purpose routines to convert from WGS1984 to OSGB1936 (BNG) and back again

 

I have used one of the example in the help files and have it working from wgs1984 to osgb with the following code.

 

public IPoint GetOSGB(double longitude, double latitude)
{
//Create source spatial reference
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

IEnvelope envelope = new EnvelopeClass();
envelope.PutCoords(longitude, latitude, longitude, latitude);
envelope.SpatialReference = spatialReference;

IProjectedCoordinateSystem projectedCoordinateSystem = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_BritishNationalGrid);

IGeoTransformation geoTransformation = spatialReferenceFactory.CreateGeoTransformation((int)esriSRGeoTransformationType.esriSRGeoTransformation_OSGB1936_To_WGS1984Petrol) as IGeoTransformation;

IGeometry2 geometry = envelope as IGeometry2;
geometry.ProjectEx(projectedCoordinateSystem as ISpatialReference, esriTransformDirection.esriTransformReverse, geoTransformation, false, 0, 0);

IPoint point = new PointClass();
point.SpatialReference = sRefOsgb;
point.PutCoords(envelope.XMin,envelope.YMin);
return point;
}

 

 

I have been testing it with

IPoint office = transformationClass.GetOSGB(-2.124627, 53.337185);

 

and this gives me the correct position

for the reverse I have been using the following

 

public IPoint GetWGS(double easting, double northng)
{
//Create source spatial reference
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
//ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_OSGB1936);
//spatialReference.SetFalseOriginAndUnits(-80.0000000232831, 39.9999999767169, 42949672.9);
//Create envelope and define its spatial reference
IEnvelope envelope = new EnvelopeClass();
envelope.PutCoords(easting, northng, easting, northng);
envelope.SpatialReference = spatialReference;
//Destination spatial reference
//IProjectedCoordinateSystem projectedCoordinateSystem = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_BritishNationalGrid);
IProjectedCoordinateSystem projectedCoordinateSystem = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_BritishNationalGrid);
//Define the XYDomain equivalent to SetFalseOriginAndUnits
//projectedCoordinateSystem.SetDomain(500000, 600000, 5300000, 5600000);
//Create a Geotransformation (Datum transformation)
IGeoTransformation geoTransformation = spatialReferenceFactory.CreateGeoTransformation((int)esriSRGeoTransformationType.esriSRGeoTransformation_OSGB1936_To_WGS1984Petrol) as IGeoTransformation;
String report = "Print envelope coordinates before projection:\n" +
envelope.XMin + " , " + envelope.YMin + " , " + envelope.XMax + " , " + envelope.YMax + "\n\n\n";
//Project envelope
IGeometry2 geometry = envelope as IGeometry2;
geometry.ProjectEx(projectedCoordinateSystem as ISpatialReference, esriTransformDirection.esriTransformForward, geoTransformation, false, 0, 0);
report = report + "Print envelope coordinates after projection:\n" +
envelope.XMin + " , " + envelope.YMin + " , " + envelope.XMax + " , " + envelope.YMax;
//System.Windows.Forms.MessageBox.Show(report);
IPoint point = new PointClass();
point.SpatialReference = sRefOsgb;
point.PutCoords(envelope.XMin, envelope.YMin);
return point;
}

 

 

and using the result from the first conversion, I call the second conversion with the following
IPoint back = transformationClass.GetWGS(office.X, office.Y);

 

 

However, when I try to do the reverse it dont' work. I have tried to change the projectedCoordinateSystem but there are lot of different projected coordinate systems for WGS 1984 .

 

I either get back an error saying empty co-ordinates, or some other values, miles away from the correct location.

Where am I going wrong?

Outcomes