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