1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.net.URL;
|
---|
8 | import java.util.concurrent.Future;
|
---|
9 | import java.util.regex.Matcher;
|
---|
10 | import java.util.regex.Pattern;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.Bounds;
|
---|
14 | import org.openstreetmap.josm.data.Bounds.ParseMethod;
|
---|
15 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
16 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
17 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
18 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
19 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
22 | import org.openstreetmap.josm.gui.progress.ProgressTaskIds;
|
---|
23 | import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
---|
24 | import org.openstreetmap.josm.io.GpxImporter;
|
---|
25 | import org.openstreetmap.josm.io.GpxImporter.GpxImporterData;
|
---|
26 | import org.openstreetmap.josm.io.OsmServerLocationReader;
|
---|
27 | import org.openstreetmap.josm.io.OsmServerReader;
|
---|
28 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
29 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
30 | import org.xml.sax.SAXException;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Task allowing to download GPS data.
|
---|
34 | */
|
---|
35 | public class DownloadGpsTask extends AbstractDownloadTask {
|
---|
36 |
|
---|
37 | private DownloadTask downloadTask;
|
---|
38 |
|
---|
39 | private static final String PATTERN_TRACE_ID = "https?://.*(osm|openstreetmap).org/trace/\\p{Digit}+/data";
|
---|
40 |
|
---|
41 | private static final String PATTERN_TRACKPOINTS_BBOX = "https?://.*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*";
|
---|
42 |
|
---|
43 | private static final String PATTERN_EXTERNAL_GPX_SCRIPT = "https?://.*exportgpx.*";
|
---|
44 | private static final String PATTERN_EXTERNAL_GPX_FILE = "https?://.*/(.*\\.gpx)";
|
---|
45 |
|
---|
46 | protected String newLayerName = null;
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public String[] getPatterns() {
|
---|
50 | return new String[] {PATTERN_EXTERNAL_GPX_FILE, PATTERN_EXTERNAL_GPX_SCRIPT, PATTERN_TRACE_ID, PATTERN_TRACKPOINTS_BBOX};
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public String getTitle() {
|
---|
55 | return tr("Download GPS");
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
|
---|
60 | downloadTask = new DownloadTask(newLayer,
|
---|
61 | new BoundingBoxDownloader(downloadArea), progressMonitor);
|
---|
62 | // We need submit instead of execute so we can wait for it to finish and get the error
|
---|
63 | // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
|
---|
64 | return Main.worker.submit(downloadTask);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
|
---|
69 | CheckParameterUtil.ensureParameterNotNull(url, "url");
|
---|
70 | if (url.matches(PATTERN_TRACE_ID)
|
---|
71 | || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT)
|
---|
72 | || url.matches(PATTERN_EXTERNAL_GPX_FILE)) {
|
---|
73 | downloadTask = new DownloadTask(newLayer,
|
---|
74 | new OsmServerLocationReader(url), progressMonitor);
|
---|
75 | // Extract .gpx filename from URL to set the new layer name
|
---|
76 | Matcher matcher = Pattern.compile(PATTERN_EXTERNAL_GPX_FILE).matcher(url);
|
---|
77 | newLayerName = matcher.matches() ? matcher.group(1) : null;
|
---|
78 | // We need submit instead of execute so we can wait for it to finish and get the error
|
---|
79 | // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
|
---|
80 | return Main.worker.submit(downloadTask);
|
---|
81 |
|
---|
82 | } else if (url.matches(PATTERN_TRACKPOINTS_BBOX)) {
|
---|
83 | String[] table = url.split("\\?|=|&");
|
---|
84 | for (int i = 0; i<table.length; i++) {
|
---|
85 | if ("bbox".equals(table[i]) && i<table.length-1)
|
---|
86 | return download(newLayer, new Bounds(table[i+1], ",", ParseMethod.LEFT_BOTTOM_RIGHT_TOP), progressMonitor);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return null;
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public void cancel() {
|
---|
94 | if (downloadTask != null) {
|
---|
95 | downloadTask.cancel();
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | class DownloadTask extends PleaseWaitRunnable {
|
---|
100 | private OsmServerReader reader;
|
---|
101 | private GpxData rawData;
|
---|
102 | private final boolean newLayer;
|
---|
103 |
|
---|
104 | public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) {
|
---|
105 | super(tr("Downloading GPS data"), progressMonitor, false);
|
---|
106 | this.reader = reader;
|
---|
107 | this.newLayer = newLayer;
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public void realRun() throws IOException, SAXException, OsmTransferException {
|
---|
112 | try {
|
---|
113 | if (isCanceled())
|
---|
114 | return;
|
---|
115 | ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
|
---|
116 | rawData = reader.parseRawGps(subMonitor);
|
---|
117 | } catch(Exception e) {
|
---|
118 | if (isCanceled())
|
---|
119 | return;
|
---|
120 | if (e instanceof OsmTransferException) {
|
---|
121 | rememberException(e);
|
---|
122 | } else {
|
---|
123 | rememberException(new OsmTransferException(e));
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | protected void finish() {
|
---|
130 | if (isCanceled() || isFailed())
|
---|
131 | return;
|
---|
132 | if (rawData == null)
|
---|
133 | return;
|
---|
134 | String name = newLayerName != null ? newLayerName : tr("Downloaded GPX Data");
|
---|
135 |
|
---|
136 | GpxImporterData layers = GpxImporter.loadLayers(rawData, reader.isGpxParsedProperly(), name,
|
---|
137 | tr("Markers from {0}", name));
|
---|
138 |
|
---|
139 | GpxLayer gpxLayer = addOrMergeLayer(layers.getGpxLayer(), findGpxMergeLayer());
|
---|
140 | addOrMergeLayer(layers.getMarkerLayer(), findMarkerMergeLayer(gpxLayer));
|
---|
141 |
|
---|
142 | layers.getPostLayerTask().run();
|
---|
143 | }
|
---|
144 |
|
---|
145 | private <L extends Layer> L addOrMergeLayer(L layer, L mergeLayer) {
|
---|
146 | if (layer == null) return null;
|
---|
147 | if (newLayer || mergeLayer == null) {
|
---|
148 | Main.main.addLayer(layer);
|
---|
149 | return layer;
|
---|
150 | } else {
|
---|
151 | mergeLayer.mergeFrom(layer);
|
---|
152 | Main.map.repaint();
|
---|
153 | return mergeLayer;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | private GpxLayer findGpxMergeLayer() {
|
---|
158 | if (!Main.isDisplayingMapView())
|
---|
159 | return null;
|
---|
160 | boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
|
---|
161 | Layer active = Main.map.mapView.getActiveLayer();
|
---|
162 | if (active instanceof GpxLayer && (merge || ((GpxLayer)active).data.fromServer))
|
---|
163 | return (GpxLayer) active;
|
---|
164 | for (GpxLayer l : Main.map.mapView.getLayersOfType(GpxLayer.class)) {
|
---|
165 | if (merge || l.data.fromServer)
|
---|
166 | return l;
|
---|
167 | }
|
---|
168 | return null;
|
---|
169 | }
|
---|
170 |
|
---|
171 | private MarkerLayer findMarkerMergeLayer(GpxLayer fromLayer) {
|
---|
172 | if (!Main.isDisplayingMapView())
|
---|
173 | return null;
|
---|
174 | for (MarkerLayer l : Main.map.mapView.getLayersOfType(MarkerLayer.class)) {
|
---|
175 | if (fromLayer != null && l.fromLayer == fromLayer)
|
---|
176 | return l;
|
---|
177 | }
|
---|
178 | return null;
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override protected void cancel() {
|
---|
182 | setCanceled(true);
|
---|
183 | if (reader != null) {
|
---|
184 | reader.cancel();
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public ProgressTaskId canRunInBackground() {
|
---|
190 | return ProgressTaskIds.DOWNLOAD_GPS;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Override
|
---|
195 | public String getConfirmationMessage(URL url) {
|
---|
196 | // TODO
|
---|
197 | return null;
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public boolean isSafeForRemotecontrolRequests() {
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Determines if the given URL denotes an OSM gpx-related API call.
|
---|
207 | * @param url The url to check
|
---|
208 | * @return true if the url matches "Trace ID" API call or "Trackpoints bbox" API call, false otherwise
|
---|
209 | * @see GpxData#fromServer
|
---|
210 | * @since 5745
|
---|
211 | */
|
---|
212 | public static final boolean isFromServer(String url) {
|
---|
213 | return url != null && (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_TRACKPOINTS_BBOX));
|
---|
214 | }
|
---|
215 | }
|
---|