annotate flys-backend/src/main/java/org/dive4elements/river/importer/parsers/tim/TIMParser.java @ 5829:18619c1e7c2a

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:54:36 +0200
parents dfb26b03b179
children
rev   line source
5829
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
1 package org.dive4elements.river.importer.parsers.tim;
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
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
5829
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
16 import org.dive4elements.river.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 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
20 {
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
21 /** Private logger. */
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
22 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
23
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
24 /** 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
25 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
26 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
27
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 /** 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
29 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
30
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 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
32 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
33 }
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 /** 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
36 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
37 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
38 }
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 /** 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
41 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
42 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
43 }
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 /** 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
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 if (row.length() < 54) {
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
55 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
56 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
57 }
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 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
59 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
60 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
61 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
62 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
63 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
64 } catch (NumberFormatException nfe) {
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
65 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
66 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
67 }
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 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
70
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 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
72 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
73 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
74 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
75 }
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 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
78 }
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 // 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
80 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
81 } 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
82 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
83 }
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 /** 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
87 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
88 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
89 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
90 }
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 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org