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