source: josm/trunk/src/org/openstreetmap/josm/io/GpxImporter.java@ 12636

Last change on this file since 12636 was 12636, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

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