001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/model/coverage/grid/GeoTIFFGridCoverageWriter.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.image.BufferedImage; 047 import java.io.IOException; 048 import java.io.OutputStream; 049 import java.util.Map; 050 051 import org.deegree.datatypes.parameter.GeneralParameterValueIm; 052 import org.deegree.datatypes.parameter.InvalidParameterNameException; 053 import org.deegree.datatypes.parameter.InvalidParameterValueException; 054 import org.deegree.datatypes.parameter.OperationParameterIm; 055 import org.deegree.datatypes.parameter.ParameterNotFoundException; 056 import org.deegree.framework.log.ILogger; 057 import org.deegree.framework.log.LoggerFactory; 058 import org.deegree.i18n.Messages; 059 import org.deegree.io.geotiff.GeoTiffWriter; 060 import org.deegree.model.crs.CoordinateSystem; 061 import org.deegree.model.spatialschema.Envelope; 062 import org.deegree.model.spatialschema.GeometryFactory; 063 import org.opengis.pt.PT_Envelope; 064 065 /** 066 * This class encapsulates functionality for writing a <tt>GridCoverage</tt> as a GeoTIFF to a 067 * defined destination. Ths destination will be given as an <tt>OutputStream</tt>. The current 068 * implementation is limited to support <tt>ImageGridCoverage</tt>s to be written as GeoTIFF. 069 * 070 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 071 * @author last edited by: $Author: apoth $ 072 * 073 * @version $Revision: 9343 $, $Date: 2007-12-27 14:30:32 +0100 (Do, 27 Dez 2007) $ 074 */ 075 public class GeoTIFFGridCoverageWriter extends AbstractGridCoverageWriter { 076 077 private static final ILogger LOG = LoggerFactory.getLogger( GeoTIFFGridCoverageWriter.class ); 078 079 /** 080 * @param destination 081 * @param metadata 082 * @param subNames 083 * @param currentSubname 084 * @param format 085 */ 086 public GeoTIFFGridCoverageWriter( Object destination, Map<String, Object> metadata, String[] subNames, 087 String currentSubname, Format format ) { 088 super( destination, metadata, subNames, currentSubname, format ); 089 } 090 091 /** 092 * Writes the specified grid coverage. The GridCoverage will be written in its original size 093 * (width/height). 094 * 095 * @see #write(GridCoverage, int, int, GeneralParameterValue[]) 096 * 097 * @param coverage 098 * The {@linkplain GridCoverage grid coverage} to write. 099 * @param parameters 100 * An optional set of parameters. Should be any or all of the parameters returned by 101 * {@link "org.opengis.coverage.grid.Format#getWriteParameters"}. 102 * @throws InvalidParameterNameException 103 * if a parameter in <code>parameters</code> doesn't have a recognized name. 104 * @throws InvalidParameterValueException 105 * if a parameter in <code>parameters</code> doesn't have a valid value. 106 * @throws ParameterNotFoundException 107 * if a parameter was required for the operation but was not provided in the 108 * <code>parameters</code> list. 109 * @throws FileFormatNotCompatibleWithGridCoverageException 110 * if the grid coverage can't be exported in the 111 * {@linkplain org.opengis.coverage.grid.GridCoverageWriter#getFormat writer format}. 112 * @throws IOException 113 * if the export failed for some other input/output reason, including 114 * {@link javax.imageio.IIOException} if an error was thrown by the underlying image 115 * library. 116 */ 117 public void write( GridCoverage coverage, GeneralParameterValueIm[] parameters ) 118 throws InvalidParameterNameException, InvalidParameterValueException, 119 ParameterNotFoundException, IOException { 120 121 int width = -1; 122 int height = -1; 123 for ( int i = 0; i < parameters.length; i++ ) { 124 OperationParameterIm op = (OperationParameterIm) parameters[i].getDescriptor(); 125 String name = op.getName(); 126 if ( name.equalsIgnoreCase( "WIDTH" ) ) { 127 Object o = op.getDefaultValue(); 128 width = ( (Integer) o ).intValue(); 129 } else if ( name.equalsIgnoreCase( "HEIGHT" ) ) { 130 Object o = op.getDefaultValue(); 131 height = ( (Integer) o ).intValue(); 132 } 133 } 134 135 OutputStream out = (OutputStream) destination; 136 AbstractGridCoverage igc = (AbstractGridCoverage) coverage; 137 BufferedImage bi = igc.getAsImage( width, height ); 138 PT_Envelope ptenv = igc.getEnvelope(); 139 double xmin = ptenv.minCP.ord[0]; 140 double ymin = ptenv.minCP.ord[1]; 141 double xmax = ptenv.maxCP.ord[0]; 142 double ymax = ptenv.maxCP.ord[1]; 143 CoordinateSystem crs = null; 144 Envelope envelope = GeometryFactory.createEnvelope( xmin, ymin, xmax, ymax, null ); 145 try { 146 double offset = 0; 147 double scaleFactor = 1; 148 if ( metadata.get( "offset" ) != null ) { 149 offset = (Double) metadata.get( "offset" ); 150 } 151 if ( metadata.get( "scaleFactor" ) != null ) { 152 scaleFactor = (Double) metadata.get( "scaleFactor" ); 153 } 154 155 GeoTiffWriter gtw = new GeoTiffWriter( bi, envelope, ( xmax - xmin ) / bi.getWidth(), ( ymax - ymin ) 156 / bi.getHeight(), 157 crs, offset, scaleFactor ); 158 159 gtw.write( out ); 160 161 } catch ( Exception e ) { 162 LOG.logError( e.getMessage(), e ); 163 throw new IOException( Messages.getMessage( "GC_ERROR_GEOTIFFWRITER" ) ); 164 } 165 } 166 167 /** 168 * Allows any resources held by this object to be released. The result of calling any other 169 * method subsequent to a call to this method is undefined. It is important for applications to 170 * call this method when they know they will no longer be using this 171 * <code>GridCoverageWriter</code>. Otherwise, the writer may continue to hold on to 172 * resources indefinitely. 173 * 174 * @throws IOException 175 * if an error occured while disposing resources (for example while flushing data 176 * and closing a file). 177 */ 178 public void dispose() 179 throws IOException { 180 ( (OutputStream) destination ).close(); 181 } 182 183 }