Source Code Cross Referenced for Text2Tiff.java in  » GIS » deegree » org » deegree » tools » raster » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » deegree » org.deegree.tools.raster 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/tools/raster/Text2Tiff.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.ColorModel;
048:        import java.awt.image.ComponentColorModel;
049:        import java.awt.image.DataBuffer;
050:        import java.awt.image.Raster;
051:        import java.awt.image.WritableRaster;
052:        import java.io.BufferedReader;
053:        import java.io.File;
054:        import java.io.FileOutputStream;
055:        import java.io.FileReader;
056:        import java.io.IOException;
057:        import java.io.PrintStream;
058:        import java.util.ArrayList;
059:        import java.util.Arrays;
060:        import java.util.Hashtable;
061:        import java.util.List;
062:        import java.util.StringTokenizer;
063:
064:        import org.deegree.datatypes.values.Interval;
065:        import org.deegree.datatypes.values.TypedLiteral;
066:        import org.deegree.datatypes.values.Values;
067:        import org.deegree.framework.util.ConvenienceFileFilter;
068:        import org.deegree.framework.util.ImageUtils;
069:        import org.deegree.io.quadtree.IndexException;
070:        import org.deegree.io.quadtree.MemPointQuadtree;
071:        import org.deegree.io.quadtree.Quadtree;
072:        import org.deegree.model.coverage.grid.WorldFile;
073:        import org.deegree.model.spatialschema.Envelope;
074:        import org.deegree.model.spatialschema.GeometryFactory;
075:        import org.deegree.model.spatialschema.Point;
076:        import org.deegree.processing.raster.interpolation.DataTuple;
077:        import org.deegree.processing.raster.interpolation.InterpolationException;
078:        import org.deegree.processing.raster.interpolation.InverseDistanceToPower;
079:
080:        /**
081:         * This class converts geodata and special values from a simple text file format to a .tiff file
082:         * format. The values are written as 32 bit float values. The <code>main</code> method should be
083:         * used to utilise this class as a command line tool.
084:         * 
085:         * 
086:         * @version $Revision: 9346 $
087:         * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
088:         * @author last edited by: $Author: apoth $
089:         * 
090:         * @version 1.0. $Revision: 9346 $, $Date: 2007-12-27 08:39:07 -0800 (Thu, 27 Dec 2007) $
091:         * 
092:         * @since 2.0
093:         */
094:        public class Text2Tiff {
095:
096:            // parameters
097:            private int columnNumber = 0;
098:
099:            private double resolution = 0;
100:
101:            private String columnName = null;
102:
103:            private List<String> inputFilenames = new ArrayList<String>();
104:
105:            private String outputFilename = null;
106:
107:            private boolean readHeader = false;
108:
109:            private boolean oracle = false;
110:
111:            private Envelope boundingBox = null;
112:
113:            private boolean interpolate = false;
114:
115:            private boolean use32Bits = false;
116:
117:            // data
118:            private BufferedReader in;
119:
120:            private Raster raster;
121:
122:            private Quadtree quadtree;
123:
124:            private BufferedImage image;
125:
126:            private int imageWidth;
127:
128:            private int imageHeight;
129:
130:            private DataBuffer buffer;
131:
132:            private float offset = 0;
133:
134:            // interpolating options
135:            private double interpolatePower = 2;
136:
137:            private int interpolateMinData = 5;
138:
139:            private int interpolateMaxData = 20;
140:
141:            private double interpolateNoValue = 0;
142:
143:            private double interpolateRadiusX = 2;
144:
145:            private double interpolateRadiusY = 2;
146:
147:            private double interpolateRadiusIncreaseX = 0;
148:
149:            private double interpolateRadiusIncreaseY = 0;
150:
151:            private Values ignoreValues = null;
152:
153:            /**
154:             * The only constructor, called usually by the main method with command line arguments.
155:             * 
156:             * @param args
157:             */
158:            public Text2Tiff(String[] args) {
159:                if (args.length < 3) {
160:                    printUsage("Not enough arguments.");
161:                }
162:
163:                parseArgs(args);
164:
165:                // check for consistency
166:                if ((columnName != null) && !readHeader) {
167:                    printUsage("If a column name is given, I have to read the header!");
168:                }
169:
170:                if (inputFilenames.size() == 0) {
171:                    printUsage("No input filename given.");
172:                }
173:
174:                if (outputFilename == null) {
175:                    printUsage("No output filename given.");
176:                }
177:
178:                if (columnName == null && columnNumber == 0) {
179:                    printUsage("No column specified.");
180:                }
181:
182:                if (!readHeader) {
183:                    --columnNumber;
184:                }
185:            }
186:
187:            private void parseArgs(String[] args) {
188:                List<Interval> intervals = new ArrayList<Interval>();
189:
190:                // parse options
191:                try {
192:                    for (int i = 0; i < (args.length - 1); ++i) {
193:                        if (args[i].equals("--image-type")) {
194:                            use32Bits = args[i + 1].equals("32");
195:                            ++i;
196:                        } else if (args[i].equals("--offset")) {
197:                            offset = Float.parseFloat(args[i + 1]);
198:                            ++i;
199:                        } else if (args[i].equals("--image-width")) {
200:                            imageWidth = Integer.parseInt(args[i + 1]);
201:                            ++i;
202:                        } else if (args[i].equals("--image-height")) {
203:                            imageHeight = Integer.parseInt(args[i + 1]);
204:                            ++i;
205:                        } else if (args[i].equals("-c")
206:                                || args[i].equals("--column-number")) {
207:                            columnNumber = Integer.parseInt(args[i + 1]);
208:                            ++i;
209:                        } else if (args[i].equals("-h")
210:                                || args[i].equals("--no-read-header")) {
211:                            readHeader = false;
212:                        } else if (args[i].equals("-o")
213:                                || args[i].equals("--oracle")) {
214:                            oracle = true;
215:                        } else if (args[i].equals("+h")
216:                                || args[i].equals("--read-header")) {
217:                            readHeader = true;
218:                        } else if (args[i].equals("-cn")
219:                                || args[i].equals("--column-name")) {
220:                            columnName = args[i + 1];
221:                            ++i;
222:                        } else if (args[i].equals("-r")
223:                                || args[i].equals("--resolution")) {
224:                            resolution = Double.parseDouble(args[i + 1]);
225:                            ++i;
226:                        } else if (args[i].equals("-i")
227:                                || args[i].equals("--interpolate")) {
228:                            interpolate = true;
229:                        } else if (args[i].equals("--interpolate-power")) {
230:                            interpolatePower = Double.parseDouble(args[i + 1]);
231:                            ++i;
232:                        } else if (args[i].equals("--interpolate-min-data")) {
233:                            interpolateMinData = Integer.parseInt(args[i + 1]);
234:                            ++i;
235:                        } else if (args[i].equals("--interpolate-max-data")) {
236:                            interpolateMaxData = Integer.parseInt(args[i + 1]);
237:                            ++i;
238:                        } else if (args[i].equals("--interpolate-no-value")) {
239:                            interpolateNoValue = Double
240:                                    .parseDouble(args[i + 1]);
241:                            ++i;
242:                        } else if (args[i].equals("--interpolate-radius-x")) {
243:                            interpolateRadiusX = Double
244:                                    .parseDouble(args[i + 1]);
245:                            ++i;
246:                        } else if (args[i].equals("--interpolate-radius-y")) {
247:                            interpolateRadiusY = Double
248:                                    .parseDouble(args[i + 1]);
249:                            ++i;
250:                        } else if (args[i]
251:                                .equals("--interpolate-radius-increase-x")) {
252:                            interpolateRadiusIncreaseX = Double
253:                                    .parseDouble(args[i + 1]);
254:                            ++i;
255:                        } else if (args[i]
256:                                .equals("--interpolate-radius-increase-y")) {
257:                            interpolateRadiusIncreaseY = Double
258:                                    .parseDouble(args[i + 1]);
259:                            ++i;
260:                        } else if (args[i].equals("--interpolate-ignore-range")) {
261:                            TypedLiteral min = new TypedLiteral(args[i + 1],
262:                                    null);
263:                            TypedLiteral max = new TypedLiteral(args[i + 2],
264:                                    null);
265:                            Interval interval = new Interval(min, max, null,
266:                                    null, null);
267:                            intervals.add(interval);
268:                            i += 2;
269:                        } else if (args[i].equals("--help")) {
270:                            printUsage(null);
271:                        } else if (args[i].equals("-help")) {
272:                            printUsage(null);
273:                        } else {
274:                            File file = new File(args[i]);
275:                            if (file.isDirectory()) {
276:                                File[] files = file
277:                                        .listFiles(new ConvenienceFileFilter(
278:                                                false, "XYZ,TXT"));
279:                                for (int j = 0; j < files.length; j++) {
280:                                    inputFilenames.add(files[j]
281:                                            .getAbsolutePath());
282:                                }
283:                            } else {
284:                                inputFilenames.add(args[i]);
285:                            }
286:                        }
287:
288:                    }
289:                } catch (NumberFormatException nfe) {
290:                    printUsage("Illegal argument, number expected.");
291:                }
292:
293:                // get file names
294:                outputFilename = args[args.length - 1];
295:                if (intervals.size() != 0) {
296:                    ignoreValues = new Values(intervals
297:                            .toArray(new Interval[intervals.size()]), null,
298:                            null);
299:                }
300:            }
301:
302:            // reads the first line
303:            private void readHeader() throws IOException {
304:                if (!readHeader) {
305:                    return;
306:                }
307:
308:                String s = in.readLine();
309:
310:                columnNumber = 0;
311:
312:                // get the right index for the column
313:                if (columnName != null) {
314:                    StringTokenizer tok = new StringTokenizer(s);
315:                    while (tok.hasMoreTokens()) {
316:                        String t = tok.nextToken();
317:                        if (t.equals(columnName)) {
318:                            break;
319:                        }
320:                        ++columnNumber;
321:                    }
322:                } else {
323:                    --columnNumber;
324:                }
325:            }
326:
327:            // reads all data into the array lists
328:            private ArrayList<DataTuple> readValues(String filename)
329:                    throws IOException, NumberFormatException {
330:
331:                readHeader();
332:
333:                File file = new File(filename);
334:                int size = (int) (file.length() / 30);
335:
336:                ArrayList<DataTuple> values = new ArrayList<DataTuple>(size);
337:                BufferedReader in = new BufferedReader(new FileReader(filename));
338:                int counter = 0;
339:                while (in.ready()) {
340:                    StringTokenizer tokenizer = new StringTokenizer(in
341:                            .readLine());
342:                    int idx = 0;
343:                    double x = 0;
344:                    double y = 0;
345:                    while (tokenizer.hasMoreTokens()) {
346:                        if (idx == 0) {
347:                            x = Double.parseDouble(tokenizer.nextToken());
348:                        } else if (idx == 1) {
349:                            y = Double.parseDouble(tokenizer.nextToken());
350:                        } else if (idx == columnNumber) {
351:                            values.add(new DataTuple(x, y, Double
352:                                    .parseDouble(tokenizer.nextToken())));
353:                            break;
354:                        } else
355:                            tokenizer.nextToken();
356:                        ++idx;
357:                    }
358:                    if (++counter % 10000 == 0) {
359:                        System.out.print("Read " + counter / 1000
360:                                + " thousand lines.\r");
361:                    }
362:                }
363:                System.out.println();
364:                in.close();
365:
366:                return values;
367:            }
368:
369:            // calculate resolution and bbox
370:            private void preprocessFiles() throws IOException {
371:                double minx = Double.MAX_VALUE;
372:                double miny = Double.MAX_VALUE;
373:                double maxx = Double.MIN_VALUE;
374:                double maxy = Double.MIN_VALUE;
375:                boolean calcResolution = (resolution == 0);
376:                if (imageWidth != 0 && imageHeight != 0) {
377:                    calcResolution = false;
378:                }
379:
380:                if (calcResolution) {
381:                    resolution = Double.MAX_VALUE;
382:                }
383:
384:                for (String filename : inputFilenames) {
385:                    System.out.println("Reading file " + filename);
386:                    ArrayList<DataTuple> values = readValues(filename);
387:
388:                    // Collections.sort( values );
389:
390:                    double[] ys = null;
391:                    DataTuple prev = null;
392:                    double cur;
393:                    if (calcResolution) {
394:                        ys = new double[values.size()];
395:                        prev = values.get(0);
396:                        cur = 0;
397:                    }
398:
399:                    for (int i = 0; i < values.size(); ++i) {
400:                        DataTuple tuple = values.get(i);
401:
402:                        if (maxx < tuple.x) {
403:                            maxx = tuple.x;
404:                        }
405:                        if (maxy < tuple.y) {
406:                            maxy = tuple.y;
407:                        }
408:                        if (minx > tuple.x) {
409:                            minx = tuple.x;
410:                        }
411:                        if (miny > tuple.y) {
412:                            miny = tuple.y;
413:                        }
414:
415:                        if (calcResolution) {
416:                            cur = Math.abs(tuple.x - prev.x);
417:                            if ((cur != 0) && (resolution > cur)) {
418:                                resolution = cur;
419:                            }
420:                            ys[i] = tuple.y;
421:                            prev = tuple;
422:                        }
423:                    }
424:
425:                    if (calcResolution) {
426:                        Arrays.sort(ys);
427:
428:                        for (int i = 0; i < ys.length - 1; ++i) {
429:                            cur = Math.abs(ys[i] - ys[i + 1]);
430:                            if (cur != 0 && cur < resolution) {
431:                                resolution = cur;
432:                            }
433:                        }
434:                    }
435:                }
436:
437:                System.out.println("Covered area:");
438:                System.out.println(minx + " - " + maxx);
439:                System.out.println(miny + " - " + maxy);
440:
441:                boundingBox = GeometryFactory.createEnvelope(minx, miny, maxx,
442:                        maxy, null);
443:
444:                if (!calcResolution && resolution == 0) {
445:                    resolution = Math.abs((maxy - miny) / (imageHeight));
446:                    double h = Math.abs((maxx - minx) / (imageWidth));
447:                    if (h < resolution) {
448:                        resolution = h;
449:                    }
450:                }
451:                if (imageWidth == 0 && imageHeight == 0) {
452:                    imageWidth = (int) (boundingBox.getWidth() / resolution) + 1;
453:                    imageHeight = (int) (boundingBox.getHeight() / resolution) + 1;
454:                }
455:
456:                System.gc();
457:
458:                System.out.println("Resolution: " + resolution);
459:
460:            }
461:
462:            // creates the buffered image with the right size
463:            private void createImage() {
464:
465:                ColorModel ccm;
466:
467:                if (use32Bits) {
468:                    image = new BufferedImage(imageWidth, imageHeight,
469:                            BufferedImage.TYPE_INT_ARGB);
470:                } else {
471:                    ccm = new ComponentColorModel(ColorSpace
472:                            .getInstance(ColorSpace.CS_GRAY), null, false,
473:                            false, BufferedImage.OPAQUE, DataBuffer.TYPE_USHORT);
474:                    WritableRaster wr = ccm.createCompatibleWritableRaster(
475:                            imageWidth, imageHeight);
476:
477:                    image = new BufferedImage(ccm, wr, false,
478:                            new Hashtable<Object, Object>());
479:                }
480:
481:                raster = image.getData();
482:
483:                buffer = raster.getDataBuffer();
484:
485:            }
486:
487:            // calculates the index of the desired position (in regard to a DataBuffer of a Raster)
488:            private int calculatePosition(double x, double y) {
489:
490:                double tmp = (x - boundingBox.getMin().getX()) / resolution;
491:
492:                double ypos = imageHeight
493:                        - ((y - boundingBox.getMin().getY()) / resolution) - 1;
494:                return (int) Math.round(tmp + (ypos * imageWidth));
495:
496:            }
497:
498:            // inserts all values into the image
499:            private void insertValue(double x, double y, double val) {
500:
501:                int pos = Math.abs(calculatePosition(x, y));
502:                if (use32Bits) {
503:                    buffer.setElem(pos, Float.floatToIntBits(((float) val)
504:                            + offset));
505:                } else {
506:                    buffer
507:                            .setElem(pos, (int) Math
508:                                    .round(((val + offset) * 10)));
509:                }
510:
511:            }
512:
513:            // creates the worldfile, depends on minimum values (call after createImage)
514:            private void writeWorldfile() throws IOException {
515:                if (oracle) {
516:                    PrintStream out = new PrintStream(new FileOutputStream(
517:                            outputFilename + ".tfw"));
518:                    out.println(resolution);
519:                    out.println("0.0");
520:                    out.println("0.0");
521:                    out.println(-resolution);
522:                    if (oracle) {
523:                        out.println(boundingBox.getMin().getX() - resolution
524:                                / 2);
525:                    } else {
526:                        out.println(boundingBox.getMin().getX());
527:                    }
528:                    if (oracle) {
529:                        out.println(boundingBox.getMax().getY() + resolution
530:                                / 2);
531:                    } else {
532:                        out.println(boundingBox.getMax().getY());
533:                    }
534:                    out.println();
535:                } else {
536:                    WorldFile wf = new WorldFile(resolution, -resolution, 0, 0,
537:                            boundingBox);
538:                    WorldFile.writeWorldFile(wf, outputFilename);
539:                }
540:            }
541:
542:            private void buildQuadtree() throws IOException, IndexException {
543:                for (String filename : inputFilenames) {
544:                    readHeader();
545:
546:                    BufferedReader in = new BufferedReader(new FileReader(
547:                            filename));
548:                    int counter = 0;
549:                    while (in.ready()) {
550:                        StringTokenizer tokenizer = new StringTokenizer(in
551:                                .readLine());
552:                        int idx = 0;
553:                        double x = 0;
554:                        double y = 0;
555:                        while (tokenizer.hasMoreTokens()) {
556:                            if (idx == 0) {
557:                                x = Double.parseDouble(tokenizer.nextToken());
558:                            } else if (idx == 1) {
559:                                y = Double.parseDouble(tokenizer.nextToken());
560:                            } else if (idx == columnNumber) {
561:                                Point point = GeometryFactory.createPoint(x, y,
562:                                        null);
563:                                quadtree.insert(new DataTuple(x, y, Double
564:                                        .parseDouble(tokenizer.nextToken())),
565:                                        point);
566:                                break;
567:                            } else
568:                                tokenizer.nextToken();
569:                            ++idx;
570:                        }
571:                        if (++counter % 10000 == 0) {
572:                            System.out.print("Read " + counter / 1000
573:                                    + " thousand lines.\r");
574:                        }
575:                    }
576:                    in.close();
577:                    System.out.println();
578:
579:                }
580:
581:            }
582:
583:            /**
584:             * This method executes all steps that are required to transform the text file into a tiff file.
585:             * 
586:             */
587:            private void transform() {
588:                try {
589:                    preprocessFiles();
590:
591:                    quadtree = new MemPointQuadtree(boundingBox);
592:
593:                    buildQuadtree();
594:
595:                    createImage();
596:
597:                    interpolate();
598:
599:                    image.setData(raster);
600:
601:                    System.out.println("Writing output files...");
602:                    ImageUtils.saveImage(image, new File(outputFilename
603:                            + ".tif"), 1);
604:                    writeWorldfile();
605:                    System.out.println("Done.");
606:                    // testOutput();
607:                } catch (IOException ioe) {
608:                    System.out
609:                            .println("Could not read or write a file, reason:");
610:                    ioe.printStackTrace();
611:                    System.exit(0);
612:                } catch (NumberFormatException nfe) {
613:                    System.out
614:                            .println("A number could not be parsed correctly. Reason: ");
615:                    nfe.printStackTrace();
616:                    System.exit(0);
617:                } catch (InterpolationException e) {
618:                    System.out
619:                            .println("Could not interpolate missing values. Reason: ");
620:                    e.printStackTrace();
621:                    System.exit(0);
622:                } catch (IndexException e) {
623:                    System.out.println("Could not build Quadtree. Reason: ");
624:                    e.printStackTrace();
625:                    System.exit(0);
626:                }
627:            }
628:
629:            private void interpolate() throws InterpolationException {
630:
631:                InverseDistanceToPower interpolator = new InverseDistanceToPower(
632:                        quadtree, ignoreValues, interpolateRadiusX,
633:                        interpolateRadiusY, 0, interpolateMinData,
634:                        interpolateMaxData, interpolateNoValue,
635:                        interpolateRadiusIncreaseX, interpolateRadiusIncreaseY,
636:                        interpolatePower);
637:
638:                double minx = boundingBox.getMin().getX();
639:                double miny = boundingBox.getMin().getY();
640:
641:                int count = imageWidth * imageHeight;
642:
643:                int counter = 0;
644:
645:                int interpolatedCounter = 0;
646:
647:                for (int xipos = 0; xipos < imageWidth; ++xipos) {
648:                    for (int yipos = 0; yipos < imageHeight; ++yipos) {
649:                        double xpos = minx + resolution * xipos;
650:                        double ypos = miny + resolution * yipos;
651:
652:                        Envelope env = GeometryFactory.createEnvelope(
653:                                xpos - 0.01, ypos - 0.01, xpos + 0.01,
654:                                ypos + 0.01, null);
655:
656:                        try {
657:                            List<?> list = quadtree.query(env);
658:                            double val = 0;
659:                            if (list.size() == 0) {
660:                                if (interpolate) {
661:                                    val = interpolator.calcInterpolatedValue(
662:                                            xpos, ypos, interpolateRadiusX,
663:                                            interpolateRadiusY);
664:                                    ++interpolatedCounter;
665:                                }
666:                            } else {
667:                                val = ((DataTuple) list.get(0)).value;
668:                            }
669:
670:                            insertValue(xpos, ypos, val);
671:
672:                        } catch (IndexException e) {
673:                            throw new InterpolationException(
674:                                    "Could not interpolate.", e);
675:                        }
676:
677:                        if (++counter % 1000 == 0) {
678:                            System.out.print(counter + "/" + count + "\r");
679:                        }
680:                    }
681:                }
682:
683:                System.out.println(counter + '/' + count + ", interpolated "
684:                        + interpolatedCounter + " values");
685:            }
686:
687:            /**
688:             * Prints out an error message and general usage information of the tool.
689:             * 
690:             * @param error
691:             *            an error message
692:             */
693:            public void printUsage(String error) {
694:                if (error != null) {
695:                    System.out.println("Error: " + error);
696:                    System.out.println();
697:                }
698:                System.out
699:                        .println("java Text2Tiff <options> <inputfile[s]> <outputfile>");
700:                System.out.println("Options:");
701:                System.out.println();
702:                System.out.println("    --help, -help:");
703:                System.out.println("              print this message");
704:                System.out.println("    --read-header, +h:");
705:                System.out.println("    --no-read-header, -h:");
706:                System.out
707:                        .println("              Do/Do not read a header line in the input file. If enabled,");
708:                System.out
709:                        .println("              one can specify column names instead of column numbers as");
710:                System.out.println("              seen below. Default is no.");
711:                System.out.println("    --column-number n, -c n:");
712:                System.out
713:                        .println("              Use the column number n as input column. Must be specified");
714:                System.out
715:                        .println("              if column name below is not given. Counting starts with one,");
716:                System.out
717:                        .println("              so '3' means actually the third column, not the fourth.");
718:                System.out.println("    --column-name n, -cn n:");
719:                System.out
720:                        .println("              Use the column named n as input column. Must be specified");
721:                System.out
722:                        .println("              if no column number is given.");
723:                System.out.println("    --oracle, -o:");
724:                System.out
725:                        .println("              Write the worldfile as Oracle expects it, using the outer");
726:                System.out
727:                        .println("              bounds of the bbox and not the point centers. Default is no.");
728:                System.out.println("    --image-type n:");
729:                System.out
730:                        .println("              n can be either 16 or 32. If n is 16, an image of type USHORT");
731:                System.out
732:                        .println("              will be created, and the values will be stored as shorts,");
733:                System.out
734:                        .println("              multiplied by 10. If n is 32, the float values will be");
735:                System.out
736:                        .println("              stored in an image of type integer, as can be seen in");
737:                System.out
738:                        .println("              Java's Float.floatToIntBits() method. Default is 16.");
739:                System.out.println("    --image-width n:");
740:                System.out.println("    --image-height n:");
741:                System.out
742:                        .println("              If set, an image of this size will be created. If not set");
743:                System.out
744:                        .println("              (default), the size will be determined by the resolution");
745:                System.out
746:                        .println("              either determined automatically or set by hand.");
747:                System.out.println("    --offset n:");
748:                System.out
749:                        .println("              use this as offset value for the result tiff. If result tiff");
750:                System.out
751:                        .println("              shall be a 16Bit tiff first offset will be added to value");
752:                System.out
753:                        .println("              before it will be multiplyed by 10");
754:                System.out.println("    --resolution n, -r n:");
755:                System.out
756:                        .println("              Set geo resolution to n. If omitted, the resolution will be");
757:                System.out
758:                        .println("              set to the smallest value found in the input data.");
759:                System.out.println("    --interpolate, i:");
760:                System.out
761:                        .println("              Interpolate missing values. By default, no interpolation");
762:                System.out.println("              will be performed.");
763:                System.out.println("    --interpolate-power n:");
764:                System.out
765:                        .println("              Interpolate using n as power. Default is "
766:                                + interpolatePower + ".");
767:                System.out.println("    --interpolate-min-data n:");
768:                System.out
769:                        .println("              Interpolate only in the presence of n values within the search");
770:                System.out.println("              radius. Default is "
771:                        + interpolateMinData + ".");
772:                System.out.println("    --interpolate-max-data n:");
773:                System.out
774:                        .println("              Interpolate using a maximum of n values from within the search");
775:                System.out
776:                        .println("              radius. If more values are found, the n nearest will be used.");
777:                System.out.println("              Default is "
778:                        + interpolateMaxData + ".");
779:                System.out.println("    --interpolate-no-value n:");
780:                System.out
781:                        .println("              The value to be used if insufficient data is in the search");
782:                System.out
783:                        .println("              radius. See also the radius-increase options below. Default");
784:                System.out.println("              is " + interpolateNoValue
785:                        + ".");
786:                System.out.println("    --interpolate-radius-x n:");
787:                System.out
788:                        .println("              Interpolate using a search radius of n in the x direction.");
789:                System.out.println("              Default is "
790:                        + interpolateRadiusX + ".");
791:                System.out.println("    --interpolate-radius-y n:");
792:                System.out
793:                        .println("              Interpolate using a search radius of n in the y direction.");
794:                System.out.println("              Default is "
795:                        + interpolateRadiusY + ".");
796:                System.out.println("    --interpolate-radius-increase-x n:");
797:                System.out
798:                        .println("              Automatically increase the x search radius by n if less than");
799:                System.out
800:                        .println("              --i-min-data values are found. If specified and not 0, the");
801:                System.out
802:                        .println("              value --i-no-value will be ignored. Default is 0.");
803:                System.out.println("    --interpolate-radius-increase-y n:");
804:                System.out
805:                        .println("              Automatically increase the y search radius by n if less than");
806:                System.out
807:                        .println("              --i-min-data values are found. If specified and not 0, the");
808:                System.out
809:                        .println("              value --i-no-value will be ignored. Default is 0.");
810:                System.out.println("    --interpolate-ignore-range min max:");
811:                System.out
812:                        .println("              Adds a new range of values to be ignored while interpolating.");
813:                System.out.println();
814:                System.out
815:                        .println(".tif/.tfw will be appended to the <outputfile> parameter.");
816:                System.out.println();
817:
818:                if (error == null) {
819:                    System.exit(0);
820:                } else {
821:                    System.exit(1);
822:                }
823:            }
824:
825:            /**
826:             * This method is used from the command line.
827:             * 
828:             * @param args
829:             *            the command line arguments.
830:             */
831:            public static void main(String[] args) {
832:                new Text2Tiff(args).transform();
833:            }
834:
835:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.