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

Last change on this file since 12617 was 12421, checked in by Don-vip, 7 years ago

see #14924 - improve NMEA documentation thanks to gpsd (http://www.catb.org/gpsd/NMEA.html) + add support for NMEA sentences coming from GLONASS, Galileo or Beidu receivers

  • Property svn:eol-style set to native
File size: 4.4 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.IOException;
8import java.io.InputStream;
9
10import javax.swing.JOptionPane;
11import javax.swing.SwingUtilities;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.ExtensionFileFilter;
15import org.openstreetmap.josm.gui.HelpAwareOptionPane;
16import org.openstreetmap.josm.gui.Notification;
17import org.openstreetmap.josm.gui.layer.GpxLayer;
18import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
20import org.openstreetmap.josm.gui.util.GuiHelper;
21import org.openstreetmap.josm.io.GpxImporter.GpxImporterData;
22import org.openstreetmap.josm.io.nmea.NmeaReader;
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 = ExtensionFileFilter.newFilterWithArchiveExtensions(
34 "nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files"), false);
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 = Compression.getUncompressedFileInputStream(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(() -> {
54 Main.getLayerManager().addLayer(gpxLayer);
55 if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
56 MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), fileFinal, gpxLayer);
57 if (!ml.data.isEmpty()) {
58 Main.getLayerManager().addLayer(ml);
59 }
60 }
61 });
62 }
63 showNmeaInfobox(r.getNumberOfCoordinates() > 0, r);
64 }
65 }
66
67 private static void showNmeaInfobox(boolean success, NmeaReader r) {
68 final StringBuilder msg = new StringBuilder(160).append("<html>")
69 .append(tr("Coordinates imported: {0}", r.getNumberOfCoordinates())).append("<br>")
70 .append(tr("Malformed sentences: {0}", r.getParserMalformed())).append("<br>")
71 .append(tr("Checksum errors: {0}", r.getParserChecksumErrors())).append("<br>");
72 if (!success) {
73 msg.append(tr("Unknown sentences: {0}", r.getParserUnknown())).append("<br>");
74 }
75 msg.append(tr("Zero coordinates: {0}", r.getParserZeroCoordinates()))
76 .append("</html>");
77 if (success) {
78 SwingUtilities.invokeLater(() -> new Notification(
79 "<h3>" + tr("NMEA import success:") + "</h3>" + msg.toString())
80 .setIcon(JOptionPane.INFORMATION_MESSAGE)
81 .show());
82 } else {
83 HelpAwareOptionPane.showMessageDialogInEDT(
84 Main.parent,
85 msg.toString(),
86 tr("NMEA import failure!"),
87 JOptionPane.ERROR_MESSAGE, null);
88 }
89 }
90
91 /**
92 * Replies the new GPX and marker layers corresponding to the specified NMEA file.
93 * @param is input stream to NMEA 0183 data
94 * @param associatedFile NMEA file
95 * @param gpxLayerName The GPX layer name
96 * @param markerLayerName The marker layer name
97 * @return the new GPX and marker layers corresponding to the specified NMEA file
98 * @throws IOException if an I/O error occurs
99 */
100 public static GpxImporterData loadLayers(InputStream is, final File associatedFile,
101 final String gpxLayerName, String markerLayerName) throws IOException {
102 final NmeaReader r = new NmeaReader(is);
103 final boolean parsedProperly = r.getNumberOfCoordinates() > 0;
104 r.data.storageFile = associatedFile;
105 return GpxImporter.loadLayers(r.data, parsedProperly, gpxLayerName, markerLayerName);
106 }
107}
Note: See TracBrowser for help on using the repository browser.