001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/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 14:30:32 +0100 (Do, 27 Dez 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, Map<String, Object> metadata, String[] subNames,
080                                        String currentSubname, Format format ) {
081            super( destination, metadata, subNames, currentSubname, format );
082        }
083    
084        /**
085         * Writes the specified grid coverage.
086         * 
087         * @param coverage
088         *            The {@linkplain GridCoverage grid coverage} to write.
089         * @param parameters
090         *            An optional set of parameters. Should be any or all of the parameters returned by
091         *            {@link "org.opengis.coverage.grid.Format#getWriteParameters"}.
092         * @throws InvalidParameterNameException
093         *             if a parameter in <code>parameters</code> doesn't have a recognized name.
094         * @throws InvalidParameterValueException
095         *             if a parameter in <code>parameters</code> doesn't have a valid value.
096         * @throws ParameterNotFoundException
097         *             if a parameter was required for the operation but was not provided in the
098         *             <code>parameters</code> list.
099         * @throws FileFormatNotCompatibleWithGridCoverageException
100         *             if the grid coverage can't be exported in the
101         *             {@linkplain org.opengis.coverage.grid.GridCoverageWriter#getFormat writer format}.
102         * @throws IOException
103         *             if the export failed for some other input/output reason, including
104         *             {@link javax.imageio.IIOException} if an error was thrown by the underlying image
105         *             library.
106         */
107        public void write( GridCoverage coverage, GeneralParameterValueIm[] parameters )
108                                throws InvalidParameterNameException, InvalidParameterValueException,
109                                ParameterNotFoundException, IOException {
110            int width = -1;
111            int height = -1;
112            for ( int i = 0; i < parameters.length; i++ ) {
113                OperationParameterIm op = (OperationParameterIm) parameters[i].getDescriptor();
114                String name = op.getName();
115                if ( name.equalsIgnoreCase( "WIDTH" ) ) {
116                    Object o = op.getDefaultValue();
117                    width = ( (Integer) o ).intValue();
118                } else if ( name.equalsIgnoreCase( "HEIGHT" ) ) {
119                    Object o = op.getDefaultValue();
120                    height = ( (Integer) o ).intValue();
121                }
122            }
123    
124            OutputStream out = (OutputStream) destination;
125            ImageGridCoverage igc = (ImageGridCoverage) coverage;
126            ImageUtils.saveImage( igc.getAsImage( width, height ), out, format.getName(), 1f );
127    
128        }
129    
130        /**
131         * Allows any resources held by this object to be released. The result of calling any other
132         * method subsequent to a call to this method is undefined. It is important for applications to
133         * call this method when they know they will no longer be using this
134         * <code>GridCoverageWriter</code>. Otherwise, the writer may continue to hold on to
135         * resources indefinitely.
136         * 
137         * @throws IOException
138         *             if an error occured while disposing resources (for example while flushing data
139         *             and closing a file).
140         */
141        public void dispose()
142                                throws IOException {
143            ( (OutputStream) destination ).close();
144        }
145    
146    }