2

At the Moment I try to load a WMS-Map into a Java Geotools Project. I followed every Step in the Userguide of Geotools. (here the Link: http://docs.geotools.org/latest/userguide/extension/wms/wms.html#getmaprequest)

At the end of the Guide it shows me how to Convert my GetMapResponse to a BufferedImage.

Until now I did not found a method, to get my Image into the MapContet. The ImageLab Tutorial from Geotools imports an Image but as a File and not a BufferedImage. http://docs.geotools.org/latest/userguide/tutorial/raster/image.html

So Here the Code i created from the userguide

public void loadWMS() {
    URL url = null;
    BufferedImage image = null;
    try {
        url = new URL(
                "HIDDENURLHERE");
    } catch (MalformedURLException e) {
        view.setStatus("WMS-Url Fehlerhaft");
    }

    WebMapServer wms = null;
    try {
        wms = new WebMapServer(url);
    } catch (IOException e) {
        view.setStatus(
                "Fehler beim Verbinden Server evtl. Server offline");
    } catch (ServiceException e) {
        view.setStatus("Server sendet eine ServiceException");
    } catch (SAXException e) {
        view.setStatus(
                "Parsen der Server-Antwort nicht möglich oder nich valie Capabilities");
    }
    WMSCapabilities capabilities = wms.getCapabilities();

    GetMapRequest request = wms.createGetMapRequest();

    request.setFormat("image/png");
    request.setDimensions("600", "600");
    request.setTransparent(true);
    request.setSRS("EPSG:4326");
    request.setBBox("-180, -85.0511287798, 180, 85.011287798");
    for (Layer layer : WMSUtils.getNamedLayers(capabilities)) {
        request.addLayer(layer);
    }
    try {
        GetMapResponse response = wms.issueRequest(request);
        image = ImageIO.read(response.getInputStream());
    } catch (ServiceException e) {
        view.setStatus("Server sendet eine ServiceException");
    } catch (IOException e) {
        view.setStatus(
                "Fehler beim Verbinden Server evtl. Server offline");
    }
1

You need to use a WMSLayer object rather than requesting the image yourself. So something like:

for (Layer layer : WMSUtils.getNamedLayers(capabilities)) {
    map.add(new WMSLayer(wms, layer));
}

though I'd probably add some sort of selection functionality and error checking :-)

There is also an addLayer(layer) method that would allow you to build up a layered map in one go:

  public WMSLayer getWMSLayer() {
    String[] layers = this.layers.split(",");
    if (layers.length > 0) {
      WMSLayer ret = new WMSLayer(wms, layerList.get(layers[0]));

      for (int i = 1; i < layers.length; i++) {
        ret.addLayer(layerList.get(layers[i]));
      }
      return ret;
    }
    return null;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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