1 | /**
|
---|
2 | *
|
---|
3 | */
|
---|
4 | package org.openstreetmap.josm.io;
|
---|
5 |
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 |
|
---|
8 | import java.io.BufferedReader;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.FileInputStream;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStream;
|
---|
13 | import java.io.InputStreamReader;
|
---|
14 | import java.util.ArrayList;
|
---|
15 |
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
22 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
23 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
24 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
25 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * @author dmuecke Data import for TangoGPS file format.
|
---|
29 | */
|
---|
30 | public class TangoGPS extends FileImporter {
|
---|
31 |
|
---|
32 | public TangoGPS() {
|
---|
33 | super(new ExtensionFileFilter("log", "log",tr("TangoGPS Files (*.log)")));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @author cbrill
|
---|
38 | * This function imports data from file and adds trackpoints
|
---|
39 | * to a layer.
|
---|
40 | * Read a log file from TangoGPS. These are simple text files in the
|
---|
41 | * form: <lat>,<lon>,<elevation>,<speed>,<course>,<hdop>,<datetime>
|
---|
42 | */
|
---|
43 | @Override
|
---|
44 | public void importData(File file) throws IOException {
|
---|
45 | // create the data tree
|
---|
46 | GpxData data = new GpxData();
|
---|
47 | GpxTrack currentTrack = new GpxTrack();
|
---|
48 | data.tracks.add(currentTrack);
|
---|
49 | ArrayList<WayPoint> currentTrackSeg = new ArrayList<WayPoint>();
|
---|
50 |
|
---|
51 | int imported = 0;
|
---|
52 | int failure = 0;
|
---|
53 |
|
---|
54 | BufferedReader rd = null;
|
---|
55 | try {
|
---|
56 | InputStream source = new FileInputStream(file);
|
---|
57 | rd = new BufferedReader(new InputStreamReader(source));
|
---|
58 |
|
---|
59 | String line;
|
---|
60 | while ((line = rd.readLine()) != null) {
|
---|
61 | failure++;
|
---|
62 | String[] lineElements = line.split(",");
|
---|
63 | if (lineElements.length == 7) {
|
---|
64 | try {
|
---|
65 | WayPoint currentWayPoint = new WayPoint(
|
---|
66 | parseLatLon(lineElements));
|
---|
67 | currentWayPoint.attr.put("ele", lineElements[2]);
|
---|
68 | currentWayPoint.attr.put("time", lineElements[6]);
|
---|
69 | currentWayPoint.setTime();
|
---|
70 | currentTrackSeg.add(currentWayPoint);
|
---|
71 | imported++;
|
---|
72 | } catch (NumberFormatException e) {
|
---|
73 | e.printStackTrace();
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | failure = failure - imported;
|
---|
78 | if(imported > 0) {
|
---|
79 | currentTrack.trackSegs.add(currentTrackSeg);
|
---|
80 | data.recalculateBounds();
|
---|
81 | data.storageFile = file;
|
---|
82 | GpxLayer gpxLayer = new GpxLayer(data, file.getName());
|
---|
83 | Main.main.addLayer(gpxLayer);
|
---|
84 | if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
---|
85 | MarkerLayer ml = new MarkerLayer(data, tr("Markers from {0}", file.getName()), file, gpxLayer);
|
---|
86 | if (ml.data.size() > 0) {
|
---|
87 | Main.main.addLayer(ml);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | showInfobox(imported,failure);
|
---|
92 | } finally {
|
---|
93 | if (rd != null)
|
---|
94 | rd.close();
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private double parseCoord(String s) {
|
---|
99 | return Double.parseDouble(s);
|
---|
100 | }
|
---|
101 |
|
---|
102 | private LatLon parseLatLon(String[] lineElements) {
|
---|
103 | if (lineElements.length < 2)
|
---|
104 | return null;
|
---|
105 | return new LatLon(parseCoord(lineElements[0]),
|
---|
106 | parseCoord(lineElements[1]));
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void showInfobox(int success,int failure) {
|
---|
110 | String msg = tr("Coordinates imported: ") + success + " " + tr("Format errors: ") + failure + "\n";
|
---|
111 | if (success > 0) {
|
---|
112 | JOptionPane.showMessageDialog(Main.parent, msg, tr("TangoGPS import success"), JOptionPane.INFORMATION_MESSAGE);
|
---|
113 | } else {
|
---|
114 | JOptionPane.showMessageDialog(Main.parent, msg, tr("TangoGPS import faliure!"), JOptionPane.ERROR_MESSAGE);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | }
|
---|