0

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) :

Google Maps vs 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 ?

10
  • 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.
    – Ian Turton
    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 ?
    – Tim Autin
    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
    – Ian Turton
    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 ?
    – Tim Autin
    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
    – Ian Turton
    Commented Jul 10, 2015 at 9:48

1 Answer 1

1

For what you are trying to do you could use the WMS datastore in GeoTools to import the layers you need:

see https://gitlab.com/snippets/6355 for a full example.

8
  • Great, thanks ! So with this code, if I ask the layer in EPSG:3857, and that the server doesn't provide it, GeoTools will do the maths for me ?? Or it will send a ServiceException ? I need to be sure, because I'm working on Android and to use this code I need to port the wms GeoTools extension to Android Java...
    – Tim Autin
    Commented Jul 10, 2015 at 13:18
  • Well the doc says that the SRS needs to be one of the layers' : docs.geotools.org/latest/javadocs/org/geotools/data/wms/request/… So, the code you provided will probably do no more than what I'm already doing, no ?
    – Tim Autin
    Commented Jul 10, 2015 at 13:38
  • you do have to ask for a supported CRS, then you have to reproject the image returned. And I don't think most of this is going to work on Android due to SPI issues
    – Ian Turton
    Commented Jul 10, 2015 at 13:50
  • Mmh okay, but I have to do it for each tile requested by the Google Maps API. I'm asking 256*256 images, but if I reproject them, they won't fit, right ? Seems I will only support EPSG:3857, that sucks :/
    – Tim Autin
    Commented Jul 10, 2015 at 14:47
  • which wms doesn't support 3857?
    – Ian Turton
    Commented Jul 10, 2015 at 15:36

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.