comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/tim/TIMParser.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/TIMParser.java@9c1964fe3995
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
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 import org.apache.log4j.Logger;
15
16 import de.intevation.flys.utils.EpsilonComparator;
17
18 /** Parser for single .tim files. */
19 public class TIMParser
20 {
21 /** Private logger. */
22 private static Logger logger = Logger.getLogger(TIMParser.class);
23
24 /** Proper encoding. */
25 public static final String ENCODING =
26 System.getProperty("tim.encoding", "ISO-8859-1");
27
28 /** Map of stations (km) to points (xyz). */
29 protected Map<Double, List<Coordinate>> lines;
30
31 public TIMParser() {
32 lines = new TreeMap<Double, List<Coordinate>>(EpsilonComparator.CMP);
33 }
34
35 /** Access map of stations (km) to coordinates (xyz). */
36 public Map<Double, List<Coordinate>> getLines() {
37 return lines;
38 }
39
40 /** Get number of lines (data). */
41 public int numLines() {
42 return lines.size();
43 }
44
45 /** Parse single .tim file. */
46 public void load(String filename) throws IOException {
47 BufferedReader reader =
48 new BufferedReader(
49 new InputStreamReader(
50 new FileInputStream(filename), ENCODING));
51 try {
52 String row;
53 while ((row = reader.readLine()) != null) {
54 if (row.length() < 54) {
55 logger.warn("row too short");
56 continue;
57 }
58 double station, x, y, z;
59 try {
60 station = Double.parseDouble(row.substring( 9, 16))/1000d;
61 x = Double.parseDouble(row.substring(20, 30))/1000d;
62 y = Double.parseDouble(row.substring(30, 40))/1000d;
63 z = Double.parseDouble(row.substring(47, 54))/10000d;
64 } catch (NumberFormatException nfe) {
65 logger.warn("Invalid row");
66 continue;
67 }
68
69 Double km = station;
70
71 List<Coordinate> line = lines.get(km);
72 if (line == null) {
73 line = new ArrayList<Coordinate>();
74 lines.put(km, line);
75 }
76
77 line.add(new Coordinate(x, y, z));
78 }
79 // Bring coords in lexicographical order.
80 sortCoordinates();
81 } finally {
82 reader.close();
83 }
84 }
85
86 /** Sort coordinates of lines lexicographically. */
87 protected void sortCoordinates() {
88 for (List<Coordinate> line: lines.values()) {
89 Collections.sort(line, LexiComparator.CMP);
90 }
91 }
92 }
93 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org