001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/model/coverage/grid/OracleGeoRasterGridCoverageReader.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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.model.coverage.grid;
044    
045    import java.awt.image.BufferedImage;
046    import java.awt.image.RenderedImage;
047    import java.awt.image.renderable.ParameterBlock;
048    import java.io.IOException;
049    
050    import javax.media.jai.InterpolationNearest;
051    import javax.media.jai.JAI;
052    import javax.media.jai.RenderedOp;
053    
054    import org.deegree.datatypes.CodeList;
055    import org.deegree.datatypes.parameter.GeneralParameterValueIm;
056    import org.deegree.datatypes.parameter.InvalidParameterNameException;
057    import org.deegree.datatypes.parameter.InvalidParameterValueException;
058    import org.deegree.datatypes.parameter.OperationParameterIm;
059    import org.deegree.datatypes.parameter.ParameterNotFoundException;
060    import org.deegree.framework.log.ILogger;
061    import org.deegree.framework.log.LoggerFactory;
062    import org.deegree.framework.util.StringTools;
063    import org.deegree.io.oraclegeoraster.GeoRasterDescription;
064    import org.deegree.io.oraclegeoraster.GeoRasterReader;
065    import org.deegree.model.crs.GeoTransformer;
066    import org.deegree.model.crs.IGeoTransformer;
067    import org.deegree.model.spatialschema.Envelope;
068    import org.deegree.model.spatialschema.GeometryFactory;
069    import org.deegree.ogcwebservices.LonLatEnvelope;
070    import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering;
071    
072    /**
073     * Reader for Coverages stored in Oracle 10g GeoRaster format
074     * 
075     * 
076     * @version $Revision: 6259 $
077     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
078     * @author last edited by: $Author: bezema $
079     * 
080     * @version 1.0. $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
081     * 
082     * @since 2.0
083     */
084    public class OracleGeoRasterGridCoverageReader extends AbstractGridCoverageReader {
085    
086        private static final ILogger LOG = LoggerFactory.getLogger( OracleGeoRasterGridCoverageReader.class );
087    
088        /**
089         * 
090         * @param jdbc
091         *            description of the JDBC connection to a Orable DB
092         * @param table
093         *            name of the table storing the GeoRaster
094         * @param column
095         *            name of the column the target GeoRaster is stored
096         * @param identification
097         *            a SQL where clause that identifies the target georaster
098         * @param description
099         * @param envelope
100         * @param format
101         */
102        public OracleGeoRasterGridCoverageReader( GeoRasterDescription grDesc,
103                                                  CoverageOffering description, Envelope envelope,
104                                                  Format format ) {
105            super( grDesc, description, envelope, format );
106        }
107    
108        public void dispose()
109                                throws IOException {
110    
111        }
112    
113        /**
114         * reads a GridCoverage from a Oracle 10g GeoRaster
115         * 
116         * @param parameters -
117         */
118        public GridCoverage read( GeneralParameterValueIm[] parameters )
119                                throws InvalidParameterNameException, InvalidParameterValueException,
120                                ParameterNotFoundException, IOException {
121    
122            float width = -1;
123            float height = -1;
124            for ( int i = 0; i < parameters.length; i++ ) {
125                OperationParameterIm op = (OperationParameterIm) parameters[i].getDescriptor();
126                String name = op.getName();
127                if ( name.equalsIgnoreCase( "WIDTH" ) ) {
128                    Object o = op.getDefaultValue();
129                    width = ( (Integer) o ).intValue();
130                } else if ( name.equalsIgnoreCase( "HEIGHT" ) ) {
131                    Object o = op.getDefaultValue();
132                    height = ( (Integer) o ).intValue();
133                }
134            }
135    
136            // get the region of the georaster that intersects with the requested
137            // envelope. First field of the returned array contains the intersection
138            // envelope in the rasters native CRS; second field contains the
139            // corresponding LonLatEnvelope
140            Object[] o = getImageRegion();
141    
142            GeoRasterDescription grDesc = (GeoRasterDescription) getSource();
143            RenderedImage img = null;
144            try {
145                LOG.logDebug( "reading GeoRaster from Oracle DB" );
146                img = GeoRasterReader.exportRaster( grDesc, (Envelope) o[0] );
147            } catch ( Exception e ) {
148                LOG.logError( "could not read GeoRaster: ", e );
149                throw new IOException( "could not read GeoRaster: " + e.getMessage() );
150            }
151    
152            ParameterBlock pb = new ParameterBlock();
153            pb.addSource( img );
154            pb.add( width / img.getWidth() ); // The xScale
155            pb.add( height / img.getHeight() ); // The yScale
156            pb.add( 0.0F ); // The x translation
157            pb.add( 0.0F ); // The y translation
158            pb.add( new InterpolationNearest() ); // The interpolation
159            // Create the scale operation
160            RenderedOp ro = JAI.create( "scale", pb, null );
161            try {
162                img = ro.getAsBufferedImage();
163            } catch ( Exception e ) {
164                LOG.logError( "could not rescale image", e );
165                throw new IOException( "could not rescale image" + e.getMessage() );
166            }
167    
168            CoverageOffering co = (CoverageOffering) description.clone();
169            co.setLonLatEnvelope( (LonLatEnvelope) o[1] );
170    
171            return new ImageGridCoverage( co, (Envelope) o[0], (BufferedImage) img );
172    
173        }
174    
175        /**
176         * returns the region of the source image that intersects with the GridCoverage to be created as
177         * Rectange as well as the Envelope of the region in the native CRS and the LonLatEnvelope of
178         * this region.
179         * 
180         * @return the region of the source image that intersects with the GridCoverage to be created as
181         *         Rectange as well as the Envelope of the region in the native CRS and the
182         *         LonLatEnvelope of this region.
183         */
184        private Object[] getImageRegion() {
185    
186            CodeList[] cl = description.getSupportedCRSs().getNativeSRSs();
187            String code = cl[0].getCodes()[0];
188    
189            LonLatEnvelope lle = description.getLonLatEnvelope();
190            Envelope tmp = GeometryFactory.createEnvelope( lle.getMin().getX(), lle.getMin().getY(),
191                                                           lle.getMax().getX(), lle.getMax().getY(),
192                                                           null );
193            try {
194                if ( !code.equals( "EPSG:4326" ) ) {
195                    IGeoTransformer trans = new GeoTransformer( code );
196                    tmp = trans.transform( tmp, "EPSG:4326" );
197                }
198            } catch ( Exception e ) {
199                LOG.logError( StringTools.stackTraceToString( e ) );
200            }
201    
202            // calculate envelope of the part of the grid coverage that intersects
203            // within the image
204            Envelope env = envelope.createIntersection( tmp );
205            LonLatEnvelope lonLatEnvelope = calcLonLatEnvelope( env, code );
206    
207            return new Object[] { env, lonLatEnvelope };
208        }
209    
210    }
211    
212    /***************************************************************************************************
213     * <code>
214     Changes to this class. What the people have been up to:
215    
216     $Log$
217     Revision 1.18  2007/02/09 17:27:59  poth
218     *** empty log message ***
219    
220     Revision 1.17  2007/01/26 14:55:11  wanhoff
221     fixed Javadoc @return tag and footer
222    
223     Revision 1.16  2006/11/27 09:07:52  poth
224     JNI integration of proj4 has been removed. The CRS functionality now will be done by native deegree code.
225    
226     Revision 1.15  2006/11/17 08:58:01  poth
227     bug fixes - setting correct interpolation
228    
229     Revision 1.14  2006/11/08 17:13:46  poth
230     *** empty log message ***
231    
232     Revision 1.13  2006/09/27 16:46:41  poth
233     transformation method signature changed
234    
235     Revision 1.12  2006/08/29 19:54:14  poth
236     footer corrected
237    
238     Revision 1.11  2006/06/30 14:16:18  poth
239     comment corrected
240    
241     Revision 1.10  2006/05/03 20:09:52  poth
242     *** empty log message ***
243    
244     Revision 1.9  2006/05/01 20:15:27  poth
245     *** empty log message ***
246    
247     </code>
248     **************************************************************************************************/