source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java@ 13569

Last change on this file since 13569 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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