How to get the projected coordinate system of an IGeometry?

609
5
Jump to solution
05-23-2012 02:52 PM
SuiHuang
Occasional Contributor II
Hi Everybody:

    I made a function to convert X/Y coordinate to Lat/Long coordinates. The interface of the function is
=====================================================================================
public static void GetLatLongFromPlannerXY(esriSRProjCSType proj, double x, double y, out double longitude, out double latitude)
=====================================================================================
    which takes a predefined projected coordinate system (esriSRProjCSType) as an input.

    Now I want to use ArcObject code to retrieve the projected coordinate system of the point geometry (contains X/Y) to be converted instead of hard-coding it.
1. How can I do that?
2. On another hand, the function I wrote can only take predefined projected coordinate systems. Is there any way to improve it to take any projected coordinated systems?

Below is the function code I wrote, any suggestion will be welcome. Thank you!
*********************************************************
        public static void GetLatLongFromPlannerXY(esriSRProjCSType proj, double x, double y, out double longitude, out double latitude)
        {
            // esriSRProjCSType.esriSRProjCS_NAD1983UTM_15N
            ISpatialReferenceFactory factory = null;
            IProjectedCoordinateSystem projectedGCS = null;
            try
            {
                factory = new SpatialReferenceEnvironmentClass();
                projectedGCS = factory.CreateProjectedCoordinateSystem((int) proj);
                WKSPoint pointToConvert = new WKSPoint {X = x, Y = y};
                projectedGCS.Inverse(1, ref pointToConvert);
                longitude = pointToConvert.X;
                latitude = pointToConvert.Y;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Utils.ReleaseComObjects(factory, projectedGCS);
            }
        }
**************************************************
0 Kudos
1 Solution

Accepted Solutions
AllanMills
New Contributor III
The IGeometry interface already has SpatialReference as a property. You should be able to just check that it is of type IProjectedCoordinateSystem and cast it to IProjectedCoordinateSystem if it is.

View solution in original post

0 Kudos
5 Replies
AllanMills
New Contributor III
The IGeometry interface already has SpatialReference as a property. You should be able to just check that it is of type IProjectedCoordinateSystem and cast it to IProjectedCoordinateSystem if it is.

View solution in original post

0 Kudos
DennisGeasan
Occasional Contributor II
Not sure if I fully understand your question.  The function you have written will already take any of the pre-defined projected coordinate systems defined by the enumeration 'esriSRProjCSType.

If you asking about a way to determine the coordinate system of the X and Y parameters of your method 'GetLatLongFromPlannerXY' then the answer is no.  The actual XY values could be from any projected coordinate system.  The are simply just numbers.  Presumably thru user input your code would determine which projected coordinate system the XY pair represents and then your method can convert those values to their geographic (longitude/latitude) coordinate values.

DG
0 Kudos
SuiHuang
Occasional Contributor II
The IGeometry interface already has SpatialReference as a property. You should be able to just check that it is of type IProjectedCoordinateSystem and cast it to IProjectedCoordinateSystem if it is.


Thank you. I got it work as you suggested.

*****************************************
        public static void FindLatLong(IPoint xy, out double long, out double lat)
        {
                WKSPoint tmp = new WKSPoint { X = xy.X, Y = xy.Y };
                (xy.SpatialReference as IProjectedCoordinateSystem).Inverse(1, ref tmp);
                long = tmp.X;
                lat = tmp.Y;
        }
**************************************************
0 Kudos
DuncanHornby
MVP Frequent Contributor
Sui,

You should mark this thread as ANSWERED, this helps others.

Duncan
0 Kudos
jitendrapareek
New Contributor
I really impressed with this solution, it's worked�?�.great solution !!!!
0 Kudos