001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/model/coverage/grid/oracle/OracleGeoRasterGridCoverageReader.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.model.coverage.grid.oracle;
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.coverage.grid.AbstractGridCoverageReader;
066    import org.deegree.model.coverage.grid.Format;
067    import org.deegree.model.coverage.grid.GridCoverage;
068    import org.deegree.model.coverage.grid.ImageGridCoverage;
069    import org.deegree.model.crs.GeoTransformer;
070    import org.deegree.model.spatialschema.Envelope;
071    import org.deegree.model.spatialschema.GeometryFactory;
072    import org.deegree.ogcwebservices.LonLatEnvelope;
073    import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering;
074    
075    /**
076     * Reader for Coverages stored in Oracle 10g GeoRaster format
077     * 
078     * 
079     * @version $Revision: 9436 $
080     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
081     * @author last edited by: $Author: rbezema $
082     * 
083     * @version 1.0. $Revision: 9436 $, $Date: 2008-01-08 11:50:19 +0100 (Di, 08 Jan 2008) $
084     * 
085     * @since 2.0
086     */
087    public class OracleGeoRasterGridCoverageReader extends AbstractGridCoverageReader {
088    
089        private static final ILogger LOG = LoggerFactory.getLogger( OracleGeoRasterGridCoverageReader.class );
090    
091        /**
092         * 
093         * @param jdbc
094         *            description of the JDBC connection to a Orable DB
095         * @param table
096         *            name of the table storing the GeoRaster
097         * @param column
098         *            name of the column the target GeoRaster is stored
099         * @param identification
100         *            a SQL where clause that identifies the target georaster
101         * @param description
102         * @param envelope
103         * @param format
104         */
105        public OracleGeoRasterGridCoverageReader( GeoRasterDescription grDesc,
106                                                  CoverageOffering description, Envelope envelope,
107                                                  Format format ) {
108            super( grDesc, description, envelope, format );
109        }
110    
111        public void dispose()
112                                throws IOException {
113    
114        }
115    
116        /**
117         * reads a GridCoverage from a Oracle 10g GeoRaster
118         * 
119         * @param parameters -
120         */
121        public GridCoverage read( GeneralParameterValueIm[] parameters )
122                                throws InvalidParameterNameException, InvalidParameterValueException,
123                                ParameterNotFoundException, IOException {
124    
125            float width = -1;
126            float height = -1;
127            for ( int i = 0; i < parameters.length; i++ ) {
128                OperationParameterIm op = (OperationParameterIm) parameters[i].getDescriptor();
129                String name = op.getName();
130                if ( name.equalsIgnoreCase( "WIDTH" ) ) {
131                    Object o = op.getDefaultValue();
132                    width = ( (Integer) o ).intValue();
133                } else if ( name.equalsIgnoreCase( "HEIGHT" ) ) {
134                    Object o = op.getDefaultValue();
135                    height = ( (Integer) o ).intValue();
136                }
137            }
138    
139            // get the region of the georaster that intersects with the requested
140            // envelope. First field of the returned array contains the intersection
141            // envelope in the rasters native CRS; second field contains the
142            // corresponding LonLatEnvelope
143            Object[] o = getImageRegion();
144    
145            GeoRasterDescription grDesc = (GeoRasterDescription) getSource();
146            RenderedImage img = null;
147            try {
148                LOG.logDebug( "reading GeoRaster from Oracle DB" );
149                img = GeoRasterReader.exportRaster( grDesc, (Envelope) o[0] );
150            } catch ( Exception e ) {
151                LOG.logError( "could not read GeoRaster: ", e );
152                throw new IOException( "could not read GeoRaster: " + e.getMessage() );
153            }
154    
155            ParameterBlock pb = new ParameterBlock();
156            pb.addSource( img );
157            pb.add( width / img.getWidth() ); // The xScale
158            pb.add( height / img.getHeight() ); // The yScale
159            pb.add( 0.0F ); // The x translation
160            pb.add( 0.0F ); // The y translation
161            pb.add( new InterpolationNearest() ); // The interpolation
162            // Create the scale operation
163            RenderedOp ro = JAI.create( "scale", pb, null );
164            try {
165                img = ro.getAsBufferedImage();
166            } catch ( Exception e ) {
167                LOG.logError( "could not rescale image", e );
168                throw new IOException( "could not rescale image" + e.getMessage() );
169            }
170    
171            CoverageOffering co = (CoverageOffering) description.clone();
172            co.setLonLatEnvelope( (LonLatEnvelope) o[1] );
173    
174            return new ImageGridCoverage( co, (Envelope) o[0], (BufferedImage) img );
175    
176        }
177    
178        /**
179         * returns the region of the source image that intersects with the GridCoverage to be created as
180         * Rectange as well as the Envelope of the region in the native CRS and the LonLatEnvelope of
181         * this region.
182         * 
183         * @return the region of the source image that intersects with the GridCoverage to be created as
184         *         Rectange as well as the Envelope of the region in the native CRS and the
185         *         LonLatEnvelope of this region.
186         */
187        private Object[] getImageRegion() {
188    
189            CodeList[] cl = description.getSupportedCRSs().getNativeSRSs();
190            String code = cl[0].getCodes()[0];
191    
192            LonLatEnvelope lle = description.getLonLatEnvelope();
193            Envelope tmp = GeometryFactory.createEnvelope( lle.getMin().getX(), lle.getMin().getY(),
194                                                           lle.getMax().getX(), lle.getMax().getY(),
195                                                           null );
196            try {
197                if ( !code.equals( "EPSG:4326" ) ) {
198                    GeoTransformer trans = new GeoTransformer( code );
199                    tmp = trans.transform( tmp, "EPSG:4326" );
200                }
201            } catch ( Exception e ) {
202                LOG.logError( StringTools.stackTraceToString( e ) );
203            }
204    
205            // calculate envelope of the part of the grid coverage that intersects
206            // within the image
207            Envelope env = envelope.createIntersection( tmp );
208            LonLatEnvelope lonLatEnvelope = calcLonLatEnvelope( env, code );
209    
210            return new Object[] { env, lonLatEnvelope };
211        }
212    
213    }