| 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;
|
|---|
| 13 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 14 | import 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 | */
|
|---|
| 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 | }
|
|---|
| 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 | }
|
|---|