# HG changeset patch # User Sascha Teichmann # Date 1356703003 -3600 # Node ID 7d9fcd322c45c7487e42f2d2a68457b82bf4b6ad # Parent 1aca30035932ddd08f92b0a88a1c452b2c49d871 Initial version of TIMParser, comments and TODOs by Felix Wolfsteller, Code by Sascha Teichmann. diff -r 1aca30035932 -r 7d9fcd322c45 flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/TIMParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/TIMParser.java Fri Dec 28 14:56:43 2012 +0100 @@ -0,0 +1,88 @@ +package de.intevation.flys.importer.parsers.tim; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + + +/** Parser for single .tim files. */ +// TODO switch to proper logging. +public class TIMParser +{ + /** Proper encoding. */ + public static final String ENCODING = + System.getProperty("tim.encoding", "ISO-8859-1"); + + /** Map of stations (km) to points (xyz). */ + protected Map> lines; + + public TIMParser() { + lines = new TreeMap>(EpsilonComparator.CMP); + } + + /** Access map of stations (km) to coordinates (xyz). */ + public Map> getLines() { + return lines; + } + + /** Get number of lines (data). */ + public int numLines() { + return lines.size(); + } + + /** Parse single .tim file. */ + public void load(String filename) throws IOException { + BufferedReader reader = + new BufferedReader( + new InputStreamReader( + new FileInputStream(filename), ENCODING)); + try { + String row; + while ((row = reader.readLine()) != null) { + if (row.length() < 54) { + System.err.println("row too short"); + continue; + } + double station, x, y, z; + try { + station = Double.parseDouble(row.substring( 9, 16))/1000d; + x = Double.parseDouble(row.substring(20, 30))/1000d; + y = Double.parseDouble(row.substring(30, 40))/1000d; + z = Double.parseDouble(row.substring(47, 54))/10000d; + } catch (NumberFormatException nfe) { + System.err.println("Invalid row"); + continue; + } + + Double km = station; + + List line = lines.get(km); + if (line == null) { + line = new ArrayList(); + lines.put(km, line); + } + + line.add(new Coordinate(x, y, z)); + } + // Bring coords in lexicographical order. + sortCoordinates(); + } finally { + reader.close(); + } + } + + /** Sort coordinates of lines lexicographically. */ + protected void sortCoordinates() { + for (List line: lines.values()) { + Collections.sort(line, LexiComparator.CMP); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :