001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/model/coverage/grid/GeoTIFFGridCoverageWriter.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: klaus.greve@uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.model.coverage.grid;
045:
046: import java.awt.image.BufferedImage;
047: import java.io.IOException;
048: import java.io.OutputStream;
049: import java.util.Map;
050:
051: import org.deegree.datatypes.parameter.GeneralParameterValueIm;
052: import org.deegree.datatypes.parameter.InvalidParameterNameException;
053: import org.deegree.datatypes.parameter.InvalidParameterValueException;
054: import org.deegree.datatypes.parameter.OperationParameterIm;
055: import org.deegree.datatypes.parameter.ParameterNotFoundException;
056: import org.deegree.framework.log.ILogger;
057: import org.deegree.framework.log.LoggerFactory;
058: import org.deegree.i18n.Messages;
059: import org.deegree.io.geotiff.GeoTiffWriter;
060: import org.deegree.model.crs.CoordinateSystem;
061: import org.deegree.model.spatialschema.Envelope;
062: import org.deegree.model.spatialschema.GeometryFactory;
063: import org.opengis.pt.PT_Envelope;
064:
065: /**
066: * This class encapsulates functionality for writing a <tt>GridCoverage</tt> as a GeoTIFF to a
067: * defined destination. Ths destination will be given as an <tt>OutputStream</tt>. The current
068: * implementation is limited to support <tt>ImageGridCoverage</tt>s to be written as GeoTIFF.
069: *
070: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
071: * @author last edited by: $Author: apoth $
072: *
073: * @version $Revision: 9343 $, $Date: 2007-12-27 05:30:32 -0800 (Thu, 27 Dec 2007) $
074: */
075: public class GeoTIFFGridCoverageWriter extends
076: AbstractGridCoverageWriter {
077:
078: private static final ILogger LOG = LoggerFactory
079: .getLogger(GeoTIFFGridCoverageWriter.class);
080:
081: /**
082: * @param destination
083: * @param metadata
084: * @param subNames
085: * @param currentSubname
086: * @param format
087: */
088: public GeoTIFFGridCoverageWriter(Object destination,
089: Map<String, Object> metadata, String[] subNames,
090: String currentSubname, Format format) {
091: super (destination, metadata, subNames, currentSubname, format);
092: }
093:
094: /**
095: * Writes the specified grid coverage. The GridCoverage will be written in its original size
096: * (width/height).
097: *
098: * @see #write(GridCoverage, int, int, GeneralParameterValue[])
099: *
100: * @param coverage
101: * The {@linkplain GridCoverage grid coverage} to write.
102: * @param parameters
103: * An optional set of parameters. Should be any or all of the parameters returned by
104: * {@link "org.opengis.coverage.grid.Format#getWriteParameters"}.
105: * @throws InvalidParameterNameException
106: * if a parameter in <code>parameters</code> doesn't have a recognized name.
107: * @throws InvalidParameterValueException
108: * if a parameter in <code>parameters</code> doesn't have a valid value.
109: * @throws ParameterNotFoundException
110: * if a parameter was required for the operation but was not provided in the
111: * <code>parameters</code> list.
112: * @throws FileFormatNotCompatibleWithGridCoverageException
113: * if the grid coverage can't be exported in the
114: * {@linkplain org.opengis.coverage.grid.GridCoverageWriter#getFormat writer format}.
115: * @throws IOException
116: * if the export failed for some other input/output reason, including
117: * {@link javax.imageio.IIOException} if an error was thrown by the underlying image
118: * library.
119: */
120: public void write(GridCoverage coverage,
121: GeneralParameterValueIm[] parameters)
122: throws InvalidParameterNameException,
123: InvalidParameterValueException, ParameterNotFoundException,
124: IOException {
125:
126: int width = -1;
127: int height = -1;
128: for (int i = 0; i < parameters.length; i++) {
129: OperationParameterIm op = (OperationParameterIm) parameters[i]
130: .getDescriptor();
131: String name = op.getName();
132: if (name.equalsIgnoreCase("WIDTH")) {
133: Object o = op.getDefaultValue();
134: width = ((Integer) o).intValue();
135: } else if (name.equalsIgnoreCase("HEIGHT")) {
136: Object o = op.getDefaultValue();
137: height = ((Integer) o).intValue();
138: }
139: }
140:
141: OutputStream out = (OutputStream) destination;
142: AbstractGridCoverage igc = (AbstractGridCoverage) coverage;
143: BufferedImage bi = igc.getAsImage(width, height);
144: PT_Envelope ptenv = igc.getEnvelope();
145: double xmin = ptenv.minCP.ord[0];
146: double ymin = ptenv.minCP.ord[1];
147: double xmax = ptenv.maxCP.ord[0];
148: double ymax = ptenv.maxCP.ord[1];
149: CoordinateSystem crs = null;
150: Envelope envelope = GeometryFactory.createEnvelope(xmin, ymin,
151: xmax, ymax, null);
152: try {
153: double offset = 0;
154: double scaleFactor = 1;
155: if (metadata.get("offset") != null) {
156: offset = (Double) metadata.get("offset");
157: }
158: if (metadata.get("scaleFactor") != null) {
159: scaleFactor = (Double) metadata.get("scaleFactor");
160: }
161:
162: GeoTiffWriter gtw = new GeoTiffWriter(bi, envelope,
163: (xmax - xmin) / bi.getWidth(), (ymax - ymin)
164: / bi.getHeight(), crs, offset, scaleFactor);
165:
166: gtw.write(out);
167:
168: } catch (Exception e) {
169: LOG.logError(e.getMessage(), e);
170: throw new IOException(Messages
171: .getMessage("GC_ERROR_GEOTIFFWRITER"));
172: }
173: }
174:
175: /**
176: * Allows any resources held by this object to be released. The result of calling any other
177: * method subsequent to a call to this method is undefined. It is important for applications to
178: * call this method when they know they will no longer be using this
179: * <code>GridCoverageWriter</code>. Otherwise, the writer may continue to hold on to
180: * resources indefinitely.
181: *
182: * @throws IOException
183: * if an error occured while disposing resources (for example while flushing data
184: * and closing a file).
185: */
186: public void dispose() throws IOException {
187: ((OutputStream) destination).close();
188: }
189:
190: }
|