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");
}