1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.AbstractAction;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
19 | import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
|
---|
20 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
25 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
26 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
27 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
28 | import org.openstreetmap.josm.tools.GBC;
|
---|
29 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
30 | import org.openstreetmap.josm.tools.UrlLabel;
|
---|
31 |
|
---|
32 | public 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 | }
|
---|