annotate flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/TIMParser.java @ 4786:9a00be1acaee

TIMParser: Switch to log4j instead of System.err .
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 08 Jan 2013 12:41:48 +0100
parents 94b39073f0f7
children 9c1964fe3995
rev   line source
4731
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.importer.parsers.tim;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import java.io.BufferedReader;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import java.io.FileInputStream;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import java.io.IOException;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import java.io.InputStreamReader;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 import java.util.ArrayList;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import java.util.Collections;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 import java.util.List;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import java.util.Map;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import java.util.TreeMap;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
14 import org.apache.log4j.Logger;
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
15
4735
94b39073f0f7 Moved EpsilonComparator to utils package becaus its not only useful for TIM parsing. Used EpsilonComparator in DA60 and DA66 case. Maybe TODO: Use in PRF, too.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4731
diff changeset
16 import de.intevation.flys.utils.EpsilonComparator;
4731
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 /** Parser for single .tim files. */
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 // TODO switch to proper logging.
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 public class TIMParser
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 {
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
22 /** Private logger. */
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
23 private static Logger logger = Logger.getLogger(TIMParser.class);
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
24
4731
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 /** Proper encoding. */
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 public static final String ENCODING =
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 System.getProperty("tim.encoding", "ISO-8859-1");
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 /** Map of stations (km) to points (xyz). */
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 protected Map<Double, List<Coordinate>> lines;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 public TIMParser() {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 lines = new TreeMap<Double, List<Coordinate>>(EpsilonComparator.CMP);
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 /** Access map of stations (km) to coordinates (xyz). */
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 public Map<Double, List<Coordinate>> getLines() {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 return lines;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 /** Get number of lines (data). */
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 public int numLines() {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 return lines.size();
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 /** Parse single .tim file. */
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 public void load(String filename) throws IOException {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 BufferedReader reader =
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 new BufferedReader(
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 new InputStreamReader(
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 new FileInputStream(filename), ENCODING));
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 try {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 String row;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 while ((row = reader.readLine()) != null) {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 if (row.length() < 54) {
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
56 logger.warn("row too short");
4731
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 continue;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 double station, x, y, z;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 try {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 station = Double.parseDouble(row.substring( 9, 16))/1000d;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 x = Double.parseDouble(row.substring(20, 30))/1000d;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 y = Double.parseDouble(row.substring(30, 40))/1000d;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 z = Double.parseDouble(row.substring(47, 54))/10000d;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 } catch (NumberFormatException nfe) {
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
66 logger.warn("Invalid row");
4731
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 continue;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 Double km = station;
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 List<Coordinate> line = lines.get(km);
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 if (line == null) {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 line = new ArrayList<Coordinate>();
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 lines.put(km, line);
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 line.add(new Coordinate(x, y, z));
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 // Bring coords in lexicographical order.
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 sortCoordinates();
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 } finally {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 reader.close();
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 /** Sort coordinates of lines lexicographically. */
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 protected void sortCoordinates() {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 for (List<Coordinate> line: lines.values()) {
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 Collections.sort(line, LexiComparator.CMP);
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org