001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/graphics/LazyRasterLayer.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     53177 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    package org.deegree.graphics;
044    
045    import java.io.IOException;
046    import java.util.ArrayList;
047    import java.util.List;
048    
049    import org.deegree.framework.log.ILogger;
050    import org.deegree.framework.log.LoggerFactory;
051    import org.deegree.io.JDBCConnection;
052    import org.deegree.io.oraclegeoraster.GeoRasterDescription;
053    import org.deegree.model.coverage.grid.Format;
054    import org.deegree.model.coverage.grid.GridCoverage;
055    import org.deegree.model.coverage.grid.GridCoverageExchange;
056    import org.deegree.model.coverage.grid.GridCoverageReader;
057    import org.deegree.model.crs.CRSFactory;
058    import org.deegree.model.crs.CoordinateSystem;
059    import org.deegree.model.crs.UnknownCRSException;
060    import org.deegree.model.spatialschema.Envelope;
061    import org.deegree.model.spatialschema.GeometryFactory;
062    import org.deegree.ogcwebservices.InvalidParameterValueException;
063    import org.deegree.ogcwebservices.wcs.configuration.Directory;
064    import org.deegree.ogcwebservices.wcs.configuration.DirectoryResolution;
065    import org.deegree.ogcwebservices.wcs.configuration.Extension;
066    import org.deegree.ogcwebservices.wcs.configuration.File;
067    import org.deegree.ogcwebservices.wcs.configuration.FileResolution;
068    import org.deegree.ogcwebservices.wcs.configuration.OracleGeoRasterResolution;
069    import org.deegree.ogcwebservices.wcs.configuration.Resolution;
070    import org.deegree.ogcwebservices.wcs.configuration.Shape;
071    import org.deegree.ogcwebservices.wcs.configuration.ShapeResolution;
072    import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering;
073    
074    /**
075     * 
076     * 
077     * 
078     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
079     * @author last edited by: $Author: apoth $
080     * 
081     * @version $Revision: 9340 $, $Date: 2007-12-27 13:32:12 +0100 (Do, 27 Dez 2007) $
082     */
083    public class LazyRasterLayer extends AbstractLayer {
084    
085        private ILogger LOG = LoggerFactory.getLogger( LazyRasterLayer.class );
086    
087        private Extension resource;
088    
089        private CoverageOffering coverageOffering;
090    
091        /**
092         * 
093         * @param name
094         * @param coverageOffering
095         * @throws Exception
096         */
097        public LazyRasterLayer( String name, CoverageOffering coverageOffering ) throws Exception {
098            super( name );
099            this.coverageOffering = coverageOffering;
100            resource = coverageOffering.getExtension();
101        }
102    
103        /**
104         * 
105         * @param name
106         * @param crs
107         * @param coverageOffering
108         * @throws Exception
109         */
110        public LazyRasterLayer( String name, CoordinateSystem crs, CoverageOffering coverageOffering ) throws Exception {
111            super( name, crs );
112            this.coverageOffering = coverageOffering;
113            resource = coverageOffering.getExtension();
114        }
115    
116        /**
117         * @param crs
118         * @throws Exception
119         * 
120         */
121        public void setCoordinatesSystem( CoordinateSystem crs )
122                                throws Exception {
123            // not supported yet
124        }
125    
126        /*
127         * (non-Javadoc)
128         * 
129         * @see org.deegree.graphics.AbstractLayer#getBoundingBox()
130         */
131        @Override
132        public Envelope getBoundingBox() {
133            return coverageOffering.getDomainSet().getSpatialDomain().getEnvelops()[0];
134        }
135    
136        /**
137         * 
138         * @param envelope
139         * @param resolution
140         * @return
141         * @throws IOException
142         * @throws InvalidParameterValueException
143         */
144        public GridCoverage getRaster( Envelope envelope, double resolution )
145                                throws InvalidParameterValueException, IOException {
146    
147            Resolution[] resolutions = resource.getResolutions( resolution );
148    
149            String nativeCRS = coverageOffering.getSupportedCRSs().getNativeSRSs()[0].getCodes()[0];
150            CoordinateSystem crs;
151            try {
152                crs = CRSFactory.create( nativeCRS );
153            } catch ( UnknownCRSException e ) {
154                throw new InvalidParameterValueException( e );
155            }
156            envelope = GeometryFactory.createEnvelope( envelope.getMin(), envelope.getMax(), crs );
157    
158            GridCoverageReader reader = null;
159            if ( resolutions[0] instanceof FileResolution ) {
160                reader = getFileReader( resolutions, envelope );
161            } else if ( resolutions[0] instanceof ShapeResolution ) {
162                reader = getShapeReader( resolutions, envelope );
163            } else if ( resolutions[0] instanceof DirectoryResolution ) {
164                reader = getDirectoryReader( resolutions, envelope );
165            } else if ( resolutions[0] instanceof OracleGeoRasterResolution ) {
166                reader = getOracleGeoRasterReader( resolutions, envelope );
167            } else {
168                throw new InvalidParameterValueException( "not supported coverage resolution: "
169                                                          + resolutions[0].getClass().getName() );
170            }
171            return reader.read( null );
172        }
173    
174        /**
175         * 
176         * @param resolutions
177         * @param env
178         * @return
179         * @throws IOException
180         * @throws InvalidParameterValueException
181         */
182        private GridCoverageReader getDirectoryReader( Resolution[] resolutions, Envelope env )
183                                throws IOException, InvalidParameterValueException {
184    
185            LOG.logInfo( "reading coverage from directories" );
186    
187            Directory[] dirs = ( (DirectoryResolution) resolutions[0] ).getDirectories( env );
188    
189            GridCoverageExchange gce = new GridCoverageExchange( null );
190            Format format = new Format( coverageOffering.getSupportedFormats().getNativeFormat() );
191    
192            return gce.getReader( dirs, coverageOffering, env, format );
193        }
194    
195        /**
196         * 
197         * @param resolutions
198         * @param env
199         * @return
200         * @throws IOException
201         * @throws InvalidParameterValueException
202         */
203        private GridCoverageReader getFileReader( Resolution[] resolutions, Envelope env )
204                                throws IOException, InvalidParameterValueException {
205    
206            LOG.logInfo( "reading coverage from files" );
207    
208            File[] files = ( (FileResolution) resolutions[0] ).getFiles();
209            List list = new ArrayList();
210            for ( int i = 0; i < files.length; i++ ) {
211                Envelope fileEnv = files[i].getEnvelope();
212                if ( fileEnv.intersects( env ) ) {
213                    list.add( files[i] );
214                }
215            }
216            files = (File[]) list.toArray( new File[list.size()] );
217    
218            GridCoverageExchange gce = new GridCoverageExchange( null );
219            Format format = new Format( coverageOffering.getSupportedFormats().getNativeFormat() );
220    
221            return gce.getReader( files, coverageOffering, env, format );
222        }
223    
224        /**
225         * 
226         * @param resolutions
227         * @param env
228         * @return
229         * @throws InvalidParameterValueException
230         * @throws IOException
231         */
232        private GridCoverageReader getOracleGeoRasterReader( Resolution[] resolutions, Envelope env )
233                                throws InvalidParameterValueException, IOException {
234    
235            LOG.logInfo( "reading coverage from oracle georaster" );
236    
237            JDBCConnection jdbc = ( (OracleGeoRasterResolution) resolutions[0] ).getJDBCConnection();
238            String table = ( (OracleGeoRasterResolution) resolutions[0] ).getTable();
239            String rdtTable = ( (OracleGeoRasterResolution) resolutions[0] ).getRdtTable();
240            String column = ( (OracleGeoRasterResolution) resolutions[0] ).getColumn();
241            String identification = ( (OracleGeoRasterResolution) resolutions[0] ).getIdentification();
242            int level = ( (OracleGeoRasterResolution) resolutions[0] ).getLevel();
243            GeoRasterDescription grd = new GeoRasterDescription( jdbc, table, rdtTable, column, identification, level );
244    
245            GridCoverageExchange gce = new GridCoverageExchange( null );
246            Format format = new Format( coverageOffering.getSupportedFormats().getNativeFormat() );
247    
248            return gce.getReader( grd, coverageOffering, env, format );
249    
250        }
251    
252        /**
253         * 
254         * @param resolutions
255         * @param env
256         * @return
257         * @throws IOException
258         * @throws InvalidParameterValueException
259         */
260        private GridCoverageReader getShapeReader( Resolution[] resolutions, Envelope env )
261                                throws IOException, InvalidParameterValueException {
262    
263            LOG.logInfo( "reading coverage from shapes" );
264    
265            Shape shape = ( (ShapeResolution) resolutions[0] ).getShape();
266    
267            GridCoverageExchange gce = new GridCoverageExchange( null );
268            Format format = new Format( coverageOffering.getSupportedFormats().getNativeFormat() );
269            return gce.getReader( shape, coverageOffering, env, format );
270    
271        }
272    
273    }