annotate backend/src/main/java/org/dive4elements/river/importer/parsers/tim/TIMParser.java @ 8187:3bb1c62ad732

Moved package org.dive4elements.river.utils to org.dive4elements.river.backend.utils.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 04 Sep 2014 15:03:25 +0200
parents 4c3ccf2b0304
children
rev   line source
5844
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
2 * Software engineering by Intevation GmbH
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
3 *
5992
4c3ccf2b0304 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5844
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
5844
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
5992
4c3ccf2b0304 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5844
diff changeset
6 * documentation coming with Dive4Elements River for details.
5844
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
7 */
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
8
5829
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
9 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
10
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.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
12 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
13 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
14 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
15
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 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
17 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
18 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
19 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
20 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
21
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
22 import org.apache.log4j.Logger;
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
23
8187
3bb1c62ad732 Moved package org.dive4elements.river.utils to org.dive4elements.river.backend.utils.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
24 import org.dive4elements.river.backend.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
25
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 /** 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
27 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
28 {
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
29 /** Private logger. */
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
30 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
31
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
32 /** 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
33 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
34 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
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 /** 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
37 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
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 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
40 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
41 }
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
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 /** 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
44 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
45 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
46 }
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
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 /** 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
49 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
50 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
51 }
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
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 /** 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
54 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
55 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
56 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
57 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
58 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
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 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
61 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
62 if (row.length() < 54) {
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
63 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
64 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
65 }
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 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
67 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
68 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
69 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
70 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
71 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
72 } catch (NumberFormatException nfe) {
4786
9a00be1acaee TIMParser: Switch to log4j instead of System.err .
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 4735
diff changeset
73 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
74 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
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 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
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 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
80 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
81 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
82 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
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 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
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 // 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
88 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 } 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
90 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
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 /** 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
95 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
96 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
97 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
98 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 }
7d9fcd322c45 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann.
Sascha Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org