source: josm/src/org/openstreetmap/josm/io/RawCsvReader.java@ 98

Last change on this file since 98 was 93, checked in by imi, 19 years ago
  • added "insert node into line segment" mapmode
  • added direction hint to line segments
File size: 2.7 KB
Line 
1package org.openstreetmap.josm.io;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.Reader;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.LinkedList;
9import java.util.StringTokenizer;
10
11import org.jdom.JDOMException;
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.gui.layer.RawGpsDataLayer.GpsPoint;
15
16/**
17 * Read raw information from a csv style file (as defined in the preferences).
18 * @author imi
19 */
20public class RawCsvReader {
21
22 /**
23 * Reader to read the input from.
24 */
25 private BufferedReader in;
26
27 public RawCsvReader(Reader in) {
28 this.in = new BufferedReader(in);
29 }
30
31 public Collection<GpsPoint> parse() throws JDOMException, IOException {
32 Collection<GpsPoint> data = new LinkedList<GpsPoint>();
33 String formatStr = Main.pref.get("csv.importstring");
34 if (formatStr == null)
35 formatStr = in.readLine();
36 if (formatStr == null)
37 throw new JDOMException("Could not detect data format string.");
38
39 // get delimiter
40 String delim = ",";
41 for (int i = 0; i < formatStr.length(); ++i) {
42 if (!Character.isLetterOrDigit(formatStr.charAt(i))) {
43 delim = ""+formatStr.charAt(i);
44 break;
45 }
46 }
47
48 // convert format string
49 ArrayList<String> format = new ArrayList<String>();
50 for (StringTokenizer st = new StringTokenizer(formatStr, delim); st.hasMoreTokens();)
51 format.add(st.nextToken());
52
53 // test for completness
54 if (!format.contains("lat") || !format.contains("lon")) {
55 if (Main.pref.get("csv.importstring").equals(""))
56 throw new JDOMException("Format string in data is incomplete or not found. Try setting an manual format string in Preferences.");
57 throw new JDOMException("Format string is incomplete. Need at least 'lat' and 'lon' specification");
58 }
59
60 int lineNo = 0;
61 try {
62 for (String line = in.readLine(); line != null; line = in.readLine()) {
63 lineNo++;
64 StringTokenizer st = new StringTokenizer(line, delim);
65 double lat = 0, lon = 0;
66 String time = null;
67 for (String token : format) {
68 if (token.equals("lat"))
69 lat = Double.parseDouble(st.nextToken());
70 else if (token.equals("lon"))
71 lon = Double.parseDouble(st.nextToken());
72 else if (token.equals("time"))
73 time = (time == null?"":(time+" ")) + st.nextToken();
74 else if (token.equals("ignore"))
75 st.nextToken();
76 else
77 throw new JDOMException("Unknown data type: '"+token+"'."+(Main.pref.get("csv.importstring").equals("") ? " Maybe add an format string in preferences." : ""));
78 }
79 data.add(new GpsPoint(new LatLon(lat, lon), time));
80 }
81 } catch (RuntimeException e) {
82 throw new JDOMException("Parsing error in line "+lineNo, e);
83 }
84 return data;
85 }
86}
Note: See TracBrowser for help on using the repository browser.