Advertisement
santiagohiguera

StyledWMSCoverageReader

Jan 24th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. /**
  2.  * A grid coverage readers backing onto a WMS server by issuing GetMap
  3.  * with a named style for layer
  4.  */
  5. public class StyledWMSCoverageReader extends WMSCoverageReader {
  6.    
  7.     /** The logger for the map module. */
  8.     static public final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.geotools.map");
  9.    
  10.     protected String styleName;
  11.     /**
  12.      * Builds a new WMS coverage reader
  13.      *
  14.      * @param wms
  15.      * @param layer
  16.      */
  17.     public StyledWMSCoverageReader(WebMapServer wms, Layer layer, String stylename) {
  18.         super(wms, layer);
  19.         this.styleName = stylename;
  20.     }
  21.  
  22.     /**
  23.      * Sets up a max request with the provided parameters, making sure it is compatible with
  24.      * the layers own native SRS list
  25.      * @param bbox
  26.      * @param width
  27.      * @param height
  28.      * @return
  29.      * @throws IOException
  30.      */
  31.     @Override
  32.     protected ReferencedEnvelope initMapRequest(ReferencedEnvelope bbox, int width, int height, Color backgroundColor)
  33.             throws IOException {
  34.         ReferencedEnvelope gridEnvelope = bbox;
  35.         String requestSrs = srsName;
  36.         try {
  37.             // first see if we can cascade the request in its native SRS
  38.             // we first look for an official epsg code
  39.             String code = null;
  40.             Integer epsgCode = CRS.lookupEpsgCode(bbox.getCoordinateReferenceSystem(), false);
  41.             if(epsgCode != null) {
  42.                 code = "EPSG:" + epsgCode;
  43.             } else {
  44.                 // otherwise let's make a fuller scan, but this method is more fragile...
  45.                 code = CRS.lookupIdentifier(bbox.getCoordinateReferenceSystem(), false);
  46.             }
  47.            
  48.             if (code != null && validSRS.contains(code)) {
  49.                 requestSrs = code;
  50.             } else {
  51.                 // first reproject to the map CRS
  52.                 gridEnvelope = bbox.transform(getCrs(), true);
  53.  
  54.                 // then adjust the form factor
  55.                 if (gridEnvelope.getWidth() < gridEnvelope.getHeight()) {
  56.                     height = (int) Math.round(width * gridEnvelope.getHeight()
  57.                             / gridEnvelope.getWidth());
  58.                 } else {
  59.                     width = (int) Math.round(height * gridEnvelope.getWidth()
  60.                             / gridEnvelope.getHeight());
  61.                 }
  62.             }
  63.         } catch (Exception e) {
  64.             throw (IOException) new IOException("Could not reproject the request envelope")
  65.                     .initCause(e);
  66.         }
  67.  
  68.         GetMapRequest mapRequest = wms.createGetMapRequest();
  69.         // for some silly reason GetMapRequest will list the layers in the opposite order...
  70.         List<Layer> reversed = new ArrayList<Layer>(layers);
  71.         Collections.reverse(reversed);
  72.         for (Layer layer : reversed) {
  73.             mapRequest.addLayer(layer, styleName);
  74.         }
  75.         mapRequest.setDimensions(width, height);
  76.         mapRequest.setFormat(format);
  77.         if(backgroundColor == null) {
  78.             mapRequest.setTransparent(true);
  79.         } else {
  80.             String rgba = Integer.toHexString(backgroundColor.getRGB());
  81.             String rgb = rgba.substring(2, rgba.length());
  82.             mapRequest.setBGColour("0x" + rgb.toUpperCase());
  83.             mapRequest.setTransparent(backgroundColor.getAlpha() < 255);
  84.         }
  85.  
  86.         try {
  87.             this.requestCRS = CRS.decode(requestSrs);
  88.         } catch(Exception e) {
  89.             throw new IOException("Could not decode request SRS " + requestSrs);
  90.         }
  91.        
  92.         ReferencedEnvelope requestEnvelope = gridEnvelope;
  93.         mapRequest.setBBox(requestEnvelope);
  94.         mapRequest.setSRS(requestSrs);
  95.        
  96.         this.mapRequest = mapRequest;
  97.         this.requestedEnvelope = gridEnvelope;
  98.         this.width = width;
  99.         this.height = height;
  100.  
  101.         return gridEnvelope;
  102.     }
  103.    
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement