001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/coverage/grid/CompoundGridCoverageReader.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.util.ArrayList;
040    import java.util.List;
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.ParameterNotFoundException;
046    import org.deegree.framework.log.ILogger;
047    import org.deegree.framework.log.LoggerFactory;
048    import org.deegree.model.spatialschema.Envelope;
049    import org.deegree.ogcwebservices.LonLatEnvelope;
050    import org.deegree.ogcwebservices.wcs.configuration.File;
051    import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering;
052    
053    /**
054     * This reader enables creation of <tt>GridCoverage</tt>s from more than one source. This will be
055     * used for example for tiled images.
056     *
057     *
058     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
059     * @author last edited by: $Author: mschneider $
060     *
061     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
062     */
063    
064    public class CompoundGridCoverageReader extends AbstractGridCoverageReader {
065    
066        private static final ILogger LOGGER = LoggerFactory.getLogger( CompoundGridCoverageReader.class );
067    
068        /**
069         * @param source
070         * @param description
071         * @param envelope
072         * @param format
073         */
074        public CompoundGridCoverageReader( File[] source, CoverageOffering description, Envelope envelope, Format format ) {
075            super( source, description, envelope, format );
076        }
077    
078        /**
079         * Read the grid coverage from the current stream position, and move to the next grid coverage.
080         *
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#getReadParameters"}.
084         * @return A new {@linkplain GridCoverage grid coverage} from the input source.
085         * @throws InvalidParameterNameException
086         *             if a parameter in <code>parameters</code> doesn't have a recognized name.
087         * @throws InvalidParameterValueException
088         *             if a parameter in <code>parameters</code> doesn't have a valid value.
089         * @throws ParameterNotFoundException
090         *             if a parameter was required for the operation but was not provided in the
091         *             <code>parameters</code> list.
092         * @throws IOException
093         *             if a read operation failed for some other input/output reason, including
094         *             {@link java.io.FileNotFoundException} if no file with the given <code>name</code>
095         *             can be found, or {@link javax.imageio.IIOException} if an error was thrown by the
096         *             underlying image library.
097         */
098        public GridCoverage read( GeneralParameterValueIm[] parameters )
099                                throws InvalidParameterNameException, InvalidParameterValueException,
100                                ParameterNotFoundException, IOException {
101    
102            File[] files = (File[]) source;
103            List<GridCoverage> list = new ArrayList<GridCoverage>( files.length );
104            for ( int i = 0; i < files.length; i++ ) {
105                GridCoverageReader gcr = createGridCoverageReader( files[i] );
106                if ( gcr != null ) {
107                    GridCoverage gc = gcr.read( parameters );
108                    if ( gc != null ) {
109                        list.add( gc );
110                    }
111                }
112            }
113            return createGridCoverage( list );
114        }
115    
116        /**
117         * creates a GridCoverage compound from the GridCoverages contained in the passed List. It is
118         * assumed that all GridCoverages in the List are of the same type and that the list contains at
119         * least one GridCoverage
120         *
121         * @param list
122         * @return a GridCoverage compound from the GridCoverages contained in the passed List.
123         */
124        private GridCoverage createGridCoverage( List<GridCoverage> list ) {
125    
126            GridCoverage gc = null;
127            if ( list != null && list.size() > 0 ) {
128                gc = list.get( 0 );
129                if ( gc instanceof ImageGridCoverage ) {
130                    ImageGridCoverage[] tmp = new ImageGridCoverage[list.size()];
131                    tmp = list.toArray( tmp );
132                    gc = new ImageGridCoverage( description, envelope, tmp );
133                } else if ( gc instanceof ByteGridCoverage ) {
134                    ByteGridCoverage[] tmp = new ByteGridCoverage[list.size()];
135                    tmp = list.toArray( tmp );
136                    gc = new ByteGridCoverage( description, envelope, tmp );
137                } else if ( gc instanceof ShortGridCoverage ) {
138                    ShortGridCoverage[] tmp = new ShortGridCoverage[list.size()];
139                    tmp = list.toArray( tmp );
140                    gc = new ShortGridCoverage( description, envelope, tmp );
141                } else if ( gc instanceof FloatGridCoverage ) {
142                    FloatGridCoverage[] tmp = new FloatGridCoverage[list.size()];
143                    tmp = list.toArray( tmp );
144                    gc = new FloatGridCoverage( description, envelope, tmp );
145                }
146            }
147    
148            return gc;
149        }
150    
151        /**
152         * creates a GridCoverageReader depending on the native format of the data source
153         *
154         * @param file
155         * @return a GridCoverageReader depending on the native format of the data source
156         * @throws IOException
157         */
158        private GridCoverageReader createGridCoverageReader( File file )
159                                throws IOException, InvalidParameterValueException {
160    
161            // calculate and set LonLatBoundingBox for the GC CoverageOffering
162            // as source of the returned GridCoverage
163            LonLatEnvelope lle = calcLonLatEnvelope( file.getEnvelope(), file.getCrs().getIdentifier() );
164            CoverageOffering desc = (CoverageOffering) description.clone();
165            desc.setLonLatEnvelope( lle );
166    
167            Envelope env = envelope.createIntersection( file.getEnvelope() );
168            GridCoverageReader gcr = null;
169            if ( env != null ) {
170                if ( format.getName().equalsIgnoreCase( "GEOTIFF" ) ) {
171                    gcr = new GeoTIFFGridCoverageReader( file, desc, env, format );
172                } else if ( isImageFormat( format ) ) {
173                    gcr = new ImageGridCoverageReader( file, desc, env, format );
174                } else {
175                    throw new IOException( "not supported file format: " + format.getName() );
176                }
177            } else {
178                LOGGER.logInfo( "no data available for BBOX: ", file.getEnvelope() );
179            }
180    
181            return gcr;
182        }
183    
184        /**
185         * returns true if the passed format is an image format
186         *
187         * @param format
188         * @return <code>true</code> if the passed format is an image format
189         */
190        private boolean isImageFormat( Format format ) {
191            String frmt = format.getName().toUpperCase();
192            return frmt.equalsIgnoreCase( "png" ) || frmt.equalsIgnoreCase( "bmp" ) || frmt.equalsIgnoreCase( "tif" )
193                   || frmt.equalsIgnoreCase( "tiff" ) || frmt.equalsIgnoreCase( "gif" ) || frmt.equalsIgnoreCase( "jpg" )
194                   || frmt.equalsIgnoreCase( "jpeg" ) || frmt.indexOf( "ECW" ) > -1;
195        }
196    
197        /**
198         * This method is an implementation dummy, it doensn't actually do anything.
199         */
200        public void dispose()
201                                throws IOException {
202            // throwin nottin
203        }
204    
205    }