comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/TIMParser.java @ 4731:7d9fcd322c45

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

http://dive4elements.wald.intevation.org