1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io.importexport;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 | import javax.swing.SwingUtilities;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
15 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
16 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
17 | import org.openstreetmap.josm.gui.Notification;
|
---|
18 | import org.openstreetmap.josm.gui.io.importexport.GpxImporter.GpxImporterData;
|
---|
19 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
21 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
22 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
23 | import org.openstreetmap.josm.io.Compression;
|
---|
24 | import org.openstreetmap.josm.io.nmea.NmeaReader;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * File importer allowing to import NMEA-0183 files (*.nmea/nme/nma/log/txt files).
|
---|
28 | * @since 1637
|
---|
29 | */
|
---|
30 | public class NMEAImporter extends FileImporter {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * The NMEA file filter (*.nmea *.nme *.nma *.log *.txt files).
|
---|
34 | */
|
---|
35 | public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
|
---|
36 | "nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files"), false);
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Constructs a new {@code NMEAImporter}.
|
---|
40 | */
|
---|
41 | public NMEAImporter() {
|
---|
42 | super(FILE_FILTER);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
|
---|
47 | final String fn = file.getName();
|
---|
48 | try (InputStream fis = Compression.getUncompressedFileInputStream(file)) {
|
---|
49 | final NmeaReader r = new NmeaReader(fis);
|
---|
50 | if (r.getNumberOfCoordinates() > 0) {
|
---|
51 | r.data.storageFile = file;
|
---|
52 | final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
|
---|
53 | final File fileFinal = file;
|
---|
54 |
|
---|
55 | GuiHelper.runInEDT(() -> {
|
---|
56 | MainApplication.getLayerManager().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 | MainApplication.getLayerManager().addLayer(ml);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | });
|
---|
64 | }
|
---|
65 | showNmeaInfobox(r.getNumberOfCoordinates() > 0, r);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private static void showNmeaInfobox(boolean success, NmeaReader r) {
|
---|
70 | final StringBuilder msg = new StringBuilder(160).append("<html>")
|
---|
71 | .append(tr("Coordinates imported: {0}", r.getNumberOfCoordinates())).append("<br>")
|
---|
72 | .append(tr("Malformed sentences: {0}", r.getParserMalformed())).append("<br>")
|
---|
73 | .append(tr("Checksum errors: {0}", r.getParserChecksumErrors())).append("<br>");
|
---|
74 | if (!success) {
|
---|
75 | msg.append(tr("Unknown sentences: {0}", r.getParserUnknown())).append("<br>");
|
---|
76 | }
|
---|
77 | msg.append(tr("Zero coordinates: {0}", r.getParserZeroCoordinates()))
|
---|
78 | .append("</html>");
|
---|
79 | if (success) {
|
---|
80 | SwingUtilities.invokeLater(() -> new Notification(
|
---|
81 | "<h3>" + tr("NMEA import success:") + "</h3>" + msg.toString())
|
---|
82 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
---|
83 | .show());
|
---|
84 | } else {
|
---|
85 | HelpAwareOptionPane.showMessageDialogInEDT(
|
---|
86 | Main.parent,
|
---|
87 | msg.toString(),
|
---|
88 | tr("NMEA import failure!"),
|
---|
89 | JOptionPane.ERROR_MESSAGE, null);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Replies the new GPX and marker layers corresponding to the specified NMEA file.
|
---|
95 | * @param is input stream to NMEA 0183 data
|
---|
96 | * @param associatedFile NMEA file
|
---|
97 | * @param gpxLayerName The GPX layer name
|
---|
98 | * @param markerLayerName The marker layer name
|
---|
99 | * @return the new GPX and marker layers corresponding to the specified NMEA file
|
---|
100 | * @throws IOException if an I/O error occurs
|
---|
101 | */
|
---|
102 | public static GpxImporterData loadLayers(InputStream is, final File associatedFile,
|
---|
103 | final String gpxLayerName, String markerLayerName) throws IOException {
|
---|
104 | final NmeaReader r = new NmeaReader(is);
|
---|
105 | final boolean parsedProperly = r.getNumberOfCoordinates() > 0;
|
---|
106 | r.data.storageFile = associatedFile;
|
---|
107 | return GpxImporter.loadLayers(r.data, parsedProperly, gpxLayerName, markerLayerName);
|
---|
108 | }
|
---|
109 | }
|
---|