Problem converting coordinates

Apr 19, 2012 at 12:19 PM
Edited Apr 20, 2012 at 11:04 AM

This is my demo code: (References ProjNet Nuget package, DotSpatial.Projections Nuget package)

class Program
    {
        static void Main(string[] args)
        {
            double[] originalCoords = { 23.57892F, 37.94712F };

            // ProjNet To EPSG:3785
            // Found in: http://projnet.codeplex.com/wikipage?title=CommonWellKnownText&referringTitle=Home 
            ICoordinateTransformation ct = TransformToEPSG3785();
            double[] resultProjNet3785 = ct.MathTransform.Transform(originalCoords);

            // ProjNet To EPSG:3857
            // Found in: http://alastaira.wordpress.com/2011/01/23/the-google-maps-bing-maps-spherical-mercator-projection/
            ct = TransformToEPSG3857();
            double[] resultProjNet3857 = ct.MathTransform.Transform(originalCoords);

            // DotSpatial.Projections to Web Mercator
            double[] resultDotSpatial = { originalCoords[0], originalCoords[1] };
            double[] z = { 0 };
            Reproject.ReprojectPoints(resultDotSpatial, z, KnownCoordinateSystems.Geographic.World.WGS1984, KnownCoordinateSystems.Projected.World.WebMercator, 0, 1);

            // Manual Convertion
            // Found in: http://alastaira.wordpress.com/2011/01/23/the-google-maps-bing-maps-spherical-mercator-projection/
            double[] resultManual = { 0F, 0F };
            resultManual[0] = originalCoords[0] * 20037508.34 / 180;
            resultManual[1] = Math.Log(Math.Tan((90 + originalCoords[1]) * Math.PI / 360)) / (Math.PI / 180);
            resultManual[1] = resultManual[1] * 20037508.34 / 180;

            // ProjNet To Google Mercator
            // Getting from KnownCoordinateSystems.Projected.World.WebMercator.ToEsriString()
            // Getting error: {"Projection Mercator_Auxiliary_Sphere is not supported."}
            //ct = TransformToWebMercator();
            //double[] resultProjNetWebMercator = ct.MathTransform.Transform(originalCoords);


            Console.WriteLine("Original Coordinates in 'EPSG:4326' convert to 'EPSG:3857':");
            Console.WriteLine("X: {0:.000000}, Y: {1:.000000}", originalCoords[0], originalCoords[1]);
            Console.WriteLine();
            
            Console.WriteLine("      with ProjNet To EPSG:3785 >> X: {0:.000000}, Y: {1:.000000}", resultProjNet3785[0], resultProjNet3785[1]);
            Console.WriteLine("      with ProjNet To EPSG:3857 >> X: {0:.000000}, Y: {1:.000000}", resultProjNet3857[0], resultProjNet3857[1]);
            Console.WriteLine("    with DotSpatial.Projections >> X: {0:.000000}, Y: {1:.000000}", resultDotSpatial[0], resultDotSpatial[1]);

            //Console.WriteLine("   with ProjNet To Web Mercator >> X: {0:.000000}, Y: {1:.000000}", resultProjNetWebMercator[0], resultProjNetWebMercator[1]);
            Console.WriteLine("          manual Convertion     >> X: {0:.000000}, Y: {1:.000000}", resultManual[0], resultManual[1]);

            Console.Read();
        }

        public static ICoordinateTransformation TransformToEPSG3785()
        {
            var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
            var epsg3785 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\", \"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\", \"6055\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\", \"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\", \"3785\"]]");

            return ctf.CreateFromCoordinateSystems(epsg4326, epsg3785);
        }

        public static ICoordinateTransformation TransformToEPSG3857()
        {
            var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
            var epsg3857 = cf.CreateFromWkt("PROJCS[\"WGS 84 / Pseudo-Mercator\", GEOGCS[\"WGS 84\", DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563, AUTHORITY[\"EPSG\", \"7030\"]], AUTHORITY[\"EPSG\", \"6326\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.01745329251994328, AUTHORITY[\"EPSG\", \"9122\"]], AUTHORITY[\"EPSG\", \"4326\"]], PROJECTION[\"Mercator_1SP\"], PARAMETER[\"central_meridian\", 0], PARAMETER[\"scale_factor\", 1], PARAMETER[\"false_easting\", 0], PARAMETER[\"false_northing\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"X\", EAST], AXIS[\"Y\", NORTH], AUTHORITY[\"EPSG\", \"3857\"]]");
            var epsg3857mod = cf.CreateFromWkt("PROJCS[\"WGS 84 / Pseudo-Mercator\", GEOGCS[\"WGS 84\", DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563, AUTHORITY[\"EPSG\", \"7030\"]], AUTHORITY[\"EPSG\", \"6326\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.01745329251994328, AUTHORITY[\"EPSG\", \"9122\"]], AUTHORITY[\"EPSG\", \"4326\"]], PROJECTION[\"Mercator_1SP\"], PARAMETER[\"latitude_of_origin\", 0], PARAMETER[\"central_meridian\", 0], PARAMETER[\"scale_factor\", 1], PARAMETER[\"false_easting\", 0], PARAMETER[\"false_northing\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"X\", EAST], AXIS[\"Y\", NORTH], AUTHORITY[\"EPSG\", \"3857\"]]");

            return ctf.CreateFromCoordinateSystems(epsg4326, epsg3857mod);
        }

        public static ICoordinateTransformation TransformToWebMercator()
        {
            var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
            var webMercator = cf.CreateFromWkt("PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223562997]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.0174532925199433]], PROJECTION[\"Mercator_Auxiliary_Sphere\"],PARAMETER[\"False_Easting\",0],PARAMETER[\"False_Northing\",0],PARAMETER[\"Central_Meridian\",0],PARAMETER[\"Standard_Parallel_1\",0],PARAMETER[\"Scale_Factor\",1],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Meter\",1]]");

            return ctf.CreateFromCoordinateSystems(epsg4326, webMercator);
        }
    }

The output of the program is:

Original Coordinates in 'EPSG:4326' convert to 'EPSG:3857':
X: 23,578920, Y: 37,947121

      with ProjNet To EPSG:3785 >> X: 2624793,408418, Y: 4545668,936282
      with ProjNet To EPSG:3857 >> X: 2624793,408418, Y: 4545679,990060
    with DotSpatial.Projections >> X: 2624793,408418, Y: 4571958,427384
          manual Convertion     >> X: 2624793,408053, Y: 4571958,426748

Clearly there is a problem with ProjNet and Y coordinate conversion:

I believe neither of the above WKT are correct. I think converting EPSG:4326 to EPSG:3857 would be a routine job - everybody is projecting layers over Google/Bing/OpenStreeMap.

Can anybody help, pointing me the correct WKT, in order ProjNet converts coordinates correctly?

 

Thanks,
George J.

Apr 19, 2012 at 9:44 PM
Edited Apr 20, 2012 at 11:02 AM

I think my quest came to an end; here is the final test code:

    class Program
    {
        static void Main(string[] args)
        {
            double[] originalCoords = { 23.57892F, 37.94712F };

            // ProjNet To EPSG:3785
            // Found in: http://projnet.codeplex.com/wikipage?title=CommonWellKnownText&referringTitle=Home
            ICoordinateTransformation ct = TransformToEPSG3785();
            double[] resultProjNet3785 = ct.MathTransform.Transform(originalCoords);

            // ProjNet To EPSG:3857
            // Found in: http://alastaira.wordpress.com/2011/01/23/the-google-maps-bing-maps-spherical-mercator-projection/
            ct = TransformToEPSG3857();
            double[] resultProjNet3857 = ct.MathTransform.Transform(originalCoords);

            // DotSpatial.Projections to Web Mercator
            double[] resultDotSpatial = { originalCoords[0], originalCoords[1] };
            double[] z = { 0 };
            Reproject.ReprojectPoints(resultDotSpatial, z, KnownCoordinateSystems.Geographic.World.WGS1984, KnownCoordinateSystems.Projected.World.WebMercator, 0, 1);

            // Manual Convertion
            // Found in: http://alastaira.wordpress.com/2011/01/23/the-google-maps-bing-maps-spherical-mercator-projection/
            double[] resultManual = { 0F, 0F };
            resultManual[0] = originalCoords[0] * 20037508.34 / 180;
            resultManual[1] = Math.Log(Math.Tan((90 + originalCoords[1]) * Math.PI / 360)) / (Math.PI / 180);
            resultManual[1] = resultManual[1] * 20037508.34 / 180;

            // ProjNet To Web Mercator
            // Getting from KnownCoordinateSystems.Projected.World.WebMercator.ToEsriString()
            // Getting error: {"Projection Mercator_Auxiliary_Sphere is not supported."}
            // Modification: Changed PROJECTION to Mercator, added PARAMETER[\"latitude_of_origin\", 0]
            ct = TransformToDotSpatialWebMercator();
            double[] resultProjNetWebMercator = ct.MathTransform.Transform(originalCoords);

            // ProjNet To Google Mercator
            // Found in SharpMap examples WMS server example
            ct = TransformToGoogleMercator();
            double[] resultProjNetGoogleMercator = ct.MathTransform.Transform(originalCoords);

            // ProjNet To Mercator
            // Getting from Goolge Mercator example => targetCs.WKT
            // Getting error: {"Projection Google Mercator is not supported."}
            // Modification: Changed PROJECTION to Mercator_1SP
            ct = TransformToMercator(false);
            double[] resultProjNetMercator = ct.MathTransform.Transform(originalCoords);

            Console.WriteLine("Original Coordinates in 'EPSG:4326' convert to 'EPSG:3857':");
            Console.WriteLine("X: {0:.000000}, Y: {1:.000000}", originalCoords[0], originalCoords[1]);
            Console.WriteLine();

            Console.WriteLine("      with ProjNet To EPSG:3785 >> X: {0:.000000}, Y: {1:.000000}", resultProjNet3785[0], resultProjNet3785[1]);
            Console.WriteLine("      with ProjNet To EPSG:3857 >> X: {0:.000000}, Y: {1:.000000}", resultProjNet3857[0], resultProjNet3857[1]);
            Console.WriteLine("    with DotSpatial.Projections >> X: {0:.000000}, Y: {1:.000000}", resultDotSpatial[0], resultDotSpatial[1]);

            Console.WriteLine("   with ProjNet To Web Mercator >> X: {0:.000000}, Y: {1:.000000}", resultProjNetWebMercator[0], resultProjNetWebMercator[1]);
            Console.WriteLine("  with ProjNet programmatically >> X: {0:.000000}, Y: {1:.000000}", resultProjNetGoogleMercator[0], resultProjNetGoogleMercator[1]);
            Console.WriteLine("       with ProjNet To Mercator >> X: {0:.000000}, Y: {1:.000000}", resultProjNetMercator[0], resultProjNetMercator[1]);
            Console.WriteLine("              manual Convertion >> X: {0:.000000}, Y: {1:.000000}", resultManual[0], resultManual[1]);

            ct = TransformToMercator(true);
            originalCoords = ct.MathTransform.Transform(resultProjNetMercator);
            Console.WriteLine("         reverse transformation >> X: {0:.000000}, Y: {1:.000000}", originalCoords[0], originalCoords[1]);

            Console.Read();
        }

        public static ICoordinateTransformation TransformToEPSG3785()
        {
            var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
            var epsg3785 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\", \"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\", \"6055\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\", \"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\", \"3785\"]]");

            return ctf.CreateFromCoordinateSystems(epsg4326, epsg3785);
        }

        public static ICoordinateTransformation TransformToEPSG3857()
        {
            var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
            var epsg3857 = cf.CreateFromWkt("PROJCS[\"WGS 84 / Pseudo-Mercator\", GEOGCS[\"WGS 84\", DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563, AUTHORITY[\"EPSG\", \"7030\"]], AUTHORITY[\"EPSG\", \"6326\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.01745329251994328, AUTHORITY[\"EPSG\", \"9122\"]], AUTHORITY[\"EPSG\", \"4326\"]], PROJECTION[\"Mercator_1SP\"], PARAMETER[\"central_meridian\", 0], PARAMETER[\"scale_factor\", 1], PARAMETER[\"false_easting\", 0], PARAMETER[\"false_northing\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"X\", EAST], AXIS[\"Y\", NORTH], AUTHORITY[\"EPSG\", \"3857\"]]");
            var epsg3857mod = cf.CreateFromWkt("PROJCS[\"WGS 84 / Pseudo-Mercator\", GEOGCS[\"WGS 84\", DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563, AUTHORITY[\"EPSG\", \"7030\"]], AUTHORITY[\"EPSG\", \"6326\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.01745329251994328, AUTHORITY[\"EPSG\", \"9122\"]], AUTHORITY[\"EPSG\", \"4326\"]], PROJECTION[\"Mercator_1SP\"], PARAMETER[\"latitude_of_origin\", 0], PARAMETER[\"central_meridian\", 0], PARAMETER[\"scale_factor\", 1], PARAMETER[\"false_easting\", 0], PARAMETER[\"false_northing\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"X\", EAST], AXIS[\"Y\", NORTH], AUTHORITY[\"EPSG\", \"3857\"]]");

            return ctf.CreateFromCoordinateSystems(epsg4326, epsg3857mod);
        }

        public static ICoordinateTransformation TransformToDotSpatialWebMercator()
        {
            var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
            var webMercator = cf.CreateFromWkt("PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223562997]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.0174532925199433]], PROJECTION[\"Mercator_Auxiliary_Sphere\"],PARAMETER[\"False_Easting\",0],PARAMETER[\"False_Northing\",0],PARAMETER[\"Central_Meridian\",0],PARAMETER[\"Standard_Parallel_1\",0],PARAMETER[\"Scale_Factor\",1],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Meter\",1]]");
            var webMercatorMod = cf.CreateFromWkt("PROJCS[\"WGS_1984_Web_Mercator_Auxiliary_Sphere\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223562997]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.0174532925199433]], PROJECTION[\"Mercator\"],PARAMETER[\"latitude_of_origin\", 0],PARAMETER[\"False_Easting\",0],PARAMETER[\"False_Northing\",0],PARAMETER[\"Central_Meridian\",0],PARAMETER[\"Standard_Parallel_1\",0],PARAMETER[\"Scale_Factor\",1],PARAMETER[\"Auxiliary_Sphere_Type\",0.0],UNIT[\"Meter\",1]]");

            return ctf.CreateFromCoordinateSystems(epsg4326, webMercatorMod);
        }

        public static ICoordinateTransformation TransformToGoogleMercator()
        {
            CoordinateSystemFactory csFac = new CoordinateSystemFactory();
            CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();
            IGeographicCoordinateSystem sourceCs = csFac.CreateGeographicCoordinateSystem(
                "WGS 84", ProjNet.CoordinateSystems.AngularUnit.Degrees,
                HorizontalDatum.WGS84,
                PrimeMeridian.Greenwich,
                new AxisInfo("north", AxisOrientationEnum.North),
                new AxisInfo("east", AxisOrientationEnum.East)
                );

            List<ProjectionParameter> parameters = new List<ProjectionParameter>();
            parameters.Add(new ProjectionParameter("semi_major", 6378137.0));
            parameters.Add(new ProjectionParameter("semi_minor", 6378137.0));
            parameters.Add(new ProjectionParameter("latitude_of_origin", 0.0));
            parameters.Add(new ProjectionParameter("central_meridian", 0.0));
            parameters.Add(new ProjectionParameter("scale_factor", 1.0));
            parameters.Add(new ProjectionParameter("false_easting", 0.0));
            parameters.Add(new ProjectionParameter("false_northing", 0.0));
            IProjection projection = csFac.CreateProjection("Google Mercator", "mercator_1sp", parameters);
            IProjectedCoordinateSystem targetCs = csFac.CreateProjectedCoordinateSystem(
                "Google Mercator",
                sourceCs,
                projection,
                ProjNet.CoordinateSystems.LinearUnit.Metre,
                new AxisInfo("East", AxisOrientationEnum.East),
                new AxisInfo("North", AxisOrientationEnum.North)
                );

            return ctFac.CreateFromCoordinateSystems(sourceCs, targetCs);
        }

        public static ICoordinateTransformation TransformToMercator(bool Invert)
        {
            var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
            var mercator = cf.CreateFromWkt("PROJCS[\"Google Mercator\",GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System 1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9102\"]],AXIS[\"north\",NORTH],AXIS[\"east\",EAST]],PROJECTION[\"Google Mercator\"],PARAMETER[\"semi_major\",6378137],PARAMETER[\"semi_minor\",6378137],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"East\",EAST],AXIS[\"North\",NORTH]]");
            var mercatorMod = cf.CreateFromWkt("PROJCS[\"Google Mercator\",GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System 1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9102\"]],AXIS[\"north\",NORTH],AXIS[\"east\",EAST]],PROJECTION[\"Mercator_1SP\"],PARAMETER[\"semi_major\",6378137],PARAMETER[\"semi_minor\",6378137],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"East\",EAST],AXIS[\"North\",NORTH]]");

            if (Invert)
                return ctf.CreateFromCoordinateSystems(mercatorMod, epsg4326);
            else
                return ctf.CreateFromCoordinateSystems(epsg4326, mercatorMod);
        }
    }

What's new in this version:

  • It took some time to figure out why WKT produced from DotSpatial.Projections wasn’t working; I had to change the Projection to "Mercator" and add PARAMETER[\"latitude_of_origin\", 0] in order ProjNet to parse the WKT. Despite the efforts the results wasn't as promising, as DotSpatial.Projection itself.
  • Following D_Guidi's advice, I went through SharpMap's examples and I found out a projection, that it was generated programmatically, but it was transforming the coordinates correctly.
  • I got the WKT code from the working projection (targetCs.WKT). I used this WKT with ProjNet in order to create a transformation. I got the error "Projection Google Mercator is not supported.". Checking the original C# code that WKT code come from, I saw that Mercator was actually Mercator_1SP. Changing "Google Mercator" with "Mercator_1SP”, I got the coordinates transform correctly.

The results:

Original Coordinates in 'EPSG:4326' convert to 'EPSG:3857':
X: 23,578920, Y: 37,947121

      with ProjNet To EPSG:3785 >> X: 2624793,408418, Y: 4545668,936282
      with ProjNet To EPSG:3857 >> X: 2624793,408418, Y: 4545679,990060
    with DotSpatial.Projections >> X: 2624793,408418, Y: 4571958,427384
   with ProjNet To Web Mercator >> X: 2624793,408418, Y: 4545679,995424
  with ProjNet programmatically >> X: 2624793,408418, Y: 4571958,427384
       with ProjNet To Mercator >> X: 2624793,408418, Y: 4571958,427384
              manual Convertion >> X: 2624793,408053, Y: 4571958,426748
         reverse transformation >> X: 23,578920, Y: 37,947121

So, the WKT that one can use in ProjNet to transform 'EPSG:4326' to 'EPSG:3857' and back is:

PROJCS["Google Mercator",
	GEOGCS["WGS 84",
		DATUM["World Geodetic System 1984",
			SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],
			AUTHORITY["EPSG","6326"]],
		PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
		UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9102"]],
		AXIS["north",NORTH],
		AXIS["east",EAST]],
	PROJECTION["Mercator_1SP"],
	PARAMETER["semi_major",6378137],
	PARAMETER["semi_minor",6378137],
	PARAMETER["latitude_of_origin",0],
	PARAMETER["central_meridian",0],
	PARAMETER["scale_factor",1],
	PARAMETER["false_easting",0],
	PARAMETER["false_northing",0],
	UNIT["metre",1,AUTHORITY["EPSG","9001"]],
	AXIS["East",EAST],
	AXIS["North",NORTH]
]

I don't know if it matches the official WKT code for EPSG:3857, or if the WKT code above is correct, but it just works.

 

Regards,
George J.

 

Jul 8, 2013 at 8:38 PM
This worked for me!!! Thank you for doing the research.