WebMercator -> WGS84 is wrong

Feb 9, 2010 at 11:08 PM

Hello,

Using the WKTs for webmercator / wgs84 from http://projnet.codeplex.com/wikipage?title=CommonWellKnownText, when i reproject a point from webMercator to WGS84 i'm seeing ~.25 degree shift in Y.  PROJ.4 works fine.

 

Here is the code:

 

string webMercatorWkt = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Junk.webMercatorWkt.txt")).ReadToEnd();
            string wgs84wkt = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Junk.wgs84Wkt.txt")).ReadToEnd();
            CoordinateSystemFactory csFact = new CoordinateSystemFactory();
            var webMercator = csFact.CreateFromWkt(webMercatorWkt);
            var wgs84 = csFact.CreateFromWkt(wgs84wkt);
            CoordinateTransformationFactory xformFact = new CoordinateTransformationFactory();
            var xform = xformFact.CreateFromCoordinateSystems(webMercator, wgs84);
            double[] sourcePoint = new double[] { -9161319.573, 4563663.442 };
            var targetpoint = xform.MathTransform.Transform(sourcePoint);

string webMercatorWkt = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Junk.webMercatorWkt.txt")).ReadToEnd();

 

            string wgs84wkt = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Junk.wgs84Wkt.txt")).ReadToEnd();

 

            CoordinateSystemFactory csFact = new CoordinateSystemFactory();

            var webMercator = csFact.CreateFromWkt(webMercatorWkt);

            var wgs84 = csFact.CreateFromWkt(wgs84wkt);

 

            CoordinateTransformationFactory xformFact = new CoordinateTransformationFactory();

            var xform = xformFact.CreateFromCoordinateSystems(webMercator, wgs84);

 

            double[] sourcePoint = new double[] { -9161319.573, 4563663.442 };

            var targetpoint = xform.MathTransform.Transform(sourcePoint);

 

 

Can anyone tell me what the problem might be?

 

Apr 15, 2010 at 5:46 AM

I faced the same problem and I used this instead:

        double[] toMercator(double lon, double lat)
        {
            double x = lon * 20037508.34 / 180;
            double y = Math.Log(Math.Tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
            y = y * 20037508.34 / 180;
            return new double[] {x, y};
        }
        double[] inverseMercator (double x, double y) {
            double lon = (x / 20037508.34) * 180;
            double lat = (y / 20037508.34) * 180;

            lat = 180/Math.PI * (2 * Math.Atan(Math.Exp(lat * Math.PI / 180)) - Math.PI / 2);
            return new double[] {lon, lat};
        }

It is a pure mathematical implementation of the transform.

Jan 23, 2011 at 11:12 PM

Sounds like you might be creating a Mercator projection with respect to the WGS84 ellipsoid, rather than relative to a sphere.

Try adding:

PARAMETER["semi_minor",6378137]

into the list of parameters in the WKT of your projection.

See here for more details: http://alastaira.wordpress.com/2011/01/23/the-google-maps-bing-maps-spherical-mercator-projection/