001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/model/coverage/grid/GMLGridCoverageWriter.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 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.io.IOException; 047 import java.io.OutputStream; 048 import java.net.URL; 049 import java.util.List; 050 import java.util.Map; 051 052 import org.deegree.datatypes.parameter.GeneralParameterValueIm; 053 import org.deegree.datatypes.parameter.InvalidParameterNameException; 054 import org.deegree.datatypes.parameter.InvalidParameterValueException; 055 import org.deegree.datatypes.parameter.OperationParameterIm; 056 import org.deegree.datatypes.parameter.ParameterNotFoundException; 057 import org.deegree.framework.log.ILogger; 058 import org.deegree.framework.log.LoggerFactory; 059 import org.deegree.framework.xml.NamespaceContext; 060 import org.deegree.framework.xml.XMLFragment; 061 import org.deegree.framework.xml.XMLParsingException; 062 import org.deegree.framework.xml.XMLTools; 063 import org.deegree.ogcbase.CommonNamespaces; 064 import org.w3c.dom.Element; 065 import org.w3c.dom.Node; 066 067 /** 068 * Implementation of {@link "org.opengis.coverage.grid.GridCoverageWriter"} for writing a 069 * GridCoverage as GML document to a defined destioation 070 * 071 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 072 * @author last edited by: $Author: apoth $ 073 * 074 * @version $Revision: 9343 $, $Date: 2007-12-27 14:30:32 +0100 (Do, 27 Dez 2007) $ 075 */ 076 public class GMLGridCoverageWriter extends AbstractGridCoverageWriter { 077 078 private static ILogger LOG = LoggerFactory.getLogger( GMLGridCoverageWriter.class ); 079 080 private URL template = GMLGridCoverageWriter.class.getResource( "gml_rectifiedgrid_template.xml" ); 081 082 /** 083 * 084 * @param destination 085 * @param metadata 086 * @param subNames 087 * @param currentSubname 088 * @param format 089 */ 090 public GMLGridCoverageWriter( Object destination, Map<String, Object> metadata, String[] subNames, 091 String currentSubname, Format format ) { 092 super( destination, metadata, subNames, currentSubname, format ); 093 094 } 095 096 /** 097 * disposes all resources assigned to a GMLGridCoverageWriter instance. For most cases this will 098 * be IO-resources 099 * 100 * @throws IOException 101 */ 102 public void dispose() 103 throws IOException { 104 105 } 106 107 /** 108 * @param coverage 109 * @param parameters 110 * must contain the servlet URL within the first field; all other fields must contain 111 * the required parameters for a valid GetCoverage request 112 * @throws InvalidParameterNameException 113 * @throws InvalidParameterValueException 114 * @throws ParameterNotFoundException 115 * @throws IOException 116 */ 117 public void write( GridCoverage coverage, GeneralParameterValueIm[] parameters ) 118 throws InvalidParameterNameException, InvalidParameterValueException, 119 ParameterNotFoundException, IOException { 120 XMLFragment xml = new XMLFragment(); 121 try { 122 xml.load( template ); 123 Element root = xml.getRootElement(); 124 NamespaceContext nsc = CommonNamespaces.getNamespaceContext(); 125 126 String xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:limits/gml:GridEnvelope/gml:low"; 127 Element element = (Element) XMLTools.getNode( root, xpath, nsc ); 128 double x = coverage.getEnvelope().minCP.ord[0]; 129 double y = coverage.getEnvelope().minCP.ord[1]; 130 Node node = root.getOwnerDocument().createTextNode( Double.toString( x ) + ' ' + Double.toString( y ) ); 131 element.appendChild( node ); 132 133 xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:limits/gml:GridEnvelope/gml:high"; 134 element = (Element) XMLTools.getNode( root, xpath, nsc ); 135 x = coverage.getEnvelope().maxCP.ord[0]; 136 y = coverage.getEnvelope().maxCP.ord[1]; 137 node = root.getOwnerDocument().createTextNode( Double.toString( x ) + ' ' + Double.toString( y ) ); 138 element.appendChild( node ); 139 140 xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:origin/gml:Point"; 141 element = (Element) XMLTools.getNode( root, xpath, nsc ); 142 element.setAttribute( "srsName", coverage.getCoordinateReferenceSystem().getName() ); 143 144 xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:origin/gml:Point/gml:pos"; 145 element = (Element) XMLTools.getNode( root, xpath, nsc ); 146 x = coverage.getEnvelope().minCP.ord[0]; 147 y = coverage.getEnvelope().minCP.ord[1]; 148 node = root.getOwnerDocument().createTextNode( Double.toString( x ) + ' ' + Double.toString( y ) ); 149 element.appendChild( node ); 150 151 double[] res = calcGridResolution( coverage, parameters ); 152 153 xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:offsetVector"; 154 List list = XMLTools.getNodes( root, xpath, nsc ); 155 for ( int i = 0; i < list.size(); i++ ) { 156 element = (Element) list.get( i ); 157 element.setAttribute( "srsName", coverage.getCoordinateReferenceSystem().getName() ); 158 if ( i == 0 ) { 159 node = root.getOwnerDocument().createTextNode( res[i] + " 0" ); 160 element.appendChild( node ); 161 } else if ( i == 1 ) { 162 node = root.getOwnerDocument().createTextNode( "0 " + res[i] ); 163 element.appendChild( node ); 164 } else if ( i == 2 ) { 165 node = root.getOwnerDocument().createTextNode( "0 0 " + res[i] ); 166 element.appendChild( node ); 167 } 168 } 169 170 xpath = "gml:rangeSet/gml:File/gml:fileName"; 171 element = (Element) XMLTools.getNode( root, xpath, nsc ); 172 StringBuffer sb = new StringBuffer( 300 ); 173 OperationParameterIm op = (OperationParameterIm) parameters[0].getDescriptor(); 174 sb.append( op.getDefaultValue() ).append( '?' ); 175 for ( int i = 1; i < parameters.length; i++ ) { 176 // OperationParameter 177 op = (OperationParameterIm) parameters[i].getDescriptor(); 178 sb.append( op.getName() ); 179 sb.append( '=' ).append( op.getDefaultValue() ); 180 if ( i < parameters.length - 1 ) { 181 sb.append( '&' ); 182 } 183 } 184 node = root.getOwnerDocument().createCDATASection( sb.toString() ); 185 element.appendChild( node ); 186 } catch ( XMLParsingException e ) { 187 LOG.logError( "could not parse GMLGridCoverage response template", e ); 188 throw new InvalidParameterValueException( "", e.getMessage(), "" ); 189 } catch ( Exception e ) { 190 LOG.logError( "could not write GMLGridCoverage", e ); 191 throw new InvalidParameterValueException( "", e.getMessage(), "" ); 192 } 193 xml.write( ( (OutputStream) destination ) ); 194 195 } 196 197 /** 198 * returns the resolution of the grid in x- and y- directory 199 * 200 * @param coverage 201 * @param parameters 202 * @return the resolution of the grid in x- and y- directory 203 */ 204 private double[] calcGridResolution( GridCoverage coverage, GeneralParameterValueIm[] parameters ) { 205 double wx = coverage.getEnvelope().maxCP.ord[0] - coverage.getEnvelope().minCP.ord[0]; 206 double wy = coverage.getEnvelope().maxCP.ord[1] - coverage.getEnvelope().minCP.ord[1]; 207 Integer width = (Integer) getNamedParameter( parameters, "width" ).getDefaultValue(); 208 Integer height = (Integer) getNamedParameter( parameters, "height" ).getDefaultValue(); 209 double dx = wx / width.doubleValue(); 210 double dy = wy / height.doubleValue(); 211 double[] res = new double[] { dx, dy }; 212 return res; 213 } 214 215 /** 216 * selects the parameter matching the passed name from the passed array 217 * 218 * @param parameters 219 * @param name 220 * @return 221 */ 222 private OperationParameterIm getNamedParameter( GeneralParameterValueIm[] parameters, String name ) { 223 for ( int i = 0; i < parameters.length; i++ ) { 224 // OperationParameter 225 OperationParameterIm op = (OperationParameterIm) parameters[i].getDescriptor(); 226 if ( op.getName().equals( name ) ) { 227 return op; 228 } 229 } 230 return null; 231 } 232 233 }