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.GpxConstants;
|
---|
19 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
20 | import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
|
---|
21 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
22 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
23 | import org.openstreetmap.josm.data.osm.Node;
|
---|
24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
25 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
26 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
27 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
28 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
29 | import org.openstreetmap.josm.tools.GBC;
|
---|
30 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
31 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
32 |
|
---|
33 | public class ConvertToDataLayerAction extends AbstractAction {
|
---|
34 | private final transient GpxLayer layer;
|
---|
35 |
|
---|
36 | public ConvertToDataLayerAction(final GpxLayer layer) {
|
---|
37 | super(tr("Convert to data layer"), ImageProvider.get("converttoosm"));
|
---|
38 | this.layer = layer;
|
---|
39 | putValue("help", ht("/Action/ConvertToDataLayer"));
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void actionPerformed(ActionEvent e) {
|
---|
44 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
45 | msg.add(new JLabel(
|
---|
46 | // CHECKSTYLE.OFF: LineLength
|
---|
47 | tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>If you want to upload traces, look here:</html>")),
|
---|
48 | // CHECKSTYLE.ON: LineLength
|
---|
49 | GBC.eol());
|
---|
50 | msg.add(new UrlLabel(Main.getOSMWebsite() + "/traces", 2), GBC.eop());
|
---|
51 | if (!ConditionalOptionPaneUtil.showConfirmationDialog("convert_to_data", Main.parent, msg, tr("Warning"),
|
---|
52 | JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_OPTION)) {
|
---|
53 | return;
|
---|
54 | }
|
---|
55 | DataSet ds = new DataSet();
|
---|
56 | for (GpxTrack trk : layer.data.tracks) {
|
---|
57 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
58 | List<Node> nodes = new ArrayList<>();
|
---|
59 | for (WayPoint p : segment.getWayPoints()) {
|
---|
60 | Node n = new Node(p.getCoor());
|
---|
61 | String timestr = p.getString(GpxConstants.PT_TIME);
|
---|
62 | if (timestr != null) {
|
---|
63 | n.setTimestamp(DateUtils.fromString(timestr));
|
---|
64 | }
|
---|
65 | ds.addPrimitive(n);
|
---|
66 | nodes.add(n);
|
---|
67 | }
|
---|
68 | Way w = new Way();
|
---|
69 | w.setNodes(nodes);
|
---|
70 | ds.addPrimitive(w);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | Main.main.addLayer(new OsmDataLayer(ds, tr("Converted from: {0}", layer.getName()), layer.getAssociatedFile()));
|
---|
74 | Main.main.removeLayer(layer);
|
---|
75 | }
|
---|
76 | }
|
---|