001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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 05:30:32 -0800 (Thu, 27 Dec 2007) $
075: */
076: public class GMLGridCoverageWriter extends AbstractGridCoverageWriter {
077:
078: private static ILogger LOG = LoggerFactory
079: .getLogger(GMLGridCoverageWriter.class);
080:
081: private URL template = GMLGridCoverageWriter.class
082: .getResource("gml_rectifiedgrid_template.xml");
083:
084: /**
085: *
086: * @param destination
087: * @param metadata
088: * @param subNames
089: * @param currentSubname
090: * @param format
091: */
092: public GMLGridCoverageWriter(Object destination,
093: Map<String, Object> metadata, String[] subNames,
094: String currentSubname, Format format) {
095: super (destination, metadata, subNames, currentSubname, format);
096:
097: }
098:
099: /**
100: * disposes all resources assigned to a GMLGridCoverageWriter instance. For most cases this will
101: * be IO-resources
102: *
103: * @throws IOException
104: */
105: public void dispose() throws IOException {
106:
107: }
108:
109: /**
110: * @param coverage
111: * @param parameters
112: * must contain the servlet URL within the first field; all other fields must contain
113: * the required parameters for a valid GetCoverage request
114: * @throws InvalidParameterNameException
115: * @throws InvalidParameterValueException
116: * @throws ParameterNotFoundException
117: * @throws IOException
118: */
119: public void write(GridCoverage coverage,
120: GeneralParameterValueIm[] parameters)
121: throws InvalidParameterNameException,
122: InvalidParameterValueException, ParameterNotFoundException,
123: IOException {
124: XMLFragment xml = new XMLFragment();
125: try {
126: xml.load(template);
127: Element root = xml.getRootElement();
128: NamespaceContext nsc = CommonNamespaces
129: .getNamespaceContext();
130:
131: String xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:limits/gml:GridEnvelope/gml:low";
132: Element element = (Element) XMLTools.getNode(root, xpath,
133: nsc);
134: double x = coverage.getEnvelope().minCP.ord[0];
135: double y = coverage.getEnvelope().minCP.ord[1];
136: Node node = root.getOwnerDocument().createTextNode(
137: Double.toString(x) + ' ' + Double.toString(y));
138: element.appendChild(node);
139:
140: xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:limits/gml:GridEnvelope/gml:high";
141: element = (Element) XMLTools.getNode(root, xpath, nsc);
142: x = coverage.getEnvelope().maxCP.ord[0];
143: y = coverage.getEnvelope().maxCP.ord[1];
144: node = root.getOwnerDocument().createTextNode(
145: Double.toString(x) + ' ' + Double.toString(y));
146: element.appendChild(node);
147:
148: xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:origin/gml:Point";
149: element = (Element) XMLTools.getNode(root, xpath, nsc);
150: element.setAttribute("srsName", coverage
151: .getCoordinateReferenceSystem().getName());
152:
153: xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:origin/gml:Point/gml:pos";
154: element = (Element) XMLTools.getNode(root, xpath, nsc);
155: x = coverage.getEnvelope().minCP.ord[0];
156: y = coverage.getEnvelope().minCP.ord[1];
157: node = root.getOwnerDocument().createTextNode(
158: Double.toString(x) + ' ' + Double.toString(y));
159: element.appendChild(node);
160:
161: double[] res = calcGridResolution(coverage, parameters);
162:
163: xpath = "gml:rectifiedGridDomain/gml:RectifiedGrid/gml:offsetVector";
164: List list = XMLTools.getNodes(root, xpath, nsc);
165: for (int i = 0; i < list.size(); i++) {
166: element = (Element) list.get(i);
167: element.setAttribute("srsName", coverage
168: .getCoordinateReferenceSystem().getName());
169: if (i == 0) {
170: node = root.getOwnerDocument().createTextNode(
171: res[i] + " 0");
172: element.appendChild(node);
173: } else if (i == 1) {
174: node = root.getOwnerDocument().createTextNode(
175: "0 " + res[i]);
176: element.appendChild(node);
177: } else if (i == 2) {
178: node = root.getOwnerDocument().createTextNode(
179: "0 0 " + res[i]);
180: element.appendChild(node);
181: }
182: }
183:
184: xpath = "gml:rangeSet/gml:File/gml:fileName";
185: element = (Element) XMLTools.getNode(root, xpath, nsc);
186: StringBuffer sb = new StringBuffer(300);
187: OperationParameterIm op = (OperationParameterIm) parameters[0]
188: .getDescriptor();
189: sb.append(op.getDefaultValue()).append('?');
190: for (int i = 1; i < parameters.length; i++) {
191: // OperationParameter
192: op = (OperationParameterIm) parameters[i]
193: .getDescriptor();
194: sb.append(op.getName());
195: sb.append('=').append(op.getDefaultValue());
196: if (i < parameters.length - 1) {
197: sb.append('&');
198: }
199: }
200: node = root.getOwnerDocument().createCDATASection(
201: sb.toString());
202: element.appendChild(node);
203: } catch (XMLParsingException e) {
204: LOG
205: .logError(
206: "could not parse GMLGridCoverage response template",
207: e);
208: throw new InvalidParameterValueException("",
209: e.getMessage(), "");
210: } catch (Exception e) {
211: LOG.logError("could not write GMLGridCoverage", e);
212: throw new InvalidParameterValueException("",
213: e.getMessage(), "");
214: }
215: xml.write(((OutputStream) destination));
216:
217: }
218:
219: /**
220: * returns the resolution of the grid in x- and y- directory
221: *
222: * @param coverage
223: * @param parameters
224: * @return the resolution of the grid in x- and y- directory
225: */
226: private double[] calcGridResolution(GridCoverage coverage,
227: GeneralParameterValueIm[] parameters) {
228: double wx = coverage.getEnvelope().maxCP.ord[0]
229: - coverage.getEnvelope().minCP.ord[0];
230: double wy = coverage.getEnvelope().maxCP.ord[1]
231: - coverage.getEnvelope().minCP.ord[1];
232: Integer width = (Integer) getNamedParameter(parameters, "width")
233: .getDefaultValue();
234: Integer height = (Integer) getNamedParameter(parameters,
235: "height").getDefaultValue();
236: double dx = wx / width.doubleValue();
237: double dy = wy / height.doubleValue();
238: double[] res = new double[] { dx, dy };
239: return res;
240: }
241:
242: /**
243: * selects the parameter matching the passed name from the passed array
244: *
245: * @param parameters
246: * @param name
247: * @return
248: */
249: private OperationParameterIm getNamedParameter(
250: GeneralParameterValueIm[] parameters, String name) {
251: for (int i = 0; i < parameters.length; i++) {
252: // OperationParameter
253: OperationParameterIm op = (OperationParameterIm) parameters[i]
254: .getDescriptor();
255: if (op.getName().equals(name)) {
256: return op;
257: }
258: }
259: return null;
260: }
261:
262: }
|