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