001: //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/tools/raster/SimpleText2Tiff.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: 53177 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@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.tools.raster;
044:
045: import java.awt.color.ColorSpace;
046: import java.awt.image.BufferedImage;
047: import java.awt.image.ComponentColorModel;
048: import java.awt.image.DataBuffer;
049: import java.awt.image.Raster;
050: import java.awt.image.WritableRaster;
051: import java.io.BufferedReader;
052: import java.io.File;
053: import java.io.FileReader;
054: import java.io.IOException;
055: import java.util.ArrayList;
056: import java.util.Hashtable;
057: import java.util.List;
058: import java.util.Properties;
059:
060: import org.deegree.framework.util.ConvenienceFileFilter;
061: import org.deegree.framework.util.ImageUtils;
062: import org.deegree.framework.util.StringTools;
063: import org.deegree.model.coverage.grid.WorldFile;
064: import org.deegree.model.spatialschema.Envelope;
065: import org.deegree.model.spatialschema.GeometryFactory;
066:
067: /**
068: * This class ist similar to Text2Tiff. The major difference is that SimpleText2Tiff just is able to transform x y z
069: * formateted textfiles into 16BIT tiff images if the text files contains equal distance rasters. Missing raster cells
070: * will be filled with '0'.<br>
071: * The major advantage of SimpleText2Tiff is its speed. It is significantly faster than Text2Tiff because several checks
072: * and calculations can be skippted.
073: *
074: *
075: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
076: * @author last edited by: $Author: poth $
077: *
078: * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
079: */
080: public class SimpleText2Tiff {
081:
082: private File[] files;
083:
084: private double resolution;
085:
086: private float offset = 0;
087:
088: private float scaleFactor = 1;
089:
090: private boolean use32Bit = false;
091:
092: /**
093: *
094: * @param files
095: * list of text files to tranform
096: * @param resolution
097: * desired target resolution
098: * @param offset
099: * desired z-value offset
100: * @param scaleFactor
101: * desired z-value scale factor [value = (z + offset) * scaleFactor]
102: * @param use32Bit
103: */
104: public SimpleText2Tiff(File[] files, double resolution,
105: float offset, float scaleFactor, boolean use32Bit) {
106: this .files = files;
107: this .resolution = resolution;
108: this .offset = offset;
109: this .scaleFactor = scaleFactor;
110: this .use32Bit = use32Bit;
111: }
112:
113: /**
114: * starts transformation
115: *
116: * @throws Exception
117: */
118: public void perform() throws Exception {
119: for (int i = 0; i < files.length; i++) {
120: System.out.println("process: " + files[i]);
121: text2tiff(files[i]);
122: }
123: }
124:
125: /**
126: *
127: * @param file
128: * @throws Exception
129: */
130: private void text2tiff(File file) throws Exception {
131: Envelope bbox = getBoundingBox(file);
132: int width = (int) Math.round(bbox.getWidth() / resolution) + 1;
133: int height = (int) Math.round(bbox.getHeight() / resolution) + 1;
134:
135: BufferedImage out = null;
136: if (use32Bit) {
137: out = new BufferedImage(width, height,
138: BufferedImage.TYPE_INT_ARGB);
139: } else {
140: ComponentColorModel ccm = new ComponentColorModel(
141: ColorSpace.getInstance(ColorSpace.CS_GRAY), null,
142: false, false, BufferedImage.OPAQUE,
143: DataBuffer.TYPE_USHORT);
144: WritableRaster wr = ccm.createCompatibleWritableRaster(
145: width, height);
146: out = new BufferedImage(ccm, wr, false,
147: new Hashtable<String, Object>());
148: }
149: DataBuffer buffer = out.getRaster().getDataBuffer();
150:
151: BufferedReader br = new BufferedReader(new FileReader(file));
152: String line = null;
153: while ((line = br.readLine()) != null) {
154: double[] d = StringTools.toArrayDouble(line.trim(), " \t");
155: int x = (int) Math.round((d[0] - bbox.getMin().getX())
156: / resolution);
157: int y = height
158: - (int) Math.round((d[1] - bbox.getMin().getY())
159: / resolution) - 1;
160: int pos = width * y + x;
161:
162: try {
163: if (use32Bit) {
164: buffer
165: .setElem(
166: pos,
167: Float
168: .floatToIntBits((float) ((d[2] + offset) * scaleFactor)));
169: } else {
170: // buffer.setElemFloat( pos, (float) ( ( d[2] + offset ) * scaleFactor ) );
171: buffer.setElem(pos, (int) Math
172: .round((d[2] + offset) * scaleFactor));
173: }
174: buffer.setElem(pos, Float.floatToIntBits((float) d[2]));
175: } catch (Exception e) {
176: System.out.println("-------------------------------");
177: System.out.println(buffer.getSize());
178: System.out.println("file bbox: " + bbox);
179: System.out.println("last line read: " + line);
180: throw e;
181: }
182: }
183: br.close();
184: out.setData(Raster.createRaster(out.getSampleModel(), buffer,
185: null));
186:
187: int pos = file.getAbsolutePath().lastIndexOf('.');
188: if (pos < 0) {
189: pos = file.getAbsolutePath().length();
190: }
191: String fileName = file.getAbsolutePath().substring(0, pos)
192: + ".tif";
193: ImageUtils.saveImage(out, fileName, 1);
194: WorldFile wf = new WorldFile(resolution, resolution, 0, 0, bbox);
195: WorldFile.writeWorldFile(wf, file.getAbsolutePath().substring(
196: 0, pos));
197:
198: }
199:
200: /**
201: *
202: * @param file
203: * @return the boundingbox of the geometry read from the given file as an envelope.
204: * @throws IOException
205: */
206: private Envelope getBoundingBox(File file) throws IOException {
207: BufferedReader br = new BufferedReader(new FileReader(file));
208: String line = null;
209: double minx = Double.MAX_VALUE;
210: double miny = Double.MAX_VALUE;
211: double maxx = Double.MIN_VALUE;
212: double maxy = Double.MIN_VALUE;
213:
214: while ((line = br.readLine()) != null) {
215: double[] d = StringTools.toArrayDouble(line.trim(), " \t");
216: if (d[0] < minx) {
217: minx = d[0];
218: }
219: if (d[0] > maxx) {
220: maxx = d[0];
221: }
222:
223: if (d[1] < miny) {
224: miny = d[1];
225: }
226: if (d[1] > maxy) {
227: maxy = d[1];
228: }
229: }
230: br.close();
231: return GeometryFactory.createEnvelope(minx, miny, maxx, maxy,
232: null);
233: }
234:
235: /**
236: * @param args
237: * @throws Exception
238: * if something went wrong.
239: */
240: public static void main(String[] args) throws Exception {
241:
242: Properties map = new Properties();
243: for (int i = 0; i < args.length; i += 2) {
244: map.put(args[i], args[i + 1]);
245: }
246: if (!validate(map)) {
247: System.out
248: .println("Parameters: -rootDir, -resolution, -offset and -scaleFactor must be set");
249: return;
250: }
251:
252: List<File> fileList = new ArrayList<File>(1000);
253: File dir = new File(map.getProperty("-rootDir"));
254: File[] files = null;
255: ConvenienceFileFilter cff = null;
256: if (map.getProperty("-extension") != null) {
257: cff = new ConvenienceFileFilter(true, map
258: .getProperty("-extension"));
259: files = dir.listFiles(cff);
260: } else {
261: files = dir.listFiles();
262: }
263: for (int i = 0; i < files.length; i++) {
264: if (files[i].isDirectory()) {
265: readSubDirs(files[i], fileList, cff);
266: } else {
267: fileList.add(files[i]);
268: }
269: }
270:
271: double resolution = Double.parseDouble(map
272: .getProperty("-resolution"));
273: float offset = Float.parseFloat(map.getProperty("-offset"));
274: float scaleFactor = Float.parseFloat(map
275: .getProperty("-scaleFactor"));
276: boolean use32Bit = "true".equals(map.getProperty("-use32Bit"));
277: SimpleText2Tiff t2t = new SimpleText2Tiff(fileList
278: .toArray(new File[fileList.size()]), resolution,
279: offset, scaleFactor, use32Bit);
280: t2t.perform();
281:
282: }
283:
284: /**
285: *
286: * @param file
287: * @param list
288: * @param cff
289: * @return list of files
290: */
291: private static List<File> readSubDirs(File file, List<File> list,
292: ConvenienceFileFilter cff) {
293: File[] entries = null;
294: if (cff != null) {
295: entries = file.listFiles(cff);
296: } else {
297: entries = file.listFiles();
298: }
299: if (entries != null) {
300: for (int i = 0; i < entries.length; i++) {
301: if (entries[i].isDirectory()) {
302: list = readSubDirs(entries[i], list, cff);
303: } else {
304: list.add(entries[i]);
305: }
306: }
307: }
308: return list;
309: }
310:
311: /**
312: *
313: * @param param
314: * @return true if everything is correct
315: * @throws Exception
316: */
317: private static boolean validate(Properties param) throws Exception {
318:
319: if (param.get("-rootDir") == null) {
320: System.out.println("parameter -rootDir must be set");
321: return false;
322: }
323: if (param.get("-resolution") == null) {
324: System.out.println("parameter -resolution must be set");
325: return false;
326: }
327: if (param.get("-offset") == null) {
328: System.out.println("parameter -offset must be set");
329: return false;
330: }
331: if (param.get("-scaleFactor") == null) {
332: System.out.println("parameter -scaleFactor must be set");
333: return false;
334: }
335:
336: return true;
337: }
338:
339: }
|