# How to get the projected coordinate system of an IGeometry?

609
5
05-23-2012 02:52 PM
by
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)
{
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);
}
}
**************************************************
Tags (5)
1 Solution

Accepted Solutions
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.
5 Replies
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.
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
by
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;
}
**************************************************
MVP Frequent Contributor
Sui,

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

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