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

Last change on this file since 99 was 99, checked in by imi, 19 years ago
  • added GeoImage feature (showing images on a tracklog)
  • added zoom slider
  • added Escape cancels selection rectangle
  • added "Save password" option to Auth-dialog
  • fixed that redo/undo buttons were not enabled
  • fixed hotkeys beeing inaccessible when no data is loaded
File size: 2.8 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.RawGpsLayer.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 String token = st.nextToken();
52 if (!token.equals("lat") && !token.equals("lon") && !token.equals("time"))
53 token = "ignore";
54 format.add(token);
55 }
56
57 // test for completness
58 if (!format.contains("lat") || !format.contains("lon")) {
59 if (Main.pref.get("csv.importstring").equals(""))
60 throw new JDOMException("Format string in data is incomplete or not found. Try setting an manual format string in Preferences.");
61 throw new JDOMException("Format string is incomplete. Need at least 'lat' and 'lon' specification");
62 }
63
64 int lineNo = 0;
65 try {
66 for (String line = in.readLine(); line != null; line = in.readLine()) {
67 lineNo++;
68 StringTokenizer st = new StringTokenizer(line, delim);
69 double lat = 0, lon = 0;
70 String time = null;
71 for (String token : format) {
72 if (token.equals("lat"))
73 lat = Double.parseDouble(st.nextToken());
74 else if (token.equals("lon"))
75 lon = Double.parseDouble(st.nextToken());
76 else if (token.equals("time"))
77 time = (time == null?"":(time+" ")) + st.nextToken();
78 else if (token.equals("ignore"))
79 st.nextToken();
80 else
81 throw new JDOMException("Unknown data type: '"+token+"'."+(Main.pref.get("csv.importstring").equals("") ? " Maybe add an format string in preferences." : ""));
82 }
83 data.add(new GpsPoint(new LatLon(lat, lon), time));
84 }
85 } catch (RuntimeException e) {
86 throw new JDOMException("Parsing error in line "+lineNo, e);
87 }
88 return data;
89 }
90}
Note: See TracBrowser for help on using the repository browser.