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