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