I have a point FC in a mxd file in WGS_1984_Web_Mercator_Auxiliary_Sphere, and displays in Decimal Degrees (Long/Lat). I published it as a mapservice, and retrieved the features in my SOE project. However, the features' Extent are not in Long/Lat. Then, I used the piece of code below to convert the X/Y from GCS to PCS (in meters) but it does not work. What's wrong in my code? Thanks if you can help.
public IPoint CoordinateConvert(IPoint point, string[] srID) { | |
ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironmentClass(); | |
ISpatialReference srTo; | |
IProjectedCoordinateSystem pcs = srFactory.CreateProjectedCoordinateSystem(Convert.ToInt32(srID[0])); | |
srTo = pcs; | |
IGeometry geometry; | |
geometry = point; | |
geometry.SpatialReference = point.SpatialReference; | |
geometry.Project(srTo); | |
point = geometry as IPoint; | |
double x, y; | |
point.QueryCoords(out x, out y); | |
System.Diagnostics.Debug.Print("X: " + x.ToString()); | |
System.Diagnostics.Debug.Print("Y: " + y.ToString()); | |
return point; | |
} |
Solved! Go to Solution.
Yes, the second code sample you posted does convert lat/lon to "Web Mercator" which is also known by several well-known IDs including EPSG:3857, OpenLayers:900913, and Esri:102100. The sm_b line isn't needed because this projected coordinate system uses a sphere so only the semimajor axis also known as "a" is used. What srID were you passing in the first code sample? It should have been 3857 or 102100.
Melita
referring to .net - How to convert lat long to meters using Mercator projection in C# - Geographic Information Sy... , I coded like that and solve this problem. Thanks for your reviewing.
public IPoint Convert_GCS2PCS(IPoint pt) { | |
double lon = projDeg2Rad(pt.X); | |
double lat = projDeg2Rad(pt.Y); |
double lon0 = 0; | |
double sm_a = 6378137.0 ; | |
double sm_b = 6356752.314; |
double x = x = sm_a*(lon-lon0); | |
double y = sm_a*System.Math.Log((System.Math.Sin(lat)+1)/System.Math.Cos(lat)); |
IPoint pt2 = new Point(); | |
pt2.X = x; | |
pt2.Y = y; | |
return pt2; | |
} |
public double projDeg2Rad(double deg) { | |
return (deg / 180.0 * System.Math.PI); | |
} |
Yes, the second code sample you posted does convert lat/lon to "Web Mercator" which is also known by several well-known IDs including EPSG:3857, OpenLayers:900913, and Esri:102100. The sm_b line isn't needed because this projected coordinate system uses a sphere so only the semimajor axis also known as "a" is used. What srID were you passing in the first code sample? It should have been 3857 or 102100.
Melita
You are right. I use 102100. I used another piece of code for general conversion about 1.5 yrs. for the same purpose. After I dig it out, I will update the thread. Thanks.