001    // $HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/coverage/grid/AbstractGridCoverageReader.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.HashMap;
040    import java.util.Map;
041    
042    import org.deegree.model.crs.GeoTransformer;
043    import org.deegree.model.spatialschema.Envelope;
044    import org.deegree.ogcwebservices.LonLatEnvelope;
045    import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering;
046    
047    /**
048     * @version $Revision: 18195 $
049     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
050     * @author last edited by: $Author: mschneider $
051     *
052     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
053     */
054    
055    public abstract class AbstractGridCoverageReader implements GridCoverageReader {
056    
057        protected CoverageOffering description = null;
058    
059        protected Object source = null;
060    
061        protected Envelope envelope = null;
062    
063        private Map<String, String> metadata = new HashMap<String, String>();
064    
065        private String[] subNames = null;
066    
067        private String currentSubname = null;
068    
069        protected Format format = null;
070    
071        /**
072         * @param source
073         * @param description
074         * @param envelope
075         * @param format
076         */
077        public AbstractGridCoverageReader( Object source, CoverageOffering description, Envelope envelope, Format format ) {
078            this.description = description;
079            this.source = source;
080            this.envelope = envelope;
081            this.format = format;
082        }
083    
084        /**
085         * Returns the input source. This is the object passed to the
086         * {@link "org.opengis.coverage.grid.GridCoverageExchange#getReader(Object)"} method. It can be
087         * a {@link java.lang.String}, an {@link java.io.InputStream}, a
088         * {@link java.nio.channels.FileChannel}, whatever.
089         *
090         * @return the input source.
091         */
092        public Object getSource() {
093            return source;
094        }
095    
096        /**
097         * Returns the list of metadata keywords associated with the {@linkplain #getSource input
098         * source} as a whole (not associated with any particular grid coverage). If no metadata is
099         * available, the array will be empty.
100         *
101         * @return The list of metadata keywords for the input source.
102         * @throws IOException
103         *             if an error occurs during reading.
104         *
105         * @revisit This javadoc may not apply thats well in the iterator scheme.
106         */
107        public String[] getMetadataNames()
108                                throws IOException {
109            return metadata.keySet().toArray( new String[metadata.size()] );
110        }
111    
112        /**
113         * Retrieve the metadata value for a given metadata name.
114         *
115         * @param name
116         *            Metadata keyword for which to retrieve metadata.
117         * @return The metadata value for the given metadata name. Should be one of the name returned by
118         *         {@link #getMetadataNames}.
119         * @throws IOException
120         *             if an error occurs during reading.
121         * @throws MetadataNameNotFoundException
122         *             if there is no value for the specified metadata name.
123         *
124         * @revisit This javadoc may not apply thats well in the iterator scheme.
125         */
126        public String getMetadataValue( String name )
127                                throws IOException, MetadataNameNotFoundException {
128            return metadata.get( name );
129        }
130    
131        /**
132         * Sets the metadata value for a given metadata name.
133         *
134         * @param name
135         *            Metadata keyword for which to set the metadata.
136         * @param value
137         *            The metadata value for the given metadata name.
138         * @throws IOException
139         *             if an error occurs during writing.
140         * @throws MetadataNameNotFoundException
141         *             if the specified metadata name is not handled for this format.
142         *
143         * @revisit This javadoc may not apply thats well in the iterator scheme.
144         */
145        public void setMetadataValue( String name, String value )
146                                throws IOException, MetadataNameNotFoundException {
147            metadata.put( name, value );
148        }
149    
150        /**
151         * Set the name for the next grid coverage to GridCoverageWriter#write within the{@linkplain #getSource() input}.
152         * The subname can been fetch later at reading time.
153         *
154         * @param name
155         *
156         * @throws IOException
157         *             if an error occurs during writing.
158         * @revisit Do we need a special method for that, or should it be a metadata?
159         *
160         */
161        public void setCurrentSubname( String name )
162                                throws IOException {
163            currentSubname = name;
164        }
165    
166        /**
167         * Retrieve the list of grid coverages contained within the {@linkplain #getSource input
168         * source}. Each grid can have a different coordinate system, number of dimensions and grid
169         * geometry. For example, a HDF-EOS file (GRID.HDF) contains 6 grid coverages each having a
170         * different projection. An empty array will be returned if no sub names exist.
171         *
172         * @return The list of grid coverages contained within the input source.
173         * @throws IOException
174         *             if an error occurs during reading.
175         *
176         * @revisit The javadoc should also be more explicit about hierarchical format. Should the names
177         *          be returned as paths? Explain what to return if the GridCoverage are accessible by
178         *          index only. A proposal is to name them "grid1", "grid2", etc.
179         */
180        public String[] listSubNames()
181                                throws IOException {
182            return subNames;
183        }
184    
185        /**
186         * Returns the name for the next grid coverage to be read from the
187         * {@linkplain #getSource input source}.
188         *
189         * @return the name for the next grid coverage to be read from the input source.
190         *
191         * @throws IOException
192         *             if an error occurs during reading.
193         * @revisit Do we need a special method for that, or should it be a metadata?
194         *
195         */
196        public String getCurrentSubname()
197                                throws IOException {
198            return currentSubname;
199        }
200    
201        /**
202         * Returns the format handled by this <code>GridCoverageReader</code>.
203         *
204         * @return the format handled by this <code>GridCoverageReader</code>.
205         *
206         */
207        public Format getFormat() {
208            return format;
209        }
210    
211        /**
212         * transforms the passed <tt>Envelope</tt> to a <tt>LonLatEnvelope</tt> If the passed source
213         * CRS isn't equal to "EPSG:4326" the <tt>Envelope</tt> will be transformed to "EPSG:4326"
214         * first.
215         *
216         * @param env
217         * @param sourceCRS
218         * @return LatLonEnvelope in "EPSG:4326"
219         */
220        protected LonLatEnvelope calcLonLatEnvelope( Envelope env, String sourceCRS ) {
221            LonLatEnvelope lle = null;
222            if ( sourceCRS.equalsIgnoreCase( "EPSG:4326" ) ) {
223                lle = new LonLatEnvelope( env );
224            } else {
225                try {
226                    GeoTransformer tr = new GeoTransformer( "EPSG:4326" );
227                    env = tr.transform( env, sourceCRS );
228                } catch ( Exception e ) {
229                    e.printStackTrace();
230                }
231                lle = new LonLatEnvelope( env );
232            }
233            return lle;
234        }
235    
236    }