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

Last change on this file since 2474 was 2235, checked in by Gubaer, 15 years ago

fixed #3182: GPX consisting of only markers loads as two layers instead of one.

File size: 3.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.FileNotFoundException;
9import java.io.IOException;
10import java.io.InputStream;
11import java.util.zip.GZIPInputStream;
12
13import javax.swing.SwingUtilities;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.actions.ExtensionFileFilter;
17import org.openstreetmap.josm.gui.layer.GpxLayer;
18import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
19import org.xml.sax.SAXException;
20
21public class GpxImporter extends FileImporter {
22
23 public GpxImporter() {
24 super(new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"));
25 }
26
27 @Override public void importData(final File file) throws IOException {
28 final String fn = file.getName();
29
30 try {
31 InputStream is;
32 if (file.getName().endsWith(".gpx.gz")) {
33 is = new GZIPInputStream(new FileInputStream(file));
34 } else {
35 is = new FileInputStream(file);
36 }
37 // Workaround for SAX BOM bug
38 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835
39 if (!((is.read() == 0xef) && (is.read() == 0xbb) && (is.read() == 0xbf))) {
40 is.close();
41 if (file.getName().endsWith(".gpx.gz")) {
42 is = new GZIPInputStream(new FileInputStream(file));
43 } else {
44 is = new FileInputStream(file);
45 }
46 }
47 final GpxReader r = new GpxReader(is, file.getAbsoluteFile().getParentFile());
48 r.data.storageFile = file;
49 final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
50
51 // FIXME: remove UI stuff from the IO subsystem
52 //
53 Runnable task = new Runnable() {
54 public void run() {
55 if (!r.data.tracks.isEmpty() || ! r.data.routes.isEmpty()) {
56 Main.main.addLayer(gpxLayer);
57 }
58 if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) {
59 MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
60 if (ml.data.size() > 0) {
61 Main.main.addLayer(ml);
62 }
63 }
64 }
65 };
66 if (SwingUtilities.isEventDispatchThread()) {
67 task.run();
68 } else {
69 SwingUtilities.invokeLater(task);
70 }
71 } catch (FileNotFoundException e) {
72 e.printStackTrace();
73 throw new IOException(tr("File \"{0}\" does not exist", file.getName()));
74 } catch (SAXException e) {
75 e.printStackTrace();
76 throw new IOException(tr("Parsing file \"{0}\" failed", file.getName()));
77 }
78 }
79}
Note: See TracBrowser for help on using the repository browser.