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