1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.stream.Collectors;
|
---|
10 |
|
---|
11 | import javax.swing.AbstractAction;
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
|
---|
15 | import org.openstreetmap.josm.actions.AbstractMergeAction;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
18 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
19 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
20 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
21 | import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
|
---|
22 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
23 | import org.openstreetmap.josm.gui.progress.ProgressTaskIds;
|
---|
24 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
25 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
26 | import org.openstreetmap.josm.tools.Logging;
|
---|
27 | import org.xml.sax.SAXException;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Class downloading WMS and TMS along the GPX track.
|
---|
31 | * @since 5715
|
---|
32 | */
|
---|
33 | public class DownloadWmsAlongTrackAction extends AbstractAction {
|
---|
34 |
|
---|
35 | private final transient GpxData data;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * @param data that represents GPX track, along which data should be downloaded
|
---|
39 | */
|
---|
40 | public DownloadWmsAlongTrackAction(final GpxData data) {
|
---|
41 | super(tr("Precache imagery tiles along this track"));
|
---|
42 | new ImageProvider("downloadalongtrack").getResource().attachImageIcon(this, true);
|
---|
43 | this.data = data;
|
---|
44 | }
|
---|
45 |
|
---|
46 | static class PrecacheWmsTask extends PleaseWaitRunnable {
|
---|
47 |
|
---|
48 | private final AbstractTileSourceLayer<? extends AbstractTMSTileSource> layer;
|
---|
49 | private final List<LatLon> points;
|
---|
50 | private AbstractTileSourceLayer<? extends AbstractTMSTileSource>.PrecacheTask precacheTask;
|
---|
51 |
|
---|
52 | protected PrecacheWmsTask(AbstractTileSourceLayer<? extends AbstractTMSTileSource> layer, List<LatLon> points) {
|
---|
53 | super(tr("Precaching WMS"));
|
---|
54 | this.layer = layer;
|
---|
55 | this.points = points;
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
60 | precacheTask = layer.getDownloadAreaToCacheTask(progressMonitor, points, 0, 0);
|
---|
61 | precacheTask.run();
|
---|
62 | synchronized (this) {
|
---|
63 | try {
|
---|
64 | while (!precacheTask.isFinished() && !progressMonitor.isCanceled()) {
|
---|
65 | wait(200);
|
---|
66 | }
|
---|
67 | } catch (InterruptedException ex) {
|
---|
68 | Logging.warn("InterruptedException in "+getClass().getSimpleName()+" while precaching WMS");
|
---|
69 | Thread.currentThread().interrupt();
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | protected void finish() {
|
---|
76 | // Do nothing
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | protected void cancel() {
|
---|
81 | precacheTask.cancel();
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public ProgressTaskId canRunInBackground() {
|
---|
86 | return ProgressTaskIds.PRECACHE_WMS;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | PrecacheWmsTask createTask() {
|
---|
91 | List<LatLon> points = data.tracks.stream()
|
---|
92 | .flatMap(trk -> trk.getSegments().stream())
|
---|
93 | .flatMap(segment -> segment.getWayPoints().stream())
|
---|
94 | .map(WayPoint::getCoor)
|
---|
95 | .collect(Collectors.toList());
|
---|
96 | for (WayPoint p : data.waypoints) {
|
---|
97 | points.add(p.getCoor());
|
---|
98 | }
|
---|
99 | AbstractTileSourceLayer<? extends AbstractTMSTileSource> layer = askedLayer();
|
---|
100 | return layer != null ? new PrecacheWmsTask(layer, points) : null;
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public void actionPerformed(ActionEvent e) {
|
---|
105 | PrecacheWmsTask task = createTask();
|
---|
106 | if (task != null) {
|
---|
107 | MainApplication.worker.execute(task);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | @SuppressWarnings({ "rawtypes", "unchecked" })
|
---|
112 | protected AbstractTileSourceLayer<? extends AbstractTMSTileSource> askedLayer() {
|
---|
113 | List<AbstractTileSourceLayer> targetLayers = MainApplication.getLayerManager().getLayersOfType(AbstractTileSourceLayer.class);
|
---|
114 | if (targetLayers.isEmpty()) {
|
---|
115 | warnNoImageryLayers();
|
---|
116 | return null;
|
---|
117 | }
|
---|
118 | return AbstractMergeAction.askTargetLayer(targetLayers.toArray(new AbstractTileSourceLayer[0]),
|
---|
119 | tr("Please select the imagery layer."),
|
---|
120 | tr("Select imagery layer"),
|
---|
121 | tr("Download"), "dialogs/down");
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected void warnNoImageryLayers() {
|
---|
125 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
---|
126 | tr("There are no imagery layers."), tr("No imagery layers"), JOptionPane.WARNING_MESSAGE);
|
---|
127 | }
|
---|
128 | }
|
---|