01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.vfny.geoserver.wms.responses.map.geotiff;
06:
07: import java.awt.image.RenderedImage;
08: import java.io.IOException;
09: import java.io.OutputStream;
10: import java.util.logging.Level;
11: import java.util.logging.Logger;
12:
13: import javax.imageio.ImageIO;
14: import javax.imageio.stream.ImageOutputStream;
15:
16: import org.geotools.coverage.FactoryFinder;
17: import org.geotools.coverage.grid.GridCoverage2D;
18: import org.geotools.coverage.grid.GridCoverageFactory;
19: import org.geotools.gce.geotiff.GeoTiffWriter;
20: import org.geotools.geometry.GeneralEnvelope;
21: import org.vfny.geoserver.global.WMS;
22: import org.vfny.geoserver.wms.WmsException;
23: import org.vfny.geoserver.wms.responses.DefaultRasterMapProducer;
24:
25: /**
26: * Map producer for GeoTiff output format. It basically relies on the GeoTiff
27: * module of geotools.
28: *
29: *
30: * @author Simone Giannecchini, GeoSolutions
31: *
32: */
33: public class GeoTiffMapProducer extends DefaultRasterMapProducer {
34:
35: /** A logger for this class. */
36: private static final Logger LOGGER = org.geotools.util.logging.Logging
37: .getLogger("org.vfny.geoserver.responses.wms.map.geotiff");
38:
39: /** GridCoverageFactory. */
40: private final static GridCoverageFactory factory = FactoryFinder
41: .getGridCoverageFactory(null);
42:
43: /**
44: * Constructo for a {@link GeoTiffMapProducer}.
45: *
46: * @param oformat
47: * output format we want the image to be encoded in.
48: * @param mime_type
49: * for the requested output format.
50: * @param wms
51: * that is asking us to encode the image.
52: */
53: public GeoTiffMapProducer(String oformat, String mime_type, WMS wms) {
54: super (oformat, mime_type, wms);
55: }
56:
57: public void formatImageOutputStream(RenderedImage image,
58: OutputStream outStream) throws WmsException, IOException {
59: // crating a grid coverage
60: final GridCoverage2D gc = factory.create("geotiff", image,
61: new GeneralEnvelope(mapContext.getAreaOfInterest()));
62:
63: // tiff
64: if (LOGGER.isLoggable(Level.FINE)) {
65: LOGGER.fine("Writing tiff image ...");
66: }
67:
68: // do we want it to be 8 bits?
69: if (this .format.equalsIgnoreCase("image/tiff8")
70: || (this .mapContext.getPaletteInverter() != null)) {
71: image = forceIndexed8Bitmask(image);
72: }
73:
74: // writing it out
75: final ImageOutputStream imageOutStream = ImageIO
76: .createImageOutputStream(outStream);
77: final GeoTiffWriter writer = new GeoTiffWriter(imageOutStream);
78: writer.write(gc, null);
79: imageOutStream.flush();
80: imageOutStream.close();
81:
82: if (LOGGER.isLoggable(Level.FINE)) {
83: LOGGER.fine("Writing tiff image done!");
84: }
85: }
86:
87: public String getContentDisposition() {
88: // can be null
89: return null;
90: }
91: }
|