001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/model/coverage/grid/GeoTIFFGridCoverageWriter.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.awt.image.BufferedImage;
039    import java.io.IOException;
040    import java.io.OutputStream;
041    import java.util.Map;
042    
043    import org.deegree.datatypes.parameter.GeneralParameterValueIm;
044    import org.deegree.datatypes.parameter.InvalidParameterNameException;
045    import org.deegree.datatypes.parameter.InvalidParameterValueException;
046    import org.deegree.datatypes.parameter.OperationParameterIm;
047    import org.deegree.datatypes.parameter.ParameterNotFoundException;
048    import org.deegree.framework.log.ILogger;
049    import org.deegree.framework.log.LoggerFactory;
050    import org.deegree.i18n.Messages;
051    import org.deegree.io.geotiff.GeoTiffWriter;
052    import org.deegree.model.crs.CRSFactory;
053    import org.deegree.model.crs.CoordinateSystem;
054    import org.deegree.model.crs.UnknownCRSException;
055    import org.deegree.model.spatialschema.Envelope;
056    import org.deegree.model.spatialschema.GeometryFactory;
057    
058    /**
059     * This class encapsulates functionality for writing a <tt>GridCoverage</tt> as a GeoTIFF to a defined destination. Ths
060     * destination will be given as an <tt>OutputStream</tt>. The current implementation is limited to support
061     * <tt>ImageGridCoverage</tt>s to be written as GeoTIFF.
062     *
063     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
064     * @author last edited by: $Author: mschneider $
065     *
066     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
067     */
068    public class GeoTIFFGridCoverageWriter extends AbstractGridCoverageWriter {
069    
070        private static final ILogger LOG = LoggerFactory.getLogger( GeoTIFFGridCoverageWriter.class );
071    
072        /**
073         * @param destination
074         * @param metadata
075         * @param subNames
076         * @param currentSubname
077         * @param format
078         */
079        public GeoTIFFGridCoverageWriter( 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. The GridCoverage will be written in its original size (width/height).
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 <code>parameters</code>
098         *             list.
099         * @throws IOException
100         *             if the export failed for some other input/output reason, including {@link javax.imageio.IIOException}
101         *             if an error was thrown by the underlying image library.
102         */
103        public void write( GridCoverage coverage, GeneralParameterValueIm[] parameters )
104                                throws InvalidParameterNameException, InvalidParameterValueException,
105                                ParameterNotFoundException, IOException {
106    
107            int width = -1;
108            int height = -1;
109            String response_crs = null;
110            for ( int i = 0; i < parameters.length; i++ ) {
111                OperationParameterIm op = (OperationParameterIm) parameters[i].getDescriptor();
112                String name = op.getName();
113                if ( name.equalsIgnoreCase( "WIDTH" ) ) {
114                    Object o = op.getDefaultValue();
115                    width = ( (Integer) o ).intValue();
116                } else if ( name.equalsIgnoreCase( "HEIGHT" ) ) {
117                    Object o = op.getDefaultValue();
118                    height = ( (Integer) o ).intValue();
119                } else if ( name.equalsIgnoreCase( "response_crs" ) ) {
120                    Object o = op.getDefaultValue();
121                    response_crs = (String) o;
122                }
123            }
124    
125            OutputStream out = (OutputStream) destination;
126            AbstractGridCoverage igc = (AbstractGridCoverage) coverage;
127            BufferedImage bi = igc.getAsImage( width, height );
128            Envelope ptenv = igc.getEnvelope();
129            double xmin = ptenv.getMin().getX();
130            double ymin = ptenv.getMin().getY();
131            double xmax = ptenv.getMax().getX();
132            double ymax = ptenv.getMax().getY();
133            CoordinateSystem crs = null;
134            if ( response_crs != null ) {
135                try {
136                    crs = CRSFactory.create( response_crs );
137                } catch ( UnknownCRSException e ) {
138                    // only needed for GeoTIFF CRS information
139                }
140            }
141    
142            Envelope envelope = GeometryFactory.createEnvelope( xmin, ymin, xmax, ymax, null );
143    
144            double xRes = envelope.getWidth() / bi.getWidth();
145            double yRes = envelope.getHeight() / bi.getHeight();
146    
147            try {
148                double offset = 0;
149                double scaleFactor = 1;
150                if ( metadata.get( "offset" ) != null ) {
151                    offset = (Double) metadata.get( "offset" );
152                }
153                if ( metadata.get( "scaleFactor" ) != null ) {
154                    scaleFactor = (Double) metadata.get( "scaleFactor" );
155                }
156                LOG.logDebug( "offset " + offset );
157                GeoTiffWriter gtw = new GeoTiffWriter( bi, envelope, xRes, yRes, crs, offset, scaleFactor );
158    
159                gtw.write( out );
160    
161            } catch ( Exception e ) {
162                LOG.logError( e.getMessage(), e );
163                throw new IOException( Messages.getMessage( "GC_ERROR_GEOTIFFWRITER" ) );
164            }
165        }
166    
167        /**
168         * Allows any resources held by this object to be released. The result of calling any other method subsequent to a
169         * call to this method is undefined. It is important for applications to call this method when they know they will
170         * no longer be using this <code>GridCoverageWriter</code>. Otherwise, the writer may continue to hold on to
171         * resources indefinitely.
172         *
173         * @throws IOException
174         *             if an error occured while disposing resources (for example while flushing data and closing a file).
175         */
176        public void dispose()
177                                throws IOException {
178            ( (OutputStream) destination ).close();
179        }
180    
181    }