source: josm/trunk/src/org/openstreetmap/josm/gui/layer/gpx/ConvertToDataLayerAction.java @ 6340

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

refactor of some GUI/widgets classes (impacts some plugins):

  • gui.BookmarkList moves to gui.download as it is only meant to be used by gui.download.BookmarkSelection
  • tools.UrlLabel moves to gui.widgets
  • gui.JMultilineLabel, gui.MultiplitLayout, gui.MultiSplitPane move to gui.widgets
File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.gpx;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.util.ArrayList;
10import java.util.List;
11
12import javax.swing.AbstractAction;
13import javax.swing.JLabel;
14import javax.swing.JOptionPane;
15import javax.swing.JPanel;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.gpx.GpxTrack;
19import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
20import org.openstreetmap.josm.data.gpx.WayPoint;
21import org.openstreetmap.josm.data.osm.DataSet;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
25import org.openstreetmap.josm.gui.layer.GpxLayer;
26import org.openstreetmap.josm.gui.layer.OsmDataLayer;
27import org.openstreetmap.josm.gui.widgets.UrlLabel;
28import org.openstreetmap.josm.tools.DateUtils;
29import org.openstreetmap.josm.tools.GBC;
30import org.openstreetmap.josm.tools.ImageProvider;
31
32public class ConvertToDataLayerAction extends AbstractAction {
33    private final GpxLayer layer;
34
35    public ConvertToDataLayerAction(final GpxLayer layer) {
36        super(tr("Convert to data layer"), ImageProvider.get("converttoosm"));
37        this.layer = layer;
38        putValue("help", ht("/Action/ConvertToDataLayer"));
39    }
40
41    @Override
42    public void actionPerformed(ActionEvent e) {
43        JPanel msg = new JPanel(new GridBagLayout());
44        msg.add(new JLabel(tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>If you want to upload traces, look here:</html>")), GBC.eol());
45        msg.add(new UrlLabel(tr("http://www.openstreetmap.org/traces"), 2), GBC.eop());
46        if (!ConditionalOptionPaneUtil.showConfirmationDialog("convert_to_data", Main.parent, msg, tr("Warning"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_OPTION)) {
47            return;
48        }
49        DataSet ds = new DataSet();
50        for (GpxTrack trk : layer.data.tracks) {
51            for (GpxTrackSegment segment : trk.getSegments()) {
52                List<Node> nodes = new ArrayList<Node>();
53                for (WayPoint p : segment.getWayPoints()) {
54                    Node n = new Node(p.getCoor());
55                    String timestr = p.getString("time");
56                    if (timestr != null) {
57                        n.setTimestamp(DateUtils.fromString(timestr));
58                    }
59                    ds.addPrimitive(n);
60                    nodes.add(n);
61                }
62                Way w = new Way();
63                w.setNodes(nodes);
64                ds.addPrimitive(w);
65            }
66        }
67        Main.main.addLayer(new OsmDataLayer(ds, tr("Converted from: {0}", layer.getName()), layer.getAssociatedFile()));
68        Main.main.removeLayer(layer);
69    }
70
71}
Note: See TracBrowser for help on using the repository browser.