Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valid WKTs for SRIDs 4326 and 3857? #62

Closed
Rumpelstinsk opened this issue Jan 8, 2020 · 6 comments
Closed

Valid WKTs for SRIDs 4326 and 3857? #62

Rumpelstinsk opened this issue Jan 8, 2020 · 6 comments

Comments

@Rumpelstinsk
Copy link

I get WKT from https://epsg.io/4326 and https://epsg.io/3857

However that codes throw me an exception when I try to use them:

private void changeSRID(Geometry item, out double lat, out double lng)
        {
            //From coordinate sytem 4326
            var wkt4326 = "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.0174532925199433,AUTHORITY[\"EPSG\", \"9122\"]],AUTHORITY[\"EPSG\", \"4326\"]]";
            GeoAPI.CoordinateSystems.IGeographicCoordinateSystem fromCS =
                ProjNet.Converters.WellKnownText.CoordinateSystemWktReader.Parse(wkt4326, Encoding.UTF8) as GeoAPI.CoordinateSystems.IGeographicCoordinateSystem;

            //To Corordinate system 3857
            var wkt3857 = "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.0174532925199433,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],EXTENSION[\"PROJ4\",\"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs\"],AUTHORITY[\"EPSG\",\"3857\"]]";
            GeoAPI.CoordinateSystems.IProjectedCoordinateSystem toCS =
                ProjNet.Converters.WellKnownText.CoordinateSystemWktReader.Parse(wkt3857, Encoding.UTF8) as GeoAPI.CoordinateSystems.IProjectedCoordinateSystem;
                       
            //Transformation
            CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory();
            ICoordinateTransformation trans = ctfac.CreateFromCoordinateSystems(fromCS, toCS);

            double[] fromPoint = new double[] { item.Coordinates[0].X, item.Coordinates[0].Y };
            double[] toPoint = trans.MathTransform.Transform(fromPoint);
            lat = toPoint[0];
            lng = toPoint[1];
        }

I take a look to SRID.csv file of your project, but i couldn't find any ID with 3857.
So, Where can I find the correct WKT for both SRIDs?

@FObermaier
Copy link
Member

You should use

// Coordinate systems
var cs4386 = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84;
var cs3857 = ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator;

// Transformation
var ctfac = new CoordinateTransformationFactory();
var trans = ctfac.CreateFromCoordinateSystems(cs4326, cs3857);

@Rumpelstinsk
Copy link
Author

Thanks for the help. That did the trick

@Bernoulli-IT
Copy link

Bernoulli-IT commented May 9, 2020

Yes this helped. But why don't the WKT definitions, as mentioned by OP, lead to the correct results? That is odd and feels uncomfortable.

@DGuidi
Copy link
Contributor

DGuidi commented May 20, 2020

@Bernoulli-IT I tried to run code posted by @Rumpelstinsk and no exception is thrown

@Bernoulli-IT
Copy link

@Bernoulli-IT tried to run code posted by @Rumpelstinsk and no exception is thrown

I think I misplaced my previous comment as I bumped into a similar issue where a conversion resulted in an incorrect y value. My bad.

@airbreather
Copy link
Member

Yes this helped. But why don't the WKT definitions, as mentioned by OP, lead to the correct results? That is odd and feels uncomfortable.

This is a valid question. From #37 (comment):

The issue is that the projection "Mercator_1SP" needs additional help to perform the correct conversion.
Out of the box, proj4 wouldn't compute the correct result either, that is why the Extension token is there.

Add [PARAMETER["semi_minor",6378137]] anywhere in the parameter block and you should be all set.
Or use ProjectedCoordinateSystem.WebMercator in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants