1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.directdownload;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
13 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
14 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
15 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
16 | import org.openstreetmap.josm.data.gpx.IGpxTrack;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
19 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
21 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
22 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
23 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
25 |
|
---|
26 | public class DirectDownload extends Plugin {
|
---|
27 | /**
|
---|
28 | * Will be invoked by JOSM to bootstrap the plugin
|
---|
29 | *
|
---|
30 | * @param info information about the plugin and its local installation
|
---|
31 | */
|
---|
32 | public DirectDownload(PluginInformation info) {
|
---|
33 | super(info);
|
---|
34 |
|
---|
35 | MainMenu.add(MainApplication.getMenu().gpsMenu, new DownloadAction());
|
---|
36 | }
|
---|
37 |
|
---|
38 | static class DownloadAction extends JosmAction {
|
---|
39 | DownloadAction() {
|
---|
40 | super(tr("Download Track ..."), "DownloadAction", tr("Download GPX track from openstreetmap.org"),
|
---|
41 | Shortcut.registerShortcut("directdownload:downloadgpxaction", tr("DirectDownload: Download GPX Track"),
|
---|
42 | KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void actionPerformed(ActionEvent event) {
|
---|
47 | DownloadDataGui go = new DownloadDataGui();
|
---|
48 | go.setVisible(true);
|
---|
49 |
|
---|
50 | List<UserTrack> tracks = go.getSelectedUserTracks();
|
---|
51 |
|
---|
52 | if (!((go.getValue() == 1) && (tracks != null))) {
|
---|
53 | return;
|
---|
54 | }
|
---|
55 |
|
---|
56 | for (UserTrack track: tracks) {
|
---|
57 |
|
---|
58 | GpxData data = new GpxServerReader().loadGpx(Long.parseLong(track.id));
|
---|
59 | if (data == null) {
|
---|
60 | return;
|
---|
61 | }
|
---|
62 | GpxData dataNew = new GpxData();
|
---|
63 |
|
---|
64 | for (IGpxTrack trk : data.getTracks()) {
|
---|
65 | HashMap<String, Object> attrib = new HashMap<>(trk.getAttributes());
|
---|
66 | if (!trk.getAttributes().containsKey(GpxConstants.GPX_NAME)) {
|
---|
67 | attrib.put(GpxConstants.GPX_NAME, track.filename);
|
---|
68 | }
|
---|
69 | if (!trk.getAttributes().containsKey(GpxConstants.GPX_DESC)) {
|
---|
70 | attrib.put(GpxConstants.GPX_DESC, track.description);
|
---|
71 | }
|
---|
72 | // replace the existing trace in the unmodifiable tracks
|
---|
73 | dataNew.addTrack(new GpxTrack(new ArrayList<>(trk.getSegments()), attrib));
|
---|
74 | }
|
---|
75 |
|
---|
76 | data = dataNew;
|
---|
77 |
|
---|
78 | final GpxLayer gpxLayer = new GpxLayer(data, (track.filename + " " + track.description).trim());
|
---|
79 |
|
---|
80 | if (data.hasRoutePoints() || data.hasTrackPoints()) {
|
---|
81 | MainApplication.getLayerManager().addLayer(gpxLayer);
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (Config.getPref().getBoolean("marker.makeautomarkers", true) && !data.waypoints.isEmpty()) {
|
---|
85 | MarkerLayer ml = new MarkerLayer(data, tr("Markers from {0}", track.filename), null, gpxLayer);
|
---|
86 | if (!ml.data.isEmpty()) {
|
---|
87 | MainApplication.getLayerManager().addLayer(ml);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|