1 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
2 |
|
---|
3 | import java.awt.GridBagLayout;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.List;
|
---|
7 | import javax.swing.AbstractAction;
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 | import javax.swing.JPanel;
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
13 | import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
|
---|
14 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
21 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
22 | import org.openstreetmap.josm.tools.GBC;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 | import org.openstreetmap.josm.tools.UrlLabel;
|
---|
25 |
|
---|
26 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
27 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
28 |
|
---|
29 | public class ConvertToDataLayerAction extends AbstractAction {
|
---|
30 | private final GpxLayer layer;
|
---|
31 |
|
---|
32 | public ConvertToDataLayerAction(final GpxLayer layer) {
|
---|
33 | super(tr("Convert to data layer"), ImageProvider.get("converttoosm"));
|
---|
34 | this.layer = layer;
|
---|
35 | putValue("help", ht("/Action/ConvertToDataLayer"));
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public void actionPerformed(ActionEvent e) {
|
---|
40 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
41 | 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());
|
---|
42 | msg.add(new UrlLabel(tr("http://www.openstreetmap.org/traces"), 2), GBC.eop());
|
---|
43 | if (!ConditionalOptionPaneUtil.showConfirmationDialog("convert_to_data", Main.parent, msg, tr("Warning"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_OPTION)) {
|
---|
44 | return;
|
---|
45 | }
|
---|
46 | DataSet ds = new DataSet();
|
---|
47 | for (GpxTrack trk : layer.data.tracks) {
|
---|
48 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
49 | List<Node> nodes = new ArrayList<Node>();
|
---|
50 | for (WayPoint p : segment.getWayPoints()) {
|
---|
51 | Node n = new Node(p.getCoor());
|
---|
52 | String timestr = p.getString("time");
|
---|
53 | if (timestr != null) {
|
---|
54 | n.setTimestamp(DateUtils.fromString(timestr));
|
---|
55 | }
|
---|
56 | ds.addPrimitive(n);
|
---|
57 | nodes.add(n);
|
---|
58 | }
|
---|
59 | Way w = new Way();
|
---|
60 | w.setNodes(nodes);
|
---|
61 | ds.addPrimitive(w);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | Main.main.addLayer(new OsmDataLayer(ds, tr("Converted from: {0}", layer.getName()), layer.getAssociatedFile()));
|
---|
65 | Main.main.removeLayer(layer);
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|