orbisgis / cts

Projection library in Java
GNU Lesser General Public License v3.0
50 stars 16 forks source link

Error when transforming coordinates from WGS84 (EPSG:4326) to web spherical mercator (EPSG:3857) #83

Closed JuanManuelMR closed 7 years ago

JuanManuelMR commented 7 years ago

I have tried the following code:

`try { double[] coordBogota = new double[2]; coordBogota[0] = 4.710988599999999; coordBogota[1] = -74.072092; double[] coordSingapur = new double[2]; coordSingapur[0] = 1.282858; coordSingapur[1] = 103.85304100000008; double[] coordIslazul = new double[2]; coordIslazul[0] = 40.363734; coordIslazul[1] = -3.737319999999954;

        CoordinateReferenceSystem wgs84 = cRSFactory.getCRS("EPSG:4326");
        CoordinateReferenceSystem mercator = cRSFactory.getCRS("EPSG:3857");
        //CoordinateReferenceSystem mercator = cRSFactory.getCRS("ESRI:102100");
        GeodeticCRS sourceGCRSwgs84 = (GeodeticCRS) wgs84;
        GeodeticCRS targetGCRSmercator = (GeodeticCRS) mercator;
        List<CoordinateOperation> coordOps = CoordinateOperationFactory.createCoordinateOperations(sourceGCRSwgs84, targetGCRSmercator);

        if (coordOps.size() != 0) {
           for (CoordinateOperation op : coordOps) {
               double[] dd  = op.transform(coordBogota);
               for (int i = 0; i < dd.length; i++) {
                   System.out.println(dd[i]); // for debugging
               }
               dd  = op.transform(coordSingapur);
               for (int i = 0; i < dd.length; i++) {
                   System.out.println(dd[i]); // for debugging
               }
               dd  = op.transform(coordIslazul);
               for (int i = 0; i < dd.length; i++) {
                   System.out.println(dd[i]); // for debugging
               }
           }
        }

    } catch (CRSException | IllegalCoordinateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }`

The values returned are not the correct ones. I have checked it with ArcMap from ESRI and http://twcc.fr/.

Expected Value : returned Value (Bogota) X / Y -8245667.56 : -12544724.515674358 / 525016.75 : 524424.8520849167

(Singapore) X / Y 11560867.64 : 19971868.880408563 / 142819.03 : 142807.09932007737

(Islazul, Madrid) X / Y -416036.56 : -416331.8963126612 / 4918940.75 : 4493270.315395144

ebocher commented 7 years ago

Thanks for the feedback. Please specify the CTS version.

mukoki commented 7 years ago

This is an axis order problem. JuanManuelMR entered input coordinates in lat/lon order while CTS uses lon/lat order. I'm not sure what is the best choice for CTS as axis order has not been defined from the begining by OGC standards and situation is now very confused. Anyway, I think we should not change now without a good reason to do so.

ebocher commented 7 years ago

Yes sorry +1 for @mukoki I close this issue