getContext().setStatus(ILayer.WAIT);
WebMapServer wms = getWMS();
GetMapRequest request = wms.createGetMapRequest();
// put in default exception format we understand as a client
// (if suppoted by the server)
WMSCapabilities capabilities = wms.getCapabilities();
if( capabilities.getRequest().getGetMap().getFormats().contains(GetMapRequest.EXCEPTION_XML) ){
request.setExceptions(GetMapRequest.EXCEPTION_XML);
}
setImageFormat(wms, request);
if (monitor.isCanceled())
return;
double currScale = getContext().getViewportModel().getScaleDenominator();
List<ILayer> layers = getLayers();
for( int i = layers.size() - 1; i >= 0; i-- ) {
ILayer ilayer = layers.get(i);
Layer layer;
double minScale = 0;
double maxScale = Double.MAX_VALUE;
layer = ilayer.getResource(org.geotools.data.ows.Layer.class, null);
// check if there are min/max scale rules
StyleBlackboard sb = (StyleBlackboard) ilayer.getStyleBlackboard();
Style style = (Style) sb.lookup(Style.class);
if (style != null) {
Rule rule = style.getFeatureTypeStyles()[0].getRules()[0];
minScale = rule.getMinScaleDenominator();
maxScale = rule.getMaxScaleDenominator();
}
if (currScale >= minScale && currScale <= maxScale) {
// check for a wms style
StyleImpl wmsStyle = (StyleImpl) ilayer.getStyleBlackboard().get(
WMSStyleContent.WMSSTYLE);
if (wmsStyle != null) {
request.addLayer(layer, wmsStyle);
} else {
request.addLayer(layer);
}
}
}
if (monitor.isCanceled())
return;
List<Layer> wmsLayers = getWMSLayers();
if (wmsLayers == null || wmsLayers.isEmpty()){
endLayerStatus = ILayer.WARNING;
return;
}
// figure out request CRS
CoordinateReferenceSystem viewportCRS = getViewportCRS();
IMap map = getContext().getMap();
String requestCRScode = findRequestCRS(wmsLayers, viewportCRS, map);
// TODO: make findRequestCRS more efficient (we are running CRS.decode at *least* twice)
CoordinateReferenceSystem requestCRS = CRS.decode(requestCRScode);
// figure out viewport
// ReferencedEnvelope viewport;
// Envelope viewportBBox = getViewportBBox();
// CoordinateReferenceSystem viewportCRS = getViewportCRS();
// if (viewportBBox == null) {
// // change viewport to world
// viewportBBox = new Envelope(-180, 180, -90, 90);
// if (!DefaultGeographicCRS.WGS84.equals(viewportCRS)) { // reproject
// viewport = new ReferencedEnvelope(viewportBBox, DefaultGeographicCRS.WGS84);
// viewportBBox = viewport.transform(viewportCRS, true);
// }
// }
ReferencedEnvelope requestBBox = null;
Envelope backprojectedBBox = null; // request bbox projected to the viewport crs
// viewport = new ReferencedEnvelope(viewportBBox, viewportCRS);
// requestBBox = calculateRequestBBox(wmsLayers, viewport, requestCRS);
requestBBox = calculateRequestBBox(wmsLayers, bounds, requestCRS, capabilities.getVersion() );
// check that a request is needed (not out of a bounds, invalid, etc)
if (requestBBox == NILL_BOX) {
endLayerStatus = ILayer.WARNING;
return;
}
assert requestBBox.getCoordinateReferenceSystem().equals(requestCRS);
if (requestBBox.getCoordinateReferenceSystem().equals(viewportCRS)) {
backprojectedBBox = (Envelope) requestBBox;
} else {
backprojectedBBox = (Envelope) requestBBox.transform(viewportCRS, true);
}
if (WMSPlugin.isDebugging(Trace.RENDER)) {
WMSPlugin.trace("Viewport CRS: " + viewportCRS.getName()); //$NON-NLS-1$
WMSPlugin.trace("Request CRS: " + requestCRS.getName()); //$NON-NLS-1$
WMSPlugin.trace("Context Image bounds: " + getContext().getImageBounds()); //$NON-NLS-1$
WMSPlugin.trace("Request BBox bounds: " + requestBBox); //$NON-NLS-1$
WMSPlugin.trace("Backprojected request bounds: " + backprojectedBBox); //$NON-NLS-1$
}
Service wmsService = capabilities.getService();
Dimension maxDimensions = new Dimension(wmsService.getMaxWidth(), wmsService
.getMaxHeight());
// Dimension imageDimensions = calculateImageDimensions(getContext().getMapDisplay()
// .getDisplaySize(), maxDimensions, getViewportBBox(), backprojectedBBox);
Dimension imageDimensions = calculateImageDimensions(getContext().getImageSize(), maxDimensions, bounds, backprojectedBBox);
if (imageDimensions.height < 1 || imageDimensions.width < 1) {
endLayerStatus = ILayer.WARNING;
return;
}
request.setDimensions(imageDimensions.width + "", imageDimensions.height + ""); //$NON-NLS-1$ //$NON-NLS-2$
// epsg could be under identifiers or authority.
Set<ReferenceIdentifier> identifiers = requestCRS.getIdentifiers();
String srs = identifiers.isEmpty() ? EPSG_4326 : identifiers.iterator().next().toString();
request.setSRS(srs); // EPSG_4326
request.setBBox( requestBBox );
// request.setBBox(requestBBox.getMinX() + "," + requestBBox.getMinY()+ "," + requestBBox.getMaxX()+ "," + requestBBox.getMaxY());
if (monitor.isCanceled())
return;