source: josm/trunk/src/org/openstreetmap/josm/io/NMEAImporter.java@ 8207

Last change on this file since 8207 was 7326, checked in by Don-vip, 10 years ago

fix #10292 - allow to load a session with NMEA file + enhance reading/writing unit tests for sessions

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.IOException;
9import java.io.InputStream;
10
11import javax.swing.JOptionPane;
12import javax.swing.SwingUtilities;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.ExtensionFileFilter;
16import org.openstreetmap.josm.gui.HelpAwareOptionPane;
17import org.openstreetmap.josm.gui.Notification;
18import org.openstreetmap.josm.gui.layer.GpxLayer;
19import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
21import org.openstreetmap.josm.gui.util.GuiHelper;
22import org.openstreetmap.josm.io.GpxImporter.GpxImporterData;
23
24/**
25 * File importer allowing to import NMEA-0183 files (*.nmea/nme/nma/log/txt files).
26 * @since 1637
27 */
28public class NMEAImporter extends FileImporter {
29
30 /**
31 * The NMEA file filter (*.nmea *.nme *.nma *.log *.txt files).
32 */
33 public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
34 "nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files") + " (*.nmea *.nme *.nma *.log *.txt)");
35
36 /**
37 * Constructs a new {@code NMEAImporter}.
38 */
39 public NMEAImporter() {
40 super(FILE_FILTER);
41 }
42
43 @Override
44 public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
45 final String fn = file.getName();
46 try (InputStream fis = new FileInputStream(file)) {
47 final NmeaReader r = new NmeaReader(fis);
48 if (r.getNumberOfCoordinates() > 0) {
49 r.data.storageFile = file;
50 final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
51 final File fileFinal = file;
52
53 GuiHelper.runInEDT(new Runnable() {
54 @Override
55 public void run() {
56 Main.main.addLayer(gpxLayer);
57 if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
58 MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), fileFinal, gpxLayer);
59 if (!ml.data.isEmpty()) {
60 Main.main.addLayer(ml);
61 }
62 }
63 }
64 });
65 }
66 showNmeaInfobox(r.getNumberOfCoordinates() > 0, r);
67 }
68 }
69
70 private void showNmeaInfobox(boolean success, NmeaReader r) {
71 final StringBuilder msg = new StringBuilder().append("<html>");
72 msg.append(tr("Coordinates imported: {0}", r.getNumberOfCoordinates()) + "<br>");
73 msg.append(tr("Malformed sentences: {0}", r.getParserMalformed()) + "<br>");
74 msg.append(tr("Checksum errors: {0}", r.getParserChecksumErrors()) + "<br>");
75 if (!success) {
76 msg.append(tr("Unknown sentences: {0}", r.getParserUnknown()) + "<br>");
77 }
78 msg.append(tr("Zero coordinates: {0}", r.getParserZeroCoordinates()));
79 msg.append("</html>");
80 if (success) {
81 SwingUtilities.invokeLater(new Runnable() {
82 @Override
83 public void run() {
84 new Notification(
85 "<h3>" + tr("NMEA import success:") + "</h3>" + msg.toString())
86 .setIcon(JOptionPane.INFORMATION_MESSAGE)
87 .show();
88 }
89 });
90 } else {
91 HelpAwareOptionPane.showMessageDialogInEDT(
92 Main.parent,
93 msg.toString(),
94 tr("NMEA import failure!"),
95 JOptionPane.ERROR_MESSAGE, null);
96 }
97 }
98
99 public static GpxImporterData loadLayers(InputStream is, final File associatedFile,
100 final String gpxLayerName, String markerLayerName) throws IOException {
101 final NmeaReader r = new NmeaReader(is);
102 final boolean parsedProperly = r.getNumberOfCoordinates() > 0;
103 r.data.storageFile = associatedFile;
104 return GpxImporter.loadLayers(r.data, parsedProperly, gpxLayerName, markerLayerName);
105 }
106}
Note: See TracBrowser for help on using the repository browser.