001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/model/coverage/grid/XYZGridCoverageWriter.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.model.coverage.grid;
037    
038    import java.awt.image.BufferedImage;
039    import java.io.IOException;
040    import java.io.OutputStream;
041    import java.io.PrintWriter;
042    import java.text.DecimalFormat;
043    import java.text.NumberFormat;
044    import java.util.Map;
045    
046    import org.deegree.datatypes.parameter.GeneralParameterValueIm;
047    import org.deegree.datatypes.parameter.InvalidParameterNameException;
048    import org.deegree.datatypes.parameter.InvalidParameterValueException;
049    import org.deegree.datatypes.parameter.OperationParameterIm;
050    import org.deegree.datatypes.parameter.ParameterNotFoundException;
051    import org.deegree.graphics.transformation.GeoTransform;
052    import org.deegree.graphics.transformation.WorldToScreenTransform;
053    import org.deegree.model.spatialschema.Envelope;
054    import org.deegree.processing.raster.converter.Image2RawData;
055    
056    /**
057     * Implementation of {@link "org.opengis.coverage.grid.GridCoverageWriter"} for writing a GridCoverage as XYZ coordinate
058     * tuples to a defined destioation
059     *
060     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
061     * @author last edited by: $Author: mschneider $
062     *
063     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
064     */
065    public class XYZGridCoverageWriter extends AbstractGridCoverageWriter {
066    
067        private static NumberFormat nf = new DecimalFormat( "##########.###" );
068    
069        /**
070         *
071         * @param destination
072         * @param metadata
073         * @param subNames
074         * @param currentSubname
075         * @param format
076         */
077        public XYZGridCoverageWriter( Object destination, Map<String, Object> metadata, String[] subNames,
078                                      String currentSubname, Format format ) {
079            super( destination, metadata, subNames, currentSubname, format );
080    
081        }
082    
083        /**
084         * disposes all resources assigned to a GMLGridCoverageWriter instance. For most cases this will be IO-resources
085         *
086         * @throws IOException
087         */
088        public void dispose()
089                                throws IOException {
090            // nothing happening here...
091        }
092    
093        /**
094         * @param coverage
095         * @param parameters
096         *            must contain the servlet URL within the first field; all other fields must contain the required
097         *            parameters for a valid GetCoverage request
098         * @throws InvalidParameterNameException
099         * @throws InvalidParameterValueException
100         * @throws ParameterNotFoundException
101         * @throws IOException
102         */
103        public void write( GridCoverage coverage, GeneralParameterValueIm[] parameters )
104                                throws InvalidParameterNameException, InvalidParameterValueException,
105                                ParameterNotFoundException, IOException {
106    
107            int width = -1;
108            int height = -1;
109            for ( int i = 0; i < parameters.length; i++ ) {
110                OperationParameterIm op = (OperationParameterIm) parameters[i].getDescriptor();
111                String name = op.getName();
112                if ( name.equalsIgnoreCase( "WIDTH" ) ) {
113                    Object o = op.getDefaultValue();
114                    width = ( (Integer) o ).intValue();
115                } else if ( name.equalsIgnoreCase( "HEIGHT" ) ) {
116                    Object o = op.getDefaultValue();
117                    height = ( (Integer) o ).intValue();
118                }
119            }
120    
121            PrintWriter pw = new PrintWriter( (OutputStream) destination );
122    
123            BufferedImage im = ( (AbstractGridCoverage) coverage ).getAsImage( width, height );
124            Image2RawData i2r = new Image2RawData( im );
125            float[][] data = i2r.parse();
126    
127            Envelope env = coverage.getEnvelope();
128    
129            GeoTransform gt = new WorldToScreenTransform( env.getMin().getX(), env.getMin().getY(), env.getMax().getX(),
130                                                          env.getMax().getY(), 0, 0, im.getWidth() - 1, im.getHeight() - 1 );
131            double offset = 0;
132            double scaleFactor = 1;
133            if ( metadata.get( "offset" ) != null ) {
134                offset = (Double) metadata.get( "offset" );
135            }
136            if ( metadata.get( "scaleFactor" ) != null ) {
137                scaleFactor = (Double) metadata.get( "scaleFactor" );
138            }
139    
140            for ( int r = 0; r < data.length; r++ ) {
141                for ( int c = 0; c < data[r].length; c++ ) {
142                    double x = gt.getSourceX( c );
143                    double y = gt.getSourceY( r );
144                    pw.print( x );
145                    pw.print( ' ' );
146                    pw.print( y );
147                    pw.print( ' ' );
148                    double d = ( data[r][c] / scaleFactor ) - offset;
149                    pw.print( nf.format( d ).replace( ',', '.' ) );
150                    pw.print( "\n" );
151                }
152            }
153            pw.flush();
154        }
155    
156    }