001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/model/coverage/grid/ImageGridCoverageWriter.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.model.coverage.grid;
037    
038    import java.io.IOException;
039    import java.io.OutputStream;
040    import java.util.Map;
041    
042    import org.deegree.datatypes.parameter.GeneralParameterValueIm;
043    import org.deegree.datatypes.parameter.InvalidParameterNameException;
044    import org.deegree.datatypes.parameter.InvalidParameterValueException;
045    import org.deegree.datatypes.parameter.OperationParameterIm;
046    import org.deegree.datatypes.parameter.ParameterNotFoundException;
047    import org.deegree.framework.util.ImageUtils;
048    
049    /**
050     * This class encapsulates functionality for writing a <tt>GridCoverage</tt> as a GeoTIFF to a
051     * defined destination. Ths destination will be given as an <tt>OutputStream</tt>. The current
052     * implementation is limited to support <tt>ImageGridCoverage</tt>s to be written as GeoTIFF.
053     *
054     * @version $Revision: 18195 $
055     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
056     * @author last edited by: $Author: mschneider $
057     *
058     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
059     *
060     * @since 2.0
061     */
062    public class ImageGridCoverageWriter extends AbstractGridCoverageWriter {
063    
064        /**
065         * @param destination
066         * @param metadata
067         * @param subNames
068         * @param currentSubname
069         * @param format
070         */
071        public ImageGridCoverageWriter( Object destination, Map<String, Object> metadata, String[] subNames,
072                                        String currentSubname, Format format ) {
073            super( destination, metadata, subNames, currentSubname, format );
074        }
075    
076        /**
077         * Writes the specified grid coverage.
078         *
079         * @param coverage
080         *            The {@linkplain GridCoverage grid coverage} to write.
081         * @param parameters
082         *            An optional set of parameters. Should be any or all of the parameters returned by
083         *            {@link "org.opengis.coverage.grid.Format#getWriteParameters"}.
084         * @throws InvalidParameterNameException
085         *             if a parameter in <code>parameters</code> doesn't have a recognized name.
086         * @throws InvalidParameterValueException
087         *             if a parameter in <code>parameters</code> doesn't have a valid value.
088         * @throws ParameterNotFoundException
089         *             if a parameter was required for the operation but was not provided in the
090         *             <code>parameters</code> list.
091         * @throws IOException
092         *             if the export failed for some other input/output reason, including
093         *             {@link javax.imageio.IIOException} if an error was thrown by the underlying image
094         *             library.
095         */
096        public void write( GridCoverage coverage, GeneralParameterValueIm[] parameters )
097                                throws InvalidParameterNameException, InvalidParameterValueException,
098                                ParameterNotFoundException, IOException {
099            int width = -1;
100            int height = -1;
101            for ( int i = 0; i < parameters.length; i++ ) {
102                OperationParameterIm op = (OperationParameterIm) parameters[i].getDescriptor();
103                String name = op.getName();
104                if ( name.equalsIgnoreCase( "WIDTH" ) ) {
105                    Object o = op.getDefaultValue();
106                    width = ( (Integer) o ).intValue();
107                } else if ( name.equalsIgnoreCase( "HEIGHT" ) ) {
108                    Object o = op.getDefaultValue();
109                    height = ( (Integer) o ).intValue();
110                }
111            }
112    
113            OutputStream out = (OutputStream) destination;
114            ImageGridCoverage igc = (ImageGridCoverage) coverage;
115            ImageUtils.saveImage( igc.getAsImage( width, height ), out, format.getName(), 1f );
116    
117        }
118    
119        /**
120         * Allows any resources held by this object to be released. The result of calling any other
121         * method subsequent to a call to this method is undefined. It is important for applications to
122         * call this method when they know they will no longer be using this
123         * <code>GridCoverageWriter</code>. Otherwise, the writer may continue to hold on to
124         * resources indefinitely.
125         *
126         * @throws IOException
127         *             if an error occured while disposing resources (for example while flushing data
128         *             and closing a file).
129         */
130        public void dispose()
131                                throws IOException {
132            ( (OutputStream) destination ).close();
133        }
134    
135    }