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