# GeoTools, convert CRS for Google Maps

I'm trying to use GeoTools to convert coordinates from EPSG:3857 to various CRS, in order to display WMS layers on a Google Map.

For example, I want to display a layer in the EPSG:4326 CRS. I have the coordinates in EPSG:3857, and I need to convert them in EPSG:4326 to ask the server.

Here is what I'm doing :

``````// The coordinates are in meters in the EPSG:3857 CRS
double[] lowerLeft = new double[] { longitudeMin, latitudeMin};
double[] upperRight = new double[] { longitudeMax, latitudeMax };

CoordinateReferenceSystem srcCrs = CRS.decode("EPSG:3857", true);
CoordinateReferenceSystem destCrs = CRS.decode("EPSG:4326", true);

MathTransform transform = CRS.findMathTransform(srcCrs, destCrs, true);

transform.transform(lowerLeft, 0, lowerLeft, 0, 1);
transform.transform(upperRight, 0, upperRight, 0, 1);

// Check if axis order is NORTH_EAST (= LAT_LON) or EAST_NORTH (= LON_LAT) :
String bbox;
if(CRS.getAxisOrder(_crs) == CRS.AxisOrder.NORTH_EAST) {

bbox = lowerLeft[0] + "," + lowerLeft[1] + "," + upperRight[0] + "," + upperRight[1];
}
else {

bbox = lowerLeft[1] + "," + lowerLeft[0] + "," + upperRight[1] + "," + upperRight[0];
}
``````

I have no error and it seems to be working, but the map is not displayed correctly, as you can see on the image below (on the left, the correctly displayed Google Maps, on the right, my WMS layer) :

So, the question is, what am I doing wrong ? Is everything working properly, and I just can't display a WMS in EPSG:4326 (or any other CRS) on my EPSG:3857 Google Maps ?

• Your question seems confused, to display the WMS layer on Google maps you need it to be in 3857 not 4326 - so you should ask the WMS for the map in 3857 using the coordinates you have, The WMS server takes care of the reprojection for you. Commented Jul 10, 2015 at 8:47
• Yes, but I'd like to handle WMS servers that does not provide the map in 3857. So I'd like to ask the map to the server for example in 4326, and compute the correct values to give to the server in order to get the right image. Is it possible ? Commented Jul 10, 2015 at 9:08
• That isn't possible the way you describe it - you will need some code to do the reprojection (somewhere) - the easy way is use GeoServer to cascade it docs.geoserver.org/latest/en/user/data/cascaded/wms.html Commented Jul 10, 2015 at 9:13
• Thanks, it didn't know this possibility. But if GeoServer can do it, I should also be able to do it with GeoTools, right ? Do you know what happens when GeoServer do the reprojection ? Commented Jul 10, 2015 at 9:20
• Of course GT can do it - read in WMS using WMS store, then reproject the coverage (as above) and then send as new WMS to Google Maps Commented Jul 10, 2015 at 9:48