001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/model/coverage/grid/FloatGridCoverage.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     Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: klaus.greve@uni-bonn.de
041    
042     
043     ---------------------------------------------------------------------------*/
044    package org.deegree.model.coverage.grid;
045    
046    import java.awt.Graphics;
047    import java.awt.Rectangle;
048    import java.awt.image.BufferedImage;
049    import java.awt.image.DataBuffer;
050    import java.awt.image.Raster;
051    import java.awt.image.renderable.RenderableImage;
052    
053    import org.deegree.model.spatialschema.Envelope;
054    import org.deegree.model.spatialschema.GeometryFactory;
055    import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering;
056    import org.opengis.pt.PT_Envelope;
057    
058    /**
059     * 
060     * 
061     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
062     * @author last edited by: $Author: apoth $
063     * 
064     * @version $Revision: 9343 $, $Date: 2007-12-27 14:30:32 +0100 (Do, 27 Dez 2007) $
065     */
066    public class FloatGridCoverage extends AbstractGridCoverage {
067    
068        private static final long serialVersionUID = -3642652899429594623L;
069    
070        private float[][][] data = null;
071    
072        /**
073         * @param coverageOffering
074         * @param envelope
075         * @param data
076         */
077        public FloatGridCoverage( CoverageOffering coverageOffering, Envelope envelope, float[][][] data ) {
078            this( coverageOffering, envelope, false, data );
079        }
080    
081        /**
082         * @param coverageOffering
083         * @param envelope
084         * @param isEditable
085         * @param data
086         */
087        public FloatGridCoverage( CoverageOffering coverageOffering, Envelope envelope, boolean isEditable, float[][][] data ) {
088            super( coverageOffering, envelope, isEditable );
089            this.data = data;
090        }
091    
092        /**
093         * @param coverageOffering
094         * @param envelope
095         * @param sources
096         */
097        public FloatGridCoverage( CoverageOffering coverageOffering, Envelope envelope, FloatGridCoverage[] sources ) {
098            super( coverageOffering, envelope, sources );
099        }
100    
101        /**
102         * The number of sample dimensions in the coverage. For grid coverages, a sample dimension is a
103         * band.
104         * 
105         * @return The number of sample dimensions in the coverage.
106         * @UML mandatory numSampleDimensions
107         */
108        public int getNumSampleDimensions() {
109            if ( data != null ) {
110                return data.length;
111            }
112            return sources[0].getNumSampleDimensions();
113        }
114    
115        /**
116         * Returns 2D view of this coverage as a renderable image. This optional operation allows
117         * interoperability with <A HREF="http://java.sun.com/products/java-media/2D/">Java2D</A>. If
118         * this coverage is a {@link "org.opengis.coverage.grid.GridCoverage"} backed by a
119         * {@link java.awt.image.RenderedImage}, the underlying image can be obtained with:
120         * 
121         * <code>getRenderableImage(0,1).{@linkplain RenderableImage#createDefaultRendering()
122         * createDefaultRendering()}</code>
123         * 
124         * @param xAxis
125         *            Dimension to use for the <var>x</var> axis.
126         * @param yAxis
127         *            Dimension to use for the <var>y</var> axis.
128         * @return A 2D view of this coverage as a renderable image.
129         * @throws UnsupportedOperationException
130         *             if this optional operation is not supported.
131         * @throws IndexOutOfBoundsException
132         *             if <code>xAxis</code> or <code>yAxis</code> is out of bounds.
133         */
134        public RenderableImage getRenderableImage( int xAxis, int yAxis )
135                                throws UnsupportedOperationException, IndexOutOfBoundsException {
136            if ( data != null ) {
137    
138                return null;
139            }
140            // TODO if multi images -> sources.length > 0
141            return null;
142        }
143    
144        /**
145         * this is a deegree convenience method which returns the source image of an
146         * <tt>ImageGridCoverage</tt>. In procipal the same can be done with the
147         * getRenderableImage(int xAxis, int yAxis) method. but creating a <tt>RenderableImage</tt>
148         * image is very slow. I xAxis or yAxis <= 0 then the size of the returned image will be
149         * calculated from the source images of the coverage.
150         * 
151         * @param xAxis
152         *            Dimension to use for the <var>x</var> axis.
153         * @param yAxis
154         *            Dimension to use for the <var>y</var> axis.
155         * @return
156         */
157        public BufferedImage getAsImage( int xAxis, int yAxis ) {
158    
159            if ( xAxis <= 0 || yAxis <= 0 ) {
160                // get default size if passed target size is <= 0
161                Rectangle rect = calculateOriginalSize();
162                xAxis = rect.width;
163                yAxis = rect.height;
164            }
165    
166            BufferedImage bi = new BufferedImage( xAxis, yAxis, BufferedImage.TYPE_INT_ARGB );
167    
168            if ( data != null ) {
169                BufferedImage img = new BufferedImage( data[0][0].length, data[0].length, BufferedImage.TYPE_INT_ARGB );
170                Raster raster = img.getData();
171                DataBuffer buf = raster.getDataBuffer();
172                for ( int i = 0; i < data[0][0].length; i++ ) {
173                    for ( int j = 0; j < data[0].length; j++ ) {
174                        buf.setElem( j * data[0][0].length + i, Float.floatToIntBits( data[0][j][i] ) );
175                    }
176                }
177                img.setData( raster );
178                if ( img.getWidth() != bi.getWidth() || img.getHeight() != bi.getHeight() ) {
179                    // just resize if source image size is different from
180                    // target image size
181                    Graphics g = bi.getGraphics();
182                    g.drawImage( img, 0, 0, bi.getWidth(), bi.getHeight(), null );
183                    g.dispose();
184                }
185            } else {
186                // it's a complex FloatGridCoverage made up of different
187                // source coverages
188                for ( int i = 0; i < sources.length; i++ ) {
189                    PT_Envelope env = sources[i].getEnvelope();
190                    Envelope sourceEnv = GeometryFactory.createEnvelope( env.minCP.ord[0], env.minCP.ord[1],
191                                                                         env.maxCP.ord[0], env.maxCP.ord[1], null );
192                    BufferedImage sourceImg = ( (AbstractGridCoverage) sources[i] ).getAsImage( -1, -1 );
193                    env = this.getEnvelope();
194                    Envelope targetEnv = GeometryFactory.createEnvelope( env.minCP.ord[0], env.minCP.ord[1],
195                                                                         env.maxCP.ord[0], env.maxCP.ord[1], null );
196    
197                    bi = paintImage( bi, targetEnv, sourceImg, sourceEnv );
198                }
199            }
200    
201            return bi;
202        }
203    
204        /**
205         * calculates the original size of a gridcoverage based on its resolution and the envelope(s) of
206         * its source(s).
207         * 
208         * @return
209         */
210        private Rectangle calculateOriginalSize() {
211            if ( data != null ) {
212                return new Rectangle( data[0][0].length, data[0].length );
213            }
214            BufferedImage bi = ( (ImageGridCoverage) sources[0] ).getAsImage( -1, -1 );
215            PT_Envelope env = sources[0].getEnvelope();
216            double dx = ( env.maxCP.ord[0] - env.minCP.ord[0] ) / bi.getWidth();
217            double dy = ( env.maxCP.ord[1] - env.minCP.ord[1] ) / bi.getHeight();
218            env = this.getEnvelope();
219            int w = (int) Math.round( ( env.maxCP.ord[0] - env.minCP.ord[0] ) / dx );
220            int h = (int) Math.round( ( env.maxCP.ord[1] - env.minCP.ord[1] ) / dy );
221            return new Rectangle( w, h );
222        }
223    }