3

I have to write code using java and GeoTools to do a WMS request, get the image and save it to a specific location on my computer. I've followed the GeoTools WMS tutorial and the code compiles without errors, but I don't know how to check if it had worked or how to save the requested image?

Here is the GetMap request with all the necessary parameters: http://ows.mundialis.de/services/service?request=GetMap&service=WMS&version=1.3.0&layers=OSM-Overlay-WMS&styles=default&crs=EPSG%3A4326&bbox=47.75,12.98,47.86,13.12&&width=2000&height=2000&format=image/png&transparent=true

Here is the code:

public class WmsConnectorMaven {

    public static void main(String[] args) {

        URL url = null;
        try {
          url = new URL("http://ows.mundialis.de/services/service?service=wms&version=1.3.0&request=GetCapabilities");
        } catch (MalformedURLException e) {
          //will not happen
        }

        WebMapServer wms = null;
        try {
          wms = new WebMapServer(url);
          GetMapRequest request = wms.createGetMapRequest();
          request.addLayer("OSM-Overlay-WMS", "defualt");
          request.setFormat("image/png");
          request.setDimensions("800", "800"); //sets the dimensions of the image to be returned from the server
          request.setTransparent(true);
          request.setSRS("EPSG:4326");
          request.setBBox("47.75,12.98,47.86,13.12");

          GetMapResponse response = (GetMapResponse) wms.issueRequest(request);
          BufferedImage image = ImageIO.read(response.getInputStream());

         /* File outputfile = new File("saved.png");
            ImageIO.write(image, "png", outputfile); */

         // FileOutputStream img = new FileOutputStream("C:\\Users\\Edhem\\Desktop\\WMSimage.png");
        } catch (IOException e) {
          //There was an error communicating with the server
          //For example, the server is down
        } catch (ServiceException e) {
          //The server returned a ServiceException (unusual in this case)
        } 



    }
}

1 Answers1

1

You need to check the contentType of the returned response and make a decision based on that value. Something like:

try {
  GetMapResponse response = wms.issueRequest(getMapRequest);
  if (response.getContentType().equalsIgnoreCase(format)) {
    BufferedImage image = ImageIO.read(response.getInputStream());
    return image;
  } else {
    StringWriter writer = new StringWriter();
    IOUtils.copy(response.getInputStream(), writer);
    String error = writer.toString();
    System.out.println(error);
    return null;
  }
} catch (ServiceException | IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return null;
} 

UPDATE

I just ran your code with my checks and I get:

<?xml version="1.0"?>
<ServiceExceptionReport version="1.3.0"
  xmlns="http://www.opengis.net/ogc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/ogc
http://schemas.opengis.net/wms/1.3.0/exceptions_1_3_0.xsd">
    <ServiceException code="StyleNotDefined">unsupported styles: defualt</ServiceException>
</ServiceExceptionReport>

removing the (misspelt) "defualt" gives (which I guess is right):

enter image description here

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
  • I've tried your code and it gives me the error on the return lines, that they cannot return a value in a void method. Besides that, how do I chose a specific layer from the WMS, this seems to list all of the nemed layers on the server `for ( Layer layer : WMSUtils.getNamedLayers(capabilities) ) { request.addLayer(layer); }` – Kokonut_Bananco Aug 09 '19 at 08:29
  • in your main method you would not need a return, your code specifies a layer name and style. – Ian Turton Aug 09 '19 at 09:32
  • After omitting the return lines this is what I get ` missing parameters ['layers', 'styles', 'crs'] ` – Kokonut_Bananco Aug 09 '19 at 09:42
  • Your error report helped me to make it work. I basically added `Layer[] layers = WMSUtils.getNamedLayers(capabilities); request.addLayer(layers[1]);` in order to solve the missing "layer" and "style" parameters, along with `request.setSRS("EPSG:4326");` to solve the missing "crs" parameter. With that in place it simply works with `try { GetMapResponse response = (GetMapResponse) wms.issueRequest(request); BufferedImage image = ImageIO.read(response.getInputStream()); ImageIO.write(image, "png", new File("C:\\Users\\Edhem\\Desktop\\salzburg.png"));` ... Thanks Ian! – Kokonut_Bananco Aug 09 '19 at 11:34