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 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
16 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
17 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
18 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
21 | import org.xml.sax.SAXException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * File importer allowing to import GPX files (*.gpx/gpx.gz files).
|
---|
25 | *
|
---|
26 | */
|
---|
27 | public class GpxImporter extends FileImporter {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * The GPX file filter (*.gpx and *.gpx.gz files).
|
---|
31 | */
|
---|
32 | public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
|
---|
33 | "gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)");
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Utility class containing imported GPX and marker layers, and a task to run after they are added to MapView.
|
---|
37 | */
|
---|
38 | public static class GpxImporterData {
|
---|
39 | /**
|
---|
40 | * The imported GPX layer. May be null if no GPX data.
|
---|
41 | */
|
---|
42 | private GpxLayer gpxLayer;
|
---|
43 | /**
|
---|
44 | * The imported marker layer. May be null if no marker.
|
---|
45 | */
|
---|
46 | private MarkerLayer markerLayer;
|
---|
47 | /**
|
---|
48 | * The task to run after GPX and/or marker layer has been added to MapView.
|
---|
49 | */
|
---|
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 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Constructs a new {@code GpxImporter}.
|
---|
73 | */
|
---|
74 | public GpxImporter() {
|
---|
75 | super(FILE_FILTER);
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
|
---|
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();
|
---|
87 |
|
---|
88 | try {
|
---|
89 | GpxReader r = new GpxReader(is);
|
---|
90 | boolean parsedProperly = r.parse(true);
|
---|
91 | r.getGpxData().storageFile = file;
|
---|
92 | addLayers(loadLayers(r.getGpxData(), parsedProperly, fileName, tr("Markers from {0}", fileName)));
|
---|
93 | } catch (SAXException e) {
|
---|
94 | e.printStackTrace();
|
---|
95 | throw new IOException(tr("Parsing data for layer ''{0}'' failed", fileName));
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
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) {
|
---|
105 | // FIXME: remove UI stuff from the IO subsystem
|
---|
106 | GuiHelper.runInEDT(new Runnable() {
|
---|
107 | @Override
|
---|
108 | public void run() {
|
---|
109 | if (data.markerLayer != null) {
|
---|
110 | Main.main.addLayer(data.markerLayer);
|
---|
111 | }
|
---|
112 | if (data.gpxLayer != null) {
|
---|
113 | Main.main.addLayer(data.gpxLayer);
|
---|
114 | }
|
---|
115 | data.postLayerTask.run();
|
---|
116 | }
|
---|
117 | });
|
---|
118 | }
|
---|
119 |
|
---|
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 | */
|
---|
129 | public static GpxImporterData loadLayers(final GpxData data, final boolean parsedProperly,
|
---|
130 | final String gpxLayerName, String markerLayerName) {
|
---|
131 | GpxLayer gpxLayer = null;
|
---|
132 | MarkerLayer markerLayer = null;
|
---|
133 | if (data.hasRoutePoints() || data.hasTrackPoints()) {
|
---|
134 | gpxLayer = new GpxLayer(data, gpxLayerName, data.storageFile != null);
|
---|
135 | }
|
---|
136 | if (Main.pref.getBoolean("marker.makeautomarkers", true) && !data.waypoints.isEmpty()) {
|
---|
137 | markerLayer = new MarkerLayer(data, markerLayerName, data.storageFile, gpxLayer);
|
---|
138 | if (markerLayer.data.isEmpty()) {
|
---|
139 | markerLayer = null;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | Runnable postLayerTask = new Runnable() {
|
---|
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());
|
---|
153 | }
|
---|
154 | JOptionPane.showMessageDialog(null, msg);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | };
|
---|
158 | return new GpxImporterData(gpxLayer, markerLayer, postLayerTask);
|
---|
159 | }
|
---|
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);
|
---|
166 | r.getGpxData().storageFile = associatedFile;
|
---|
167 | return loadLayers(r.getGpxData(), parsedProperly, gpxLayerName, markerLayerName);
|
---|
168 | } catch (SAXException e) {
|
---|
169 | e.printStackTrace();
|
---|
170 | throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName));
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|