Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A grid coverage readers backing onto a WMS server by issuing GetMap
- * with a named style for layer
- */
- public class StyledWMSCoverageReader extends WMSCoverageReader {
- /** The logger for the map module. */
- static public final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.geotools.map");
- protected String styleName;
- /**
- * Builds a new WMS coverage reader
- *
- * @param wms
- * @param layer
- */
- public StyledWMSCoverageReader(WebMapServer wms, Layer layer, String stylename) {
- super(wms, layer);
- this.styleName = stylename;
- }
- /**
- * Sets up a max request with the provided parameters, making sure it is compatible with
- * the layers own native SRS list
- * @param bbox
- * @param width
- * @param height
- * @return
- * @throws IOException
- */
- @Override
- protected ReferencedEnvelope initMapRequest(ReferencedEnvelope bbox, int width, int height, Color backgroundColor)
- throws IOException {
- ReferencedEnvelope gridEnvelope = bbox;
- String requestSrs = srsName;
- try {
- // first see if we can cascade the request in its native SRS
- // we first look for an official epsg code
- String code = null;
- Integer epsgCode = CRS.lookupEpsgCode(bbox.getCoordinateReferenceSystem(), false);
- if(epsgCode != null) {
- code = "EPSG:" + epsgCode;
- } else {
- // otherwise let's make a fuller scan, but this method is more fragile...
- code = CRS.lookupIdentifier(bbox.getCoordinateReferenceSystem(), false);
- }
- if (code != null && validSRS.contains(code)) {
- requestSrs = code;
- } else {
- // first reproject to the map CRS
- gridEnvelope = bbox.transform(getCrs(), true);
- // then adjust the form factor
- if (gridEnvelope.getWidth() < gridEnvelope.getHeight()) {
- height = (int) Math.round(width * gridEnvelope.getHeight()
- / gridEnvelope.getWidth());
- } else {
- width = (int) Math.round(height * gridEnvelope.getWidth()
- / gridEnvelope.getHeight());
- }
- }
- } catch (Exception e) {
- throw (IOException) new IOException("Could not reproject the request envelope")
- .initCause(e);
- }
- GetMapRequest mapRequest = wms.createGetMapRequest();
- // for some silly reason GetMapRequest will list the layers in the opposite order...
- List<Layer> reversed = new ArrayList<Layer>(layers);
- Collections.reverse(reversed);
- for (Layer layer : reversed) {
- mapRequest.addLayer(layer, styleName);
- }
- mapRequest.setDimensions(width, height);
- mapRequest.setFormat(format);
- if(backgroundColor == null) {
- mapRequest.setTransparent(true);
- } else {
- String rgba = Integer.toHexString(backgroundColor.getRGB());
- String rgb = rgba.substring(2, rgba.length());
- mapRequest.setBGColour("0x" + rgb.toUpperCase());
- mapRequest.setTransparent(backgroundColor.getAlpha() < 255);
- }
- try {
- this.requestCRS = CRS.decode(requestSrs);
- } catch(Exception e) {
- throw new IOException("Could not decode request SRS " + requestSrs);
- }
- ReferencedEnvelope requestEnvelope = gridEnvelope;
- mapRequest.setBBox(requestEnvelope);
- mapRequest.setSRS(requestSrs);
- this.mapRequest = mapRequest;
- this.requestedEnvelope = gridEnvelope;
- this.width = width;
- this.height = height;
- return gridEnvelope;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement