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