[GIS] How to convert PCS based point X/Y into GCS Lat/Lon using ArcObjects

arcobjectsconvertcoordinates

There are so many permutations of questions I've tried to find the answer to my question from various sources, but I have yet to actually find an answer the way that makes sense to me (and works).

I'm using ArcObjects in a ArcMap add-in, and I have shape file loaded with sample data that comes in a Projected Coordinate System.

I want to convert a feature's points (IPoint) which are X/Y coordinates, into it's BASE GCS Lat/Lon values.

For example:

The Data Source in the Shape File shows (Custom since Factory Code shows ZERO):

Projected Coordinate System: NAD27_BLM_3N_ftUS
Projection: Transverse_Mercator
false_easting: 1640416.67000000
false_northing: 0.00000000
central_meridian: -165.00000000
scale_factor: 0.99960000
latitude_of_origin: 0.00000000
Linear Unit: Foot_US

Geographic Coordinate System: GCS_North_American_1927
Datum: D_North_American_1927
Prime Meridian: Greenwich
Angular Unit: Degree

And I want to convert the X/Y coordinates to be Lat/Lon instead with GCS NAD 27.

There is no transformation, it's the same Datum.

I have tried to use the following code:

IPoint geographicPoint = new Point();
geographicPoint.X = _pcsX;
geographicPoint.Y = _pcsY;
geographicPoint.Z = _z;
geographicPoint.M = _m;
geographicPoint.SpatialReference = _inputSpatialReference; // PCS

ISpatialReference outputSpatialReference = SpatialReference.GenerateSpatialReference(outputCoordinateSystemWKID, false); // GCS

((IGeometry2)geographicPoint).ProjectEx(outputSpatialReference, esriTransformDirection.esriTransformReverse, null, false, 0, 0);

_gcsLat = geographicPoint.Y;
_gcsLong = geographicPoint.X;

I have also tried to use the Project() method as well instead of ProjectEx:

((IGeometry2)geographicPoint).Project(outputSpatialReference);

however, in both cases, I just get the exact same X/Y coordinates in the X/Y values of the geographicPoint, when I am expecting the X/Y values to now be the Long/Lat as I have requested the output coordinate system that is a Geographic Coordinate System.

Source Point:

X: 2440016.02472701
Y: 25980973.0785536

After Projection to 4267:

LAT: 25980973.0785536
LON: 2440016.02472701

Can anyone help?

As Requested: the PRJ file of the input document:
PROJCS["NAD27_BLM_3N_ftUS",GEOGCS["GCS_North_American_1927",DATUM["D_North_American_1927",SPHEROID["Clarke_1866",6378206.4,294.9786982]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["false_easting",1640416.67],PARAMETER["false_northing",0],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-165],PARAMETER["scale_factor",0.9996],UNIT["Foot_US",0.30480060960121924]]

Best Answer

Here's some revised code that I tested successfully:

x -165.997618623476, y 72.0016465340226

Here's the prj file contents:

PROJCS["NAD27_BLM_3N_ftUS",GEOGCS["GCS_North_American_1927",DATUM["D_North_American_1927",SPHEROID["Clarke_1866",6378206.4,294.9786982]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-165],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",1640416.67],PARAMETER["false_northing",0],UNIT["Foot_US",0.30480060960121924]]

Here's the code:

public static void TestProjectToGCS()
{
    string prjFile = @"D:\projects\blm3n.prj";

    var t = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
    var srf = Activator.CreateInstance(t) as ISpatialReferenceFactory2;
    var sr = srf.CreateESRISpatialReferenceFromPRJFile(prjFile);
    IPoint p = new PointClass();
    p.PutCoords(1527538.32, 26211344.31);
    p.SpatialReference = sr;
    var p2 = (IPoint)ProjectToGCS(p);
    Debug.Print("x {0}, y {1}", p2.X, p2.Y);
}
public static IGeometry ProjectToGCS(IGeometry inGeom)
{
    if (!(inGeom.SpatialReference is IProjectedCoordinateSystem5))
        throw new Exception("geometry is not in a projected coordinate system");

    var outGeom = ((IClone)inGeom).Clone() as IGeometry;
    var outSR = ((IProjectedCoordinateSystem5)outGeom.SpatialReference).GeographicCoordinateSystem as ISpatialReference;
    outGeom.Project(outSR);
    return outGeom;            
}