001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wcs.responses.coverage;
006:
007: import org.geotools.coverage.grid.GridCoverage2D;
008: import org.geotools.coverage.grid.io.AbstractGridFormat;
009: import org.geotools.coverage.grid.io.imageio.GeoToolsWriteParams;
010: import org.geotools.gce.geotiff.GeoTiffFormat;
011: import org.geotools.gce.geotiff.GeoTiffWriteParams;
012: import org.opengis.coverage.grid.GridCoverageWriter;
013: import org.opengis.parameter.GeneralParameterValue;
014: import org.opengis.parameter.ParameterValueGroup;
015: import org.vfny.geoserver.ServiceException;
016: import org.vfny.geoserver.global.GeoServer;
017: import org.vfny.geoserver.wcs.responses.CoverageResponseDelegate;
018: import java.io.IOException;
019: import java.io.OutputStream;
020:
021: /**
022: * DOCUMENT ME!
023: *
024: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last
025: * modification)
026: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last
027: * modification)
028: */
029: public class GeoTIFFCoverageResponseDelegate implements
030: CoverageResponseDelegate {
031: /**
032: *
033: * @uml.property name="sourceCoverage"
034: * @uml.associationEnd multiplicity="(0 1)"
035: */
036: private GridCoverage2D sourceCoverage;
037:
038: public GeoTIFFCoverageResponseDelegate() {
039: }
040:
041: public boolean canProduce(String outputFormat) {
042: if (outputFormat.equalsIgnoreCase("geotiff")) {
043: return true;
044: }
045:
046: return false;
047: }
048:
049: public void prepare(String outputFormat, GridCoverage2D coverage)
050: throws IOException {
051: this .sourceCoverage = coverage;
052: }
053:
054: public String getContentType(GeoServer gs) {
055: return "image/tiff";
056: }
057:
058: /**
059: * DOCUMENT ME!
060: *
061: * @return DOCUMENT ME!
062: */
063: public String getContentEncoding() {
064: return null;
065: }
066:
067: /**
068: * DOCUMENT ME!
069: *
070: * @return DOCUMENT ME!
071: */
072: public String getContentDisposition() {
073: return "attachment;filename=" + this .sourceCoverage.getName()
074: + ".tiff";
075: }
076:
077: public void encode(OutputStream output) throws ServiceException,
078: IOException {
079: if (sourceCoverage == null) {
080: throw new IllegalStateException(
081: "It seems prepare() has not been called"
082: + " or has not succeed");
083: }
084:
085: final GeoTiffFormat format = new GeoTiffFormat();
086: final GeoTiffWriteParams wp = new GeoTiffWriteParams();
087: wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT);
088: wp.setCompressionType("LZW");
089: wp.setCompressionQuality(0.75F);
090: wp.setTilingMode(GeoToolsWriteParams.MODE_EXPLICIT);
091: wp.setTiling(256, 256);
092:
093: final ParameterValueGroup writerParams = format
094: .getWriteParameters();
095: writerParams.parameter(
096: AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName()
097: .toString()).setValue(wp);
098:
099: GridCoverageWriter writer = format.getWriter(output);
100: writer.write(sourceCoverage,
101: (GeneralParameterValue[]) writerParams.values()
102: .toArray(new GeneralParameterValue[1]));
103:
104: writer.dispose();
105:
106: this.sourceCoverage.dispose();
107: this.sourceCoverage = null;
108: }
109: }
|