| [38] | 1 | package org.openstreetmap.josm.io;
|
|---|
| 2 |
|
|---|
| 3 | import java.io.BufferedReader;
|
|---|
| 4 | import java.io.IOException;
|
|---|
| 5 | import java.io.Reader;
|
|---|
| 6 | import java.util.ArrayList;
|
|---|
| 7 | import java.util.Collection;
|
|---|
| 8 | import java.util.LinkedList;
|
|---|
| 9 | import java.util.StringTokenizer;
|
|---|
| 10 |
|
|---|
| 11 | import org.jdom.JDOMException;
|
|---|
| 12 | import org.openstreetmap.josm.Main;
|
|---|
| [71] | 13 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| [99] | 14 | import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint;
|
|---|
| [38] | 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Read raw information from a csv style file (as defined in the preferences).
|
|---|
| 18 | * @author imi
|
|---|
| 19 | */
|
|---|
| 20 | public 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 | }
|
|---|
| [99] | 30 |
|
|---|
| [78] | 31 | public Collection<GpsPoint> parse() throws JDOMException, IOException {
|
|---|
| 32 | Collection<GpsPoint> data = new LinkedList<GpsPoint>();
|
|---|
| [93] | 33 | String formatStr = Main.pref.get("csv.importstring");
|
|---|
| [38] | 34 | if (formatStr == null)
|
|---|
| 35 | formatStr = in.readLine();
|
|---|
| 36 | if (formatStr == null)
|
|---|
| 37 | throw new JDOMException("Could not detect data format string.");
|
|---|
| [99] | 38 |
|
|---|
| [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 | }
|
|---|
| [99] | 47 |
|
|---|
| [38] | 48 | // convert format string
|
|---|
| 49 | ArrayList<String> format = new ArrayList<String>();
|
|---|
| [99] | 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 | }
|
|---|
| [38] | 56 |
|
|---|
| 57 | // test for completness
|
|---|
| 58 | if (!format.contains("lat") || !format.contains("lon")) {
|
|---|
| [93] | 59 | if (Main.pref.get("csv.importstring").equals(""))
|
|---|
| [74] | 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");
|
|---|
| [38] | 62 | }
|
|---|
| [99] | 63 |
|
|---|
| [38] | 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);
|
|---|
| [71] | 69 | double lat = 0, lon = 0;
|
|---|
| [78] | 70 | String time = null;
|
|---|
| [38] | 71 | for (String token : format) {
|
|---|
| 72 | if (token.equals("lat"))
|
|---|
| [71] | 73 | lat = Double.parseDouble(st.nextToken());
|
|---|
| [38] | 74 | else if (token.equals("lon"))
|
|---|
| [71] | 75 | lon = Double.parseDouble(st.nextToken());
|
|---|
| [78] | 76 | else if (token.equals("time"))
|
|---|
| 77 | time = (time == null?"":(time+" ")) + st.nextToken();
|
|---|
| [38] | 78 | else if (token.equals("ignore"))
|
|---|
| 79 | st.nextToken();
|
|---|
| 80 | else
|
|---|
| [93] | 81 | throw new JDOMException("Unknown data type: '"+token+"'."+(Main.pref.get("csv.importstring").equals("") ? " Maybe add an format string in preferences." : ""));
|
|---|
| [38] | 82 | }
|
|---|
| [78] | 83 | data.add(new GpsPoint(new LatLon(lat, lon), time));
|
|---|
| [38] | 84 | }
|
|---|
| 85 | } catch (RuntimeException e) {
|
|---|
| 86 | throw new JDOMException("Parsing error in line "+lineNo, e);
|
|---|
| 87 | }
|
|---|
| 88 | return data;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|