public ImageFormatter serviceRequest(IMapRequest req) throws IOException, WMSServiceException { ImageFormatter formatter = null; try { Sector reqExtent = (SingleFileLayer.this.isElevation) ? req.getExtentForElevationRequest() : req.getExtent(); if (null == this.intersects(reqExtent, SingleFileLayer.this.getBBox())) { String msg = Logging.getMessage( "WMS.Layer.OutOfCoverage", reqExtent.toString(), SingleFileLayer.this.getBBox().toString()); Logging.logger().severe(msg); throw new WMSServiceException(msg); } if (req.getHeight() <= 0 || req.getWidth() <= 0) { String msg = Logging.getMessage("generic.InvalidImageSize", req.getWidth(), req.getHeight()); Logging.logger().severe(msg); throw new WMSServiceException(msg); } DataRaster[] rasters = SingleFileLayer.this.rasters; if (null == rasters || 0 == rasters.length) { String msg = Logging.getMessage("nullValue.RasterIsNull"); Logging.logger().severe(msg); throw new WMSServiceException(msg); } double missingDataReplacement = (double) SingleFileLayer.this.nodataReplacement; try { String s = req.getBGColor(); if (null != s) { missingDataReplacement = Double.parseDouble(s); } } catch (Exception e) { missingDataReplacement = (double) SingleFileLayer.this.nodataReplacement; } DataRaster raster = rasters[0]; if (raster instanceof BufferedImageRaster) { BufferedImageRaster reqRaster = new BufferedImageRaster( req.getWidth(), req.getHeight(), Transparency.TRANSLUCENT, reqExtent); raster.drawOnTo(reqRaster); BufferedImage img = reqRaster.getBufferedImage(); this.makeNoDataTransparent( img, SingleFileLayer.this.nodataSignal, (short) missingDataReplacement); formatter = new BufferedImageFormatter(img); } else if (raster instanceof ByteBufferRaster) { AVList reqParams = new AVListImpl(); reqParams.setValue(AVKey.WIDTH, req.getWidth()); reqParams.setValue(AVKey.HEIGHT, req.getHeight()); reqParams.setValue(AVKey.SECTOR, reqExtent); reqParams.setValue( AVKey.BYTE_ORDER, AVKey.LITTLE_ENDIAN); // by default BIL is LITTLE ENDIAN reqParams.setValue(AVKey.PIXEL_FORMAT, AVKey.ELEVATION); String reqFormat = req.getFormat(); if (null != reqFormat && reqFormat.endsWith("32")) { reqParams.setValue(AVKey.DATA_TYPE, AVKey.FLOAT32); } else { reqParams.setValue(AVKey.DATA_TYPE, AVKey.INT16); } reqParams.setValue(AVKey.MISSING_DATA_REPLACEMENT, missingDataReplacement); ByteBufferRaster reqRaster = new ByteBufferRaster(req.getWidth(), req.getHeight(), reqExtent, reqParams); raster.drawOnTo(reqRaster); if (SingleFileLayer.this.convertFeetToMeters) { this.convertFeetToMeters(reqRaster); } formatter = new DataRasterFormatter(reqRaster); } else { String msg = Logging.getMessage( "generic.UnrecognizedImageSourceType", raster.getClass().getName()); Logging.logger().severe(msg); throw new WMSServiceException(msg); } } catch (WMSServiceException wmsse) { throw wmsse; } catch (Exception ex) { Logging.logger() .log( java.util.logging.Level.SEVERE, SingleFileLayer.this.getThreadId() + ex.getMessage(), ex); // throw new WMSServiceException( s ); } finally { } return formatter; }
protected void doDrawOnTo(BufferedImageRaster canvas) { Sector sector = this.getSector(); if (null == sector) { String message = Logging.getMessage("nullValue.SectorIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (!sector.intersects(canvas.getSector())) { return; } java.awt.Graphics2D g2d = null; java.awt.Shape prevClip = null; java.awt.Composite prevComposite = null; java.lang.Object prevInterpolation = null, prevAntialiasing = null; try { int canvasWidth = canvas.getWidth(); int canvasHeight = canvas.getHeight(); // Apply the transform that correctly maps the image onto the canvas. java.awt.geom.AffineTransform transform = this.computeSourceToDestTransform( this.getWidth(), this.getHeight(), this.getSector(), canvasWidth, canvasHeight, canvas.getSector()); AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); Rectangle2D rect = op.getBounds2D(this.getBufferedImage()); int clipWidth = (int) Math.ceil((rect.getMaxX() >= canvasWidth) ? canvasWidth : rect.getMaxX()); int clipHeight = (int) Math.ceil((rect.getMaxY() >= canvasHeight) ? canvasHeight : rect.getMaxY()); if (clipWidth <= 0 || clipHeight <= 0) { return; } g2d = canvas.getGraphics(); prevClip = g2d.getClip(); prevComposite = g2d.getComposite(); prevInterpolation = g2d.getRenderingHint(RenderingHints.KEY_INTERPOLATION); prevAntialiasing = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); // Set the alpha composite for appropriate alpha blending. g2d.setComposite(java.awt.AlphaComposite.SrcOver); g2d.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.drawImage(this.getBufferedImage(), transform, null); } // catch (java.awt.image.ImagingOpException ioe) // { // // If we catch a ImagingOpException, then the transformed image has a width or // height of 0. // // This indicates that there is no intersection between the source image and the // canvas, // // or the intersection is smaller than one pixel. // } // catch (java.awt.image.RasterFormatException rfe) // { // // If we catch a RasterFormatException, then the transformed image has a width or // height of 0. // // This indicates that there is no intersection between the source image and the // canvas, // // or the intersection is smaller than one pixel. // } catch (Throwable t) { String reason = WWUtil.extractExceptionReason(t); Logging.logger().log(java.util.logging.Level.SEVERE, reason, t); } finally { // Restore the previous clip, composite, and transform. try { if (null != g2d) { if (null != prevClip) g2d.setClip(prevClip); if (null != prevComposite) g2d.setComposite(prevComposite); if (null != prevInterpolation) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, prevInterpolation); if (null != prevAntialiasing) g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, prevAntialiasing); } } catch (Throwable t) { Logging.logger().log(java.util.logging.Level.FINEST, WWUtil.extractExceptionReason(t), t); } } }