[1637] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.File;
|
---|
| 7 | import java.io.FileInputStream;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 | import java.io.InputStream;
|
---|
| 10 | import java.util.zip.GZIPInputStream;
|
---|
| 11 |
|
---|
[2795] | 12 | import javax.swing.JOptionPane;
|
---|
[2062] | 13 |
|
---|
[1637] | 14 | import org.openstreetmap.josm.Main;
|
---|
| 15 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
[5494] | 16 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
[1637] | 17 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
| 18 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
[2851] | 19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[4695] | 20 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[1637] | 21 | import org.xml.sax.SAXException;
|
---|
| 22 |
|
---|
[5494] | 23 | /**
|
---|
| 24 | * File importer allowing to import GPX files (*.gpx/gpx.gz files).
|
---|
| 25 | *
|
---|
| 26 | */
|
---|
[1637] | 27 | public class GpxImporter extends FileImporter {
|
---|
| 28 |
|
---|
[5494] | 29 | /**
|
---|
| 30 | * The GPX file filter (*.gpx and *.gpx.gz files).
|
---|
| 31 | */
|
---|
[5361] | 32 | public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
|
---|
| 33 | "gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)");
|
---|
[6070] | 34 |
|
---|
[5494] | 35 | /**
|
---|
[6070] | 36 | * Utility class containing imported GPX and marker layers, and a task to run after they are added to MapView.
|
---|
[5494] | 37 | */
|
---|
| 38 | public static class GpxImporterData {
|
---|
| 39 | /**
|
---|
| 40 | * The imported GPX layer. May be null if no GPX data.
|
---|
| 41 | */
|
---|
[5501] | 42 | private GpxLayer gpxLayer;
|
---|
[5494] | 43 | /**
|
---|
| 44 | * The imported marker layer. May be null if no marker.
|
---|
| 45 | */
|
---|
[5501] | 46 | private MarkerLayer markerLayer;
|
---|
[5494] | 47 | /**
|
---|
| 48 | * The task to run after GPX and/or marker layer has been added to MapView.
|
---|
| 49 | */
|
---|
[5501] | 50 | private Runnable postLayerTask;
|
---|
| 51 |
|
---|
| 52 | public GpxImporterData(GpxLayer gpxLayer, MarkerLayer markerLayer, Runnable postLayerTask) {
|
---|
| 53 | this.gpxLayer = gpxLayer;
|
---|
| 54 | this.markerLayer = markerLayer;
|
---|
| 55 | this.postLayerTask = postLayerTask;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public GpxLayer getGpxLayer() {
|
---|
| 59 | return gpxLayer;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public MarkerLayer getMarkerLayer() {
|
---|
| 63 | return markerLayer;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public Runnable getPostLayerTask() {
|
---|
| 67 | return postLayerTask;
|
---|
| 68 | }
|
---|
[4757] | 69 | }
|
---|
[4695] | 70 |
|
---|
[5494] | 71 | /**
|
---|
| 72 | * Constructs a new {@code GpxImporter}.
|
---|
| 73 | */
|
---|
[1637] | 74 | public GpxImporter() {
|
---|
[5361] | 75 | super(FILE_FILTER);
|
---|
[1637] | 76 | }
|
---|
| 77 |
|
---|
[5494] | 78 | @Override
|
---|
| 79 | public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
|
---|
[4695] | 80 | InputStream is;
|
---|
| 81 | if (file.getName().endsWith(".gpx.gz")) {
|
---|
| 82 | is = new GZIPInputStream(new FileInputStream(file));
|
---|
| 83 | } else {
|
---|
| 84 | is = new FileInputStream(file);
|
---|
| 85 | }
|
---|
| 86 | String fileName = file.getName();
|
---|
[6070] | 87 |
|
---|
[5494] | 88 | try {
|
---|
| 89 | GpxReader r = new GpxReader(is);
|
---|
| 90 | boolean parsedProperly = r.parse(true);
|
---|
[5679] | 91 | r.getGpxData().storageFile = file;
|
---|
| 92 | addLayers(loadLayers(r.getGpxData(), parsedProperly, fileName, tr("Markers from {0}", fileName)));
|
---|
[5494] | 93 | } catch (SAXException e) {
|
---|
| 94 | e.printStackTrace();
|
---|
| 95 | throw new IOException(tr("Parsing data for layer ''{0}'' failed", fileName));
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
[6070] | 98 |
|
---|
[5494] | 99 | /**
|
---|
| 100 | * Adds the specified GPX and marker layers to Map.main
|
---|
| 101 | * @param data The layers to add
|
---|
| 102 | * @see #loadLayers
|
---|
| 103 | */
|
---|
| 104 | public static void addLayers(final GpxImporterData data) {
|
---|
[4695] | 105 | // FIXME: remove UI stuff from the IO subsystem
|
---|
| 106 | GuiHelper.runInEDT(new Runnable() {
|
---|
[6084] | 107 | @Override
|
---|
[4695] | 108 | public void run() {
|
---|
[4757] | 109 | if (data.markerLayer != null) {
|
---|
| 110 | Main.main.addLayer(data.markerLayer);
|
---|
[4695] | 111 | }
|
---|
[4757] | 112 | if (data.gpxLayer != null) {
|
---|
| 113 | Main.main.addLayer(data.gpxLayer);
|
---|
[4695] | 114 | }
|
---|
[4757] | 115 | data.postLayerTask.run();
|
---|
[4695] | 116 | }
|
---|
| 117 | });
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[5494] | 120 | /**
|
---|
| 121 | * Replies the new GPX and marker layers corresponding to the specified GPX data.
|
---|
| 122 | * @param data The GPX data
|
---|
| 123 | * @param parsedProperly True if GPX data has been properly parsed by {@link GpxReader#parse}
|
---|
| 124 | * @param gpxLayerName The GPX layer name
|
---|
| 125 | * @param markerLayerName The marker layer name
|
---|
| 126 | * @return the new GPX and marker layers corresponding to the specified GPX data, to be used with {@link #addLayers}
|
---|
| 127 | * @see #addLayers
|
---|
| 128 | */
|
---|
[6070] | 129 | public static GpxImporterData loadLayers(final GpxData data, final boolean parsedProperly,
|
---|
[5501] | 130 | final String gpxLayerName, String markerLayerName) {
|
---|
| 131 | GpxLayer gpxLayer = null;
|
---|
| 132 | MarkerLayer markerLayer = null;
|
---|
[5494] | 133 | if (data.hasRoutePoints() || data.hasTrackPoints()) {
|
---|
[5501] | 134 | gpxLayer = new GpxLayer(data, gpxLayerName, data.storageFile != null);
|
---|
[5494] | 135 | }
|
---|
| 136 | if (Main.pref.getBoolean("marker.makeautomarkers", true) && !data.waypoints.isEmpty()) {
|
---|
[5501] | 137 | markerLayer = new MarkerLayer(data, markerLayerName, data.storageFile, gpxLayer);
|
---|
| 138 | if (markerLayer.data.isEmpty()) {
|
---|
| 139 | markerLayer = null;
|
---|
[4695] | 140 | }
|
---|
[5494] | 141 | }
|
---|
[5501] | 142 | Runnable postLayerTask = new Runnable() {
|
---|
[5494] | 143 | @Override
|
---|
| 144 | public void run() {
|
---|
| 145 | if (!parsedProperly) {
|
---|
| 146 | String msg;
|
---|
| 147 | if (data.storageFile == null) {
|
---|
| 148 | msg = tr("Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.",
|
---|
| 149 | gpxLayerName);
|
---|
| 150 | } else {
|
---|
| 151 | msg = tr("Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.",
|
---|
| 152 | data.storageFile.getPath());
|
---|
[4710] | 153 | }
|
---|
[5494] | 154 | JOptionPane.showMessageDialog(null, msg);
|
---|
[1814] | 155 | }
|
---|
[5494] | 156 | }
|
---|
| 157 | };
|
---|
[5501] | 158 | return new GpxImporterData(gpxLayer, markerLayer, postLayerTask);
|
---|
[1637] | 159 | }
|
---|
[5501] | 160 |
|
---|
| 161 | public static GpxImporterData loadLayers(InputStream is, final File associatedFile,
|
---|
| 162 | final String gpxLayerName, String markerLayerName, ProgressMonitor progressMonitor) throws IOException {
|
---|
| 163 | try {
|
---|
| 164 | final GpxReader r = new GpxReader(is);
|
---|
| 165 | final boolean parsedProperly = r.parse(true);
|
---|
[5679] | 166 | r.getGpxData().storageFile = associatedFile;
|
---|
| 167 | return loadLayers(r.getGpxData(), parsedProperly, gpxLayerName, markerLayerName);
|
---|
[5501] | 168 | } catch (SAXException e) {
|
---|
| 169 | e.printStackTrace();
|
---|
| 170 | throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName));
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
[1637] | 173 | }
|
---|