001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/model/coverage/grid/XYZGridCoverageWriter.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.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: 9343 $, $Date: 2007-12-27 05:30:32 -0800 (Thu, 27 Dec 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,
086: Map<String, Object> metadata, String[] subNames,
087: String currentSubname, Format format) {
088: super (destination, metadata, subNames, currentSubname, format);
089:
090: }
091:
092: /**
093: * disposes all resources assigned to a GMLGridCoverageWriter instance. For most cases this will
094: * be IO-resources
095: *
096: * @throws IOException
097: */
098: public void dispose() 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,
112: GeneralParameterValueIm[] parameters)
113: throws InvalidParameterNameException,
114: InvalidParameterValueException, ParameterNotFoundException,
115: IOException {
116:
117: int width = -1;
118: int height = -1;
119: for (int i = 0; i < parameters.length; i++) {
120: OperationParameterIm op = (OperationParameterIm) parameters[i]
121: .getDescriptor();
122: String name = op.getName();
123: if (name.equalsIgnoreCase("WIDTH")) {
124: Object o = op.getDefaultValue();
125: width = ((Integer) o).intValue();
126: } else if (name.equalsIgnoreCase("HEIGHT")) {
127: Object o = op.getDefaultValue();
128: height = ((Integer) o).intValue();
129: }
130: }
131:
132: PrintWriter pw = new PrintWriter((OutputStream) destination);
133:
134: BufferedImage im = ((AbstractGridCoverage) coverage)
135: .getAsImage(width, height);
136: Image2RawData i2r = new Image2RawData(im);
137: float[][] data = i2r.parse();
138:
139: PT_Envelope env = coverage.getEnvelope();
140:
141: GeoTransform gt = new WorldToScreenTransform(env.minCP.ord[0],
142: env.minCP.ord[1], env.maxCP.ord[0], env.maxCP.ord[1],
143: 0, 0, im.getWidth() - 1, im.getHeight() - 1);
144: double offset = 0;
145: double scaleFactor = 1;
146: if (metadata.get("offset") != null) {
147: offset = (Double) metadata.get("offset");
148: }
149: if (metadata.get("scaleFactor") != null) {
150: scaleFactor = (Double) metadata.get("scaleFactor");
151: }
152:
153: for (int r = 0; r < data.length; r++) {
154: for (int c = 0; c < data[r].length; c++) {
155: double x = gt.getSourceX(c);
156: double y = gt.getSourceY(r);
157: pw.print(x);
158: pw.print(' ');
159: pw.print(y);
160: pw.print(' ');
161: double d = (data[r][c] / scaleFactor) - offset;
162: pw.print(nf.format(d).replace(',', '.'));
163: pw.print("\n");
164: }
165: }
166: pw.flush();
167: }
168:
169: }
|