001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/model/coverage/grid/ImageGridCoverageWriter.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.io.IOException;
047: import java.io.OutputStream;
048: import java.util.Map;
049:
050: import org.deegree.datatypes.parameter.GeneralParameterValueIm;
051: import org.deegree.datatypes.parameter.InvalidParameterNameException;
052: import org.deegree.datatypes.parameter.InvalidParameterValueException;
053: import org.deegree.datatypes.parameter.OperationParameterIm;
054: import org.deegree.datatypes.parameter.ParameterNotFoundException;
055: import org.deegree.framework.util.ImageUtils;
056:
057: /**
058: * This class encapsulates functionality for writing a <tt>GridCoverage</tt> as a GeoTIFF to a
059: * defined destination. Ths destination will be given as an <tt>OutputStream</tt>. The current
060: * implementation is limited to support <tt>ImageGridCoverage</tt>s to be written as GeoTIFF.
061: *
062: * @version $Revision: 9343 $
063: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
064: * @author last edited by: $Author: apoth $
065: *
066: * @version 1.0. $Revision: 9343 $, $Date: 2007-12-27 05:30:32 -0800 (Thu, 27 Dec 2007) $
067: *
068: * @since 2.0
069: */
070: public class ImageGridCoverageWriter extends AbstractGridCoverageWriter {
071:
072: /**
073: * @param destination
074: * @param metadata
075: * @param subNames
076: * @param currentSubname
077: * @param format
078: */
079: public ImageGridCoverageWriter(Object destination,
080: Map<String, Object> metadata, String[] subNames,
081: String currentSubname, Format format) {
082: super (destination, metadata, subNames, currentSubname, format);
083: }
084:
085: /**
086: * Writes the specified grid coverage.
087: *
088: * @param coverage
089: * The {@linkplain GridCoverage grid coverage} to write.
090: * @param parameters
091: * An optional set of parameters. Should be any or all of the parameters returned by
092: * {@link "org.opengis.coverage.grid.Format#getWriteParameters"}.
093: * @throws InvalidParameterNameException
094: * if a parameter in <code>parameters</code> doesn't have a recognized name.
095: * @throws InvalidParameterValueException
096: * if a parameter in <code>parameters</code> doesn't have a valid value.
097: * @throws ParameterNotFoundException
098: * if a parameter was required for the operation but was not provided in the
099: * <code>parameters</code> list.
100: * @throws FileFormatNotCompatibleWithGridCoverageException
101: * if the grid coverage can't be exported in the
102: * {@linkplain org.opengis.coverage.grid.GridCoverageWriter#getFormat writer format}.
103: * @throws IOException
104: * if the export failed for some other input/output reason, including
105: * {@link javax.imageio.IIOException} if an error was thrown by the underlying image
106: * library.
107: */
108: public void write(GridCoverage coverage,
109: GeneralParameterValueIm[] parameters)
110: throws InvalidParameterNameException,
111: InvalidParameterValueException, ParameterNotFoundException,
112: IOException {
113: int width = -1;
114: int height = -1;
115: for (int i = 0; i < parameters.length; i++) {
116: OperationParameterIm op = (OperationParameterIm) parameters[i]
117: .getDescriptor();
118: String name = op.getName();
119: if (name.equalsIgnoreCase("WIDTH")) {
120: Object o = op.getDefaultValue();
121: width = ((Integer) o).intValue();
122: } else if (name.equalsIgnoreCase("HEIGHT")) {
123: Object o = op.getDefaultValue();
124: height = ((Integer) o).intValue();
125: }
126: }
127:
128: OutputStream out = (OutputStream) destination;
129: ImageGridCoverage igc = (ImageGridCoverage) coverage;
130: ImageUtils.saveImage(igc.getAsImage(width, height), out, format
131: .getName(), 1f);
132:
133: }
134:
135: /**
136: * Allows any resources held by this object to be released. The result of calling any other
137: * method subsequent to a call to this method is undefined. It is important for applications to
138: * call this method when they know they will no longer be using this
139: * <code>GridCoverageWriter</code>. Otherwise, the writer may continue to hold on to
140: * resources indefinitely.
141: *
142: * @throws IOException
143: * if an error occured while disposing resources (for example while flushing data
144: * and closing a file).
145: */
146: public void dispose() throws IOException {
147: ((OutputStream) destination).close();
148: }
149:
150: }
|