1

I am trying to convert EPSG: 3857 to EPSG: 4326 with geotools. Here is my code :

public void convertEPSG3857toEPSG4326() throws FactoryException, TransformException {
    try {
        CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326");
        CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");
        MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, false);
        GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
        Point point = geometryFactory.createPoint(new Coordinate(x, y));
        Point targetPoint = (Point) JTS.transform(point, transform);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Problem for : x = " + x + " y = " + y);
    }
}

For the geojson :

{"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::3857"}},"type":"FeatureCollection","features":[{"type":"Feature","properties":{"FID":123,"TEXT":"yolo","attaches":[]},"geometry":{"type":"Polygon","coordinates":[[[9830100,3500101.25],[9699943,2332626],[10394759,2332626],[9830100,3500101.25]]]}}]}

I keep getting this error :

Problem for : x = 9830100.0 y = 3500101.25
Problem for : x = 9699943.0 y = 2332626.0
Problem for : x = 1.0394759E7 y = 2332626.0
2017-05-15T10:59:39.710+0530  WARNING  Possible use of "Popular Visualisation Pseudo Mercator" projection outside its valid area.
Longitude 3499741°15.0'E is out of range (±180°).
Latitude 9829740°00.0'N is out of range (±90°).
org.geotools.referencing.operation.projection.ProjectionException: Latitude 9829740°00.0'N is too close to a pole.
at org.geotools.referencing.operation.projection.Mercator$Spherical.transformNormalized(Mercator.java:209)
at org.geotools.referencing.operation.projection.MapProjection.transform(MapProjection.java:897)
at org.geotools.referencing.operation.projection.MapProjection.transform(MapProjection.java:938)
at org.geotools.referencing.operation.transform.ConcatenatedTransformDirect.transform(ConcatenatedTransformDirect.java:81)
at org.geotools.geometry.jts.DefaultCoordinateSequenceTransformer.transform(DefaultCoordinateSequenceTransformer.java:123)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.projectCoordinateSequence(GeometryCoordinateSequenceTransformer.java:295)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transformPoint(GeometryCoordinateSequenceTransformer.java:282)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transform(GeometryCoordinateSequenceTransformer.java:148)
at org.geotools.geometry.jts.JTS.transform(JTS.java:442)
at gis.GISCoordinate.convertEPSG3857toEPSG4326(GISCoordinate.java:52)
at gis.FeatureCollector.getAllGisFeature(FeatureCollector.java:65)
at MergeGis.main(MergeGis.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

org.geotools.referencing.operation.projection.ProjectionException: Latitude 9699583°00.0'N is too close to a pole.
at org.geotools.referencing.operation.projection.Mercator$Spherical.transformNormalized(Mercator.java:209)
at org.geotools.referencing.operation.projection.MapProjection.transform(MapProjection.java:897)
at org.geotools.referencing.operation.projection.MapProjection.transform(MapProjection.java:938)
at org.geotools.referencing.operation.transform.ConcatenatedTransformDirect.transform(ConcatenatedTransformDirect.java:81)
at org.geotools.geometry.jts.DefaultCoordinateSequenceTransformer.transform(DefaultCoordinateSequenceTransformer.java:123)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.projectCoordinateSequence(GeometryCoordinateSequenceTransformer.java:295)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transformPoint(GeometryCoordinateSequenceTransformer.java:282)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transform(GeometryCoordinateSequenceTransformer.java:148)
at org.geotools.geometry.jts.JTS.transform(JTS.java:442)
at gis.GISCoordinate.convertEPSG3857toEPSG4326(GISCoordinate.java:52)
at gis.FeatureCollector.getAllGisFeature(FeatureCollector.java:65)
at MergeGis.main(MergeGis.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

org.geotools.referencing.operation.projection.ProjectionException: Latitude 10394399°00.0'N is too close to a pole.
at org.geotools.referencing.operation.projection.Mercator$Spherical.transformNormalized(Mercator.java:209)
at org.geotools.referencing.operation.projection.MapProjection.transform(MapProjection.java:897)
at org.geotools.referencing.operation.projection.MapProjection.transform(MapProjection.java:938)
at org.geotools.referencing.operation.transform.ConcatenatedTransformDirect.transform(ConcatenatedTransformDirect.java:81)
at org.geotools.geometry.jts.DefaultCoordinateSequenceTransformer.transform(DefaultCoordinateSequenceTransformer.java:123)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.projectCoordinateSequence(GeometryCoordinateSequenceTransformer.java:295)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transformPoint(GeometryCoordinateSequenceTransformer.java:282)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transform(GeometryCoordinateSequenceTransformer.java:148)
at org.geotools.geometry.jts.JTS.transform(JTS.java:442)
at gis.GISCoordinate.convertEPSG3857toEPSG4326(GISCoordinate.java:52)
at gis.FeatureCollector.getAllGisFeature(FeatureCollector.java:65)
at MergeGis.main(MergeGis.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

org.geotools.referencing.operation.projection.ProjectionException: Latitude 9829740°00.0'N is too close to a pole.
at org.geotools.referencing.operation.projection.Mercator$Spherical.transformNormalized(Mercator.java:209)
at org.geotools.referencing.operation.projection.MapProjection.transform(MapProjection.java:897)
at org.geotools.referencing.operation.projection.MapProjection.transform(MapProjection.java:938)
at org.geotools.referencing.operation.transform.ConcatenatedTransformDirect.transform(ConcatenatedTransformDirect.java:81)
at org.geotools.geometry.jts.DefaultCoordinateSequenceTransformer.transform(DefaultCoordinateSequenceTransformer.java:123)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.projectCoordinateSequence(GeometryCoordinateSequenceTransformer.java:295)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transformPoint(GeometryCoordinateSequenceTransformer.java:282)
at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transform(GeometryCoordinateSequenceTransformer.java:148)
at org.geotools.geometry.jts.JTS.transform(JTS.java:442)
at gis.GISCoordinate.convertEPSG3857toEPSG4326(GISCoordinate.java:52)
at gis.FeatureCollector.getAllGisFeature(FeatureCollector.java:65)
at MergeGis.main(MergeGis.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

What is the problem here ?

1 Answer 1

4

The reason your coordinates are giving you problems is you are putting your source coordinates in as degrees. It has nothing to do with proximity to the poles (the first problem point is in China and the second is in India and neither are anywhere near the poles or even the ante-meridian). Read the fifth and sixth lines of the error message:

Longitude 3499741°15.0'E is out of range (±180°).
Latitude 9829740°00.0'N is out of range (±90°).

So, you have entered EPSG:3857 coordinates (which are in meters) as EPSG:4326 which are degrees.

Since your function is called convertEPSG3857toEPSG4326(), you presumably want to change these lines of code:

CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326");
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");

to:

CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:3857");
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.