Reputation: 591
I'm using GeoTools 12.2 for developing java class library project.
Firstly, I'm working on GeoTools WMS module with this guide. The point that I was failed is doing get map request so that I could get capabilities document and layers etc.
It contains 3 layers (States,Rivers,Cities)
I'm using structure to get map operation like below.
GetMapRequest getMapRequest = wms.createGetMapRequest();//wms is my WebMapServer object
getMapRequest.addLayer(tempLayer);//tempLayer contains states layer
GetMapResponse response = (GetMapResponse) wms.issueRequest(getMapRequest);
BufferedImage image = ImageIO.read(response.getInputStream());
I also tried other methods in guide to do GetMapRequest but I can't succeed, always getting NullPointerException to BufferedImage object.
What is your suggestions? Thanks in advance.
Upvotes: 1
Views: 804
Reputation: 10976
You need to set some more parameters for your request, the WMS getMapResponse doesn't provide any defaults for several of them (as they are unique to your request/map). So you need at least the following:
private BufferedImage getLayer(Layer l) {
GetMapRequest getMapRequest = wms.createGetMapRequest();
getMapRequest.addLayer(l);
getMapRequest.setBBox(l.getEnvelope(DefaultGeographicCRS.WGS84));
getMapRequest.setDimensions(200, 400);
getMapRequest.setFormat("image/png");
getMapRequest.setSRS("CRS:84");
System.out.println(getMapRequest.getFinalURL());
try {
GetMapResponse response = wms.issueRequest(getMapRequest);
BufferedImage image = ImageIO.read(response.getInputStream());
return image;
} catch (ServiceException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
In general to avoid getting an empty image you can do some error checking on the response:
if (response.getContentType().equalsIgnoreCase("image/png")) {
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;
}
which will give you an XML encoded error to tell you what went wrong:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<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="InvalidFormat">
Parameter 'bbox' can not be empty.
</ServiceException>
</ServiceExceptionReport>
Upvotes: 1