source: osm/applications/editors/josm/plugins/dataimport/src/org/openstreetmap/josm/io/TangoGPS.java@ 16176

Last change on this file since 16176 was 16176, checked in by stoecker, 15 years ago

added dataimport plugin

File size: 3.5 KB
Line 
1/**
2 *
3 */
4package org.openstreetmap.josm.io;
5
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.io.BufferedReader;
9import java.io.File;
10import java.io.FileInputStream;
11import java.io.IOException;
12import java.io.InputStream;
13import java.io.InputStreamReader;
14import java.util.ArrayList;
15
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.actions.ExtensionFileFilter;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.data.gpx.GpxData;
22import org.openstreetmap.josm.data.gpx.GpxTrack;
23import org.openstreetmap.josm.data.gpx.WayPoint;
24import org.openstreetmap.josm.gui.layer.GpxLayer;
25import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
26
27/**
28 * @author dmuecke Data import for TangoGPS file format.
29 */
30public 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}
Note: See TracBrowser for help on using the repository browser.