public static GpxData toGpxData(DataSet data, File file) { GpxData gpxData = new GpxData(); gpxData.storageFile = file; HashSet<Node> doneNodes = new HashSet<Node>(); for (Way w : data.ways) { if (w.incomplete || w.deleted) continue; ImmutableGpxTrack trk = new ImmutableGpxTrack( new LinkedList<Collection<WayPoint>>(), new HashMap<String, Object>()); gpxData.tracks.add(trk); if (w.get("name") != null) trk.attr.put("name", w.get("name")); ArrayList<WayPoint> trkseg = null; for (Node n : w.nodes) { if (n.incomplete || n.deleted) { trkseg = null; continue; } if (trkseg == null) { trkseg = new ArrayList<WayPoint>(); trk.trackSegs.add(trkseg); } if (!n.isTagged()) { doneNodes.add(n); } WayPoint wpt = new WayPoint(n.getCoor()); if (!n.isTimestampEmpty()) { wpt.attr.put("time", DateUtils.fromDate(n.getTimestamp())); wpt.setTime(); } trkseg.add(wpt); } } // what is this loop meant to do? it creates waypoints but never // records them? for (Node n : data.nodes) { if (n.incomplete || n.deleted || doneNodes.contains(n)) continue; WayPoint wpt = new WayPoint(n.getCoor()); if (!n.isTimestampEmpty()) { wpt.attr.put("time", DateUtils.fromDate(n.getTimestamp())); wpt.setTime(); } if (n.keys != null && n.keys.containsKey("name")) { wpt.attr.put("name", n.keys.get("name")); } } return gpxData; }
@Override public void actionPerformed(ActionEvent arg0) { FileFilter filter = new FileFilter() { @Override public boolean accept(File f) { return f.isDirectory() || Utils.hasExtension(f, "gpx", "gpx.gz"); } @Override public String getDescription() { return tr("GPX Files (*.gpx *.gpx.gz)"); } }; AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser( true, false, null, filter, JFileChooser.FILES_ONLY, null); if (fc == null) return; File sel = fc.getSelectedFile(); try { outerPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); for (int i = gpxLst.size() - 1; i >= 0; i--) { GpxDataWrapper wrapper = gpxLst.get(i); if (wrapper.file != null && sel.equals(wrapper.file)) { cbGpx.setSelectedIndex(i); if (!sel.getName().equals(wrapper.name)) { JOptionPane.showMessageDialog( Main.parent, tr("File {0} is loaded yet under the name \"{1}\"", sel.getName(), wrapper.name), tr("Error"), JOptionPane.ERROR_MESSAGE); } return; } } GpxData data = null; try (InputStream iStream = createInputStream(sel)) { GpxReader reader = new GpxReader(iStream); reader.parse(false); data = reader.getGpxData(); data.storageFile = sel; } catch (SAXException x) { Main.error(x); JOptionPane.showMessageDialog( Main.parent, tr("Error while parsing {0}", sel.getName()) + ": " + x.getMessage(), tr("Error"), JOptionPane.ERROR_MESSAGE); return; } catch (IOException x) { Main.error(x); JOptionPane.showMessageDialog( Main.parent, tr("Could not read \"{0}\"", sel.getName()) + '\n' + x.getMessage(), tr("Error"), JOptionPane.ERROR_MESSAGE); return; } loadedGpxData.add(data); if (gpxLst.get(0).file == null) { gpxLst.remove(0); } gpxLst.add(new GpxDataWrapper(sel.getName(), data, sel)); cbGpx.setSelectedIndex(cbGpx.getItemCount() - 1); } finally { outerPanel.setCursor(Cursor.getDefaultCursor()); } }