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

Last change on this file since 5429 was 5361, checked in by Don-vip, 12 years ago

fix #7879 - Allow to open local and remote gzipped/bzipped osmChange files + remote osm.gz files + make some public constants of File filters to share between same importers/exporters

  • Property svn:eol-style set to native
File size: 4.0 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.FileInputStream;
8import java.io.IOException;
9import java.io.InputStream;
10import java.util.zip.GZIPInputStream;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.ExtensionFileFilter;
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.xml.sax.SAXException;
21
22public class GpxImporter extends FileImporter {
23
24 public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
25 "gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)");
26
27 protected static class GpxImporterData {
28 public GpxLayer gpxLayer;
29 public MarkerLayer markerLayer;
30 public Runnable postLayerTask;
31 }
32
33 public GpxImporter() {
34 super(FILE_FILTER);
35 }
36
37 @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
38 InputStream is;
39 if (file.getName().endsWith(".gpx.gz")) {
40 is = new GZIPInputStream(new FileInputStream(file));
41 } else {
42 is = new FileInputStream(file);
43 }
44 String fileName = file.getName();
45 final GpxImporterData data = loadLayers(is, file, fileName, tr("Markers from {0}", fileName), progressMonitor);
46
47 // FIXME: remove UI stuff from the IO subsystem
48 GuiHelper.runInEDT(new Runnable() {
49 public void run() {
50 if (data.markerLayer != null) {
51 Main.main.addLayer(data.markerLayer);
52 }
53 if (data.gpxLayer != null) {
54 Main.main.addLayer(data.gpxLayer);
55 }
56 data.postLayerTask.run();
57 }
58 });
59 }
60
61 public GpxImporterData loadLayers(InputStream is, final File associatedFile,
62 final String gpxLayerName, String markerLayerName, ProgressMonitor progressMonitor) throws IOException {
63 final GpxImporterData data = new GpxImporterData();
64 try {
65 final GpxReader r = new GpxReader(is);
66 final boolean parsedProperly = r.parse(true);
67 r.data.storageFile = associatedFile;
68 if (r.data.hasRoutePoints() || r.data.hasTrackPoints()) {
69 data.gpxLayer = new GpxLayer(r.data, gpxLayerName, associatedFile != null);
70 }
71 if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) {
72 data.markerLayer = new MarkerLayer(r.data, markerLayerName, associatedFile, data.gpxLayer, false);
73 if (data.markerLayer.data.size() == 0) {
74 data.markerLayer = null;
75 }
76 }
77 data.postLayerTask = new Runnable() {
78 @Override
79 public void run() {
80 if (data.markerLayer != null) {
81 data.markerLayer.addMouseHandler();
82 }
83 if (!parsedProperly) {
84 String msg;
85 if (associatedFile == null) {
86 msg = tr("Error occurred while parsing gpx data for layer ''{0}''. Only a part of the file will be available.",
87 gpxLayerName);
88 } else {
89 msg = tr("Error occurred while parsing gpx file ''{0}''. Only a part of the file will be available.",
90 associatedFile.getPath());
91 }
92 JOptionPane.showMessageDialog(null, msg);
93 }
94 }
95 };
96 } catch (SAXException e) {
97 e.printStackTrace();
98 throw new IOException(tr("Parsing data for layer ''{0}'' failed", gpxLayerName));
99 }
100 return data;
101 }
102}
Note: See TracBrowser for help on using the repository browser.