source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java@ 14374

Last change on this file since 14374 was 14374, checked in by simon04, 5 years ago

fix #16879, see #14831 - Fix zoom to downloaded data

  • Property svn:eol-style set to native
File size: 21.5 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.MalformedURLException;
8import java.net.URL;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.HashSet;
14import java.util.Objects;
15import java.util.Optional;
16import java.util.Set;
17import java.util.concurrent.Future;
18import java.util.regex.Matcher;
19import java.util.regex.Pattern;
20import java.util.stream.Stream;
21
22import org.openstreetmap.josm.data.Bounds;
23import org.openstreetmap.josm.data.DataSource;
24import org.openstreetmap.josm.data.ProjectionBounds;
25import org.openstreetmap.josm.data.ViewportData;
26import org.openstreetmap.josm.data.coor.LatLon;
27import org.openstreetmap.josm.data.osm.DataSet;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.data.osm.Relation;
30import org.openstreetmap.josm.data.osm.Way;
31import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
32import org.openstreetmap.josm.gui.MainApplication;
33import org.openstreetmap.josm.gui.MapFrame;
34import org.openstreetmap.josm.gui.PleaseWaitRunnable;
35import org.openstreetmap.josm.gui.io.UpdatePrimitivesTask;
36import org.openstreetmap.josm.gui.layer.OsmDataLayer;
37import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
38import org.openstreetmap.josm.gui.progress.ProgressMonitor;
39import org.openstreetmap.josm.io.BoundingBoxDownloader;
40import org.openstreetmap.josm.io.OsmServerLocationReader;
41import org.openstreetmap.josm.io.OsmServerLocationReader.OsmUrlPattern;
42import org.openstreetmap.josm.io.OsmServerReader;
43import org.openstreetmap.josm.io.OsmTransferCanceledException;
44import org.openstreetmap.josm.io.OsmTransferException;
45import org.openstreetmap.josm.io.OverpassDownloadReader;
46import org.openstreetmap.josm.tools.Logging;
47import org.openstreetmap.josm.tools.Utils;
48import org.xml.sax.SAXException;
49
50/**
51 * Open the download dialog and download the data.
52 * Run in the worker thread.
53 */
54public class DownloadOsmTask extends AbstractDownloadTask<DataSet> {
55
56 protected Bounds currentBounds;
57 protected DownloadTask downloadTask;
58
59 protected String newLayerName;
60
61 /** This allows subclasses to ignore this warning */
62 protected boolean warnAboutEmptyArea = true;
63
64 protected static final String OVERPASS_INTERPRETER_DATA = "interpreter?data=";
65
66 @Override
67 public String[] getPatterns() {
68 if (this.getClass() == DownloadOsmTask.class) {
69 return Arrays.stream(OsmUrlPattern.values()).map(OsmUrlPattern::pattern).toArray(String[]::new);
70 } else {
71 return super.getPatterns();
72 }
73 }
74
75 @Override
76 public String getTitle() {
77 if (this.getClass() == DownloadOsmTask.class) {
78 return tr("Download OSM");
79 } else {
80 return super.getTitle();
81 }
82 }
83
84 @Override
85 public Future<?> download(DownloadParams settings, Bounds downloadArea, ProgressMonitor progressMonitor) {
86 return download(new BoundingBoxDownloader(downloadArea), settings, downloadArea, progressMonitor);
87 }
88
89 /**
90 * Asynchronously launches the download task for a given bounding box.
91 *
92 * Set <code>progressMonitor</code> to null, if the task should create, open, and close a progress monitor.
93 * Set progressMonitor to {@link NullProgressMonitor#INSTANCE} if progress information is to
94 * be discarded.
95 *
96 * You can wait for the asynchronous download task to finish by synchronizing on the returned
97 * {@link Future}, but make sure not to freeze up JOSM. Example:
98 * <pre>
99 * Future&lt;?&gt; future = task.download(...);
100 * // DON'T run this on the Swing EDT or JOSM will freeze
101 * future.get(); // waits for the dowload task to complete
102 * </pre>
103 *
104 * The following example uses a pattern which is better suited if a task is launched from
105 * the Swing EDT:
106 * <pre>
107 * final Future&lt;?&gt; future = task.download(...);
108 * Runnable runAfterTask = new Runnable() {
109 * public void run() {
110 * // this is not strictly necessary because of the type of executor service
111 * // Main.worker is initialized with, but it doesn't harm either
112 * //
113 * future.get(); // wait for the download task to complete
114 * doSomethingAfterTheTaskCompleted();
115 * }
116 * }
117 * MainApplication.worker.submit(runAfterTask);
118 * </pre>
119 * @param reader the reader used to parse OSM data (see {@link OsmServerReader#parseOsm})
120 * @param settings download settings
121 * @param downloadArea the area to download
122 * @param progressMonitor the progressMonitor
123 * @return the future representing the asynchronous task
124 * @since 13927
125 */
126 public Future<?> download(OsmServerReader reader, DownloadParams settings, Bounds downloadArea, ProgressMonitor progressMonitor) {
127 return download(new DownloadTask(settings, reader, progressMonitor, zoomAfterDownload), downloadArea);
128 }
129
130 protected Future<?> download(DownloadTask downloadTask, Bounds downloadArea) {
131 this.downloadTask = downloadTask;
132 this.currentBounds = new Bounds(downloadArea);
133 // We need submit instead of execute so we can wait for it to finish and get the error
134 // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
135 return MainApplication.worker.submit(downloadTask);
136 }
137
138 /**
139 * This allows subclasses to perform operations on the URL before {@link #loadUrl} is performed.
140 * @param url the original URL
141 * @return the modified URL
142 */
143 protected String modifyUrlBeforeLoad(String url) {
144 return url;
145 }
146
147 /**
148 * Loads a given URL from the OSM Server
149 * @param settings download settings
150 * @param url The URL as String
151 */
152 @Override
153 public Future<?> loadUrl(DownloadParams settings, String url, ProgressMonitor progressMonitor) {
154 String newUrl = modifyUrlBeforeLoad(url);
155 downloadTask = new DownloadTask(settings, getOsmServerReader(newUrl), progressMonitor);
156 currentBounds = null;
157 // Extract .osm filename from URL to set the new layer name
158 extractOsmFilename(settings, "https?://.*/(.*\\.osm)", newUrl);
159 return MainApplication.worker.submit(downloadTask);
160 }
161
162 protected OsmServerReader getOsmServerReader(String url) {
163 try {
164 String host = new URL(url).getHost();
165 for (String knownOverpassServer : OverpassDownloadReader.OVERPASS_SERVER_HISTORY.get()) {
166 if (host.equals(new URL(knownOverpassServer).getHost())) {
167 int index = url.indexOf(OVERPASS_INTERPRETER_DATA);
168 if (index > 0) {
169 return new OverpassDownloadReader(new Bounds(LatLon.ZERO), knownOverpassServer,
170 Utils.decodeUrl(url.substring(index + OVERPASS_INTERPRETER_DATA.length())));
171 }
172 }
173 }
174 } catch (MalformedURLException e) {
175 Logging.error(e);
176 }
177 return new OsmServerLocationReader(url);
178 }
179
180 protected final void extractOsmFilename(DownloadParams settings, String pattern, String url) {
181 newLayerName = settings.getLayerName();
182 if (newLayerName == null || newLayerName.isEmpty()) {
183 Matcher matcher = Pattern.compile(pattern).matcher(url);
184 newLayerName = matcher.matches() ? matcher.group(1) : null;
185 }
186 }
187
188 @Override
189 public void cancel() {
190 if (downloadTask != null) {
191 downloadTask.cancel();
192 }
193 }
194
195 @Override
196 public boolean isSafeForRemotecontrolRequests() {
197 return true;
198 }
199
200 @Override
201 public ProjectionBounds getDownloadProjectionBounds() {
202 return downloadTask != null ? downloadTask.computeBbox(currentBounds).orElse(null) : null;
203 }
204
205 /**
206 * Superclass of internal download task.
207 * @since 7636
208 */
209 public abstract static class AbstractInternalTask extends PleaseWaitRunnable {
210
211 protected final DownloadParams settings;
212 protected final boolean zoomAfterDownload;
213 protected DataSet dataSet;
214
215 /**
216 * Constructs a new {@code AbstractInternalTask}.
217 * @param settings download settings
218 * @param title message for the user
219 * @param ignoreException If true, exception will be propagated to calling code. If false then
220 * exception will be thrown directly in EDT. When this runnable is executed using executor framework
221 * then use false unless you read result of task (because exception will get lost if you don't)
222 * @param zoomAfterDownload If true, the map view will zoom to download area after download
223 */
224 public AbstractInternalTask(DownloadParams settings, String title, boolean ignoreException, boolean zoomAfterDownload) {
225 super(title, ignoreException);
226 this.settings = Objects.requireNonNull(settings);
227 this.zoomAfterDownload = zoomAfterDownload;
228 }
229
230 /**
231 * Constructs a new {@code AbstractInternalTask}.
232 * @param settings download settings
233 * @param title message for the user
234 * @param progressMonitor progress monitor
235 * @param ignoreException If true, exception will be propagated to calling code. If false then
236 * exception will be thrown directly in EDT. When this runnable is executed using executor framework
237 * then use false unless you read result of task (because exception will get lost if you don't)
238 * @param zoomAfterDownload If true, the map view will zoom to download area after download
239 */
240 public AbstractInternalTask(DownloadParams settings, String title, ProgressMonitor progressMonitor, boolean ignoreException,
241 boolean zoomAfterDownload) {
242 super(title, progressMonitor, ignoreException);
243 this.settings = Objects.requireNonNull(settings);
244 this.zoomAfterDownload = zoomAfterDownload;
245 }
246
247 protected OsmDataLayer getEditLayer() {
248 return MainApplication.getLayerManager().getEditLayer();
249 }
250
251 private static Stream<OsmDataLayer> getModifiableDataLayers() {
252 return MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class)
253 .stream().filter(OsmDataLayer::isDownloadable);
254 }
255
256 /**
257 * Returns the number of modifiable data layers
258 * @return number of modifiable data layers
259 * @since 13434
260 */
261 protected long getNumModifiableDataLayers() {
262 return getModifiableDataLayers().count();
263 }
264
265 /**
266 * Returns the first modifiable data layer
267 * @return the first modifiable data layer
268 * @since 13434
269 */
270 protected OsmDataLayer getFirstModifiableDataLayer() {
271 return getModifiableDataLayers().findFirst().orElse(null);
272 }
273
274 /**
275 * Creates a name for a new layer by utilizing the settings ({@link DownloadParams#getLayerName()}) or
276 * {@link OsmDataLayer#createNewName()} if the former option is {@code null}.
277 *
278 * @return a name for a new layer
279 * @since 14347
280 */
281 protected String generateLayerName() {
282 return Optional.ofNullable(settings.getLayerName())
283 .filter(layerName -> !Utils.isStripEmpty(layerName))
284 .orElse(OsmDataLayer.createNewName());
285 }
286
287 /**
288 * Can be overridden (e.g. by plugins) if a subclass of {@link OsmDataLayer} is needed.
289 * If you want to change how the name is determined, consider overriding
290 * {@link #generateLayerName()} instead.
291 *
292 * @param ds the dataset on which the layer is based, must be non-null
293 * @param layerName the name of the new layer, must be either non-blank or non-present
294 * @return a new instance of {@link OsmDataLayer} constructed with the given arguments
295 * @since 14347
296 */
297 protected OsmDataLayer createNewLayer(final DataSet ds, final Optional<String> layerName) {
298 if (layerName.filter(Utils::isStripEmpty).isPresent()) {
299 throw new IllegalArgumentException("Blank layer name!");
300 }
301 return new OsmDataLayer(
302 Objects.requireNonNull(ds, "dataset parameter"),
303 layerName.orElseGet(this::generateLayerName),
304 null
305 );
306 }
307
308 /**
309 * Convenience method for {@link #createNewLayer(DataSet, Optional)}, uses the dataset
310 * from field {@link #dataSet} and applies the settings from field {@link #settings}.
311 *
312 * @param layerName an optional layer name, must be non-blank if the [Optional] is present
313 * @return a newly constructed layer
314 * @since 14347
315 */
316 protected final OsmDataLayer createNewLayer(final Optional<String> layerName) {
317 Optional.ofNullable(settings.getDownloadPolicy())
318 .ifPresent(dataSet::setDownloadPolicy);
319 Optional.ofNullable(settings.getUploadPolicy())
320 .ifPresent(dataSet::setUploadPolicy);
321 if (dataSet.isLocked() && !settings.isLocked()) {
322 dataSet.unlock();
323 } else if (!dataSet.isLocked() && settings.isLocked()) {
324 dataSet.lock();
325 }
326 return createNewLayer(dataSet, layerName);
327 }
328
329 /**
330 * @param layerName the name of the new layer
331 * @deprecated Use {@link #createNewLayer(DataSet, Optional)}
332 * @return a newly constructed layer
333 */
334 @Deprecated
335 protected OsmDataLayer createNewLayer(final String layerName) {
336 return createNewLayer(Optional.ofNullable(layerName).filter(it -> !Utils.isStripEmpty(it)));
337 }
338
339 /**
340 * @deprecated Use {@link #createNewLayer(Optional)}
341 * @return a newly constructed layer
342 */
343 @Deprecated
344 protected OsmDataLayer createNewLayer() {
345 return createNewLayer(Optional.empty());
346 }
347
348 protected Optional<ProjectionBounds> computeBbox(Bounds bounds) {
349 BoundingXYVisitor v = new BoundingXYVisitor();
350 if (bounds != null) {
351 v.visit(bounds);
352 } else {
353 v.computeBoundingBox(dataSet.getNodes());
354 }
355 return Optional.ofNullable(v.getBounds());
356 }
357
358 protected OsmDataLayer addNewLayerIfRequired(String newLayerName) {
359 long numDataLayers = getNumModifiableDataLayers();
360 if (settings.isNewLayer() || numDataLayers == 0 || (numDataLayers > 1 && getEditLayer() == null)) {
361 // the user explicitly wants a new layer, we don't have any layer at all
362 // or it is not clear which layer to merge to
363 final OsmDataLayer layer = createNewLayer(newLayerName);
364 MainApplication.getLayerManager().addLayer(layer, zoomAfterDownload);
365 return layer;
366 }
367 return null;
368 }
369
370 protected void loadData(String newLayerName, Bounds bounds) {
371 OsmDataLayer layer = addNewLayerIfRequired(newLayerName);
372 if (layer == null) {
373 layer = getEditLayer();
374 if (layer == null || !layer.isDownloadable()) {
375 layer = getFirstModifiableDataLayer();
376 }
377 Collection<OsmPrimitive> primitivesToUpdate = searchPrimitivesToUpdate(bounds, layer.getDataSet());
378 layer.mergeFrom(dataSet);
379 MapFrame map = MainApplication.getMap();
380 if (map != null && zoomAfterDownload) {
381 computeBbox(bounds).map(ViewportData::new).ifPresent(map.mapView::zoomTo);
382 }
383 if (!primitivesToUpdate.isEmpty()) {
384 MainApplication.worker.submit(new UpdatePrimitivesTask(layer, primitivesToUpdate));
385 }
386 layer.onPostDownloadFromServer();
387 }
388 }
389
390 /**
391 * Look for primitives deleted on server (thus absent from downloaded data)
392 * but still present in existing data layer
393 * @param bounds download bounds
394 * @param ds existing data set
395 * @return the primitives to update
396 */
397 private Collection<OsmPrimitive> searchPrimitivesToUpdate(Bounds bounds, DataSet ds) {
398 if (bounds == null)
399 return Collections.emptySet();
400 Collection<OsmPrimitive> col = new ArrayList<>();
401 ds.searchNodes(bounds.toBBox()).stream().filter(n -> !n.isNew() && !dataSet.containsNode(n)).forEachOrdered(col::add);
402 if (!col.isEmpty()) {
403 Set<Way> ways = new HashSet<>();
404 Set<Relation> rels = new HashSet<>();
405 for (OsmPrimitive n : col) {
406 for (OsmPrimitive ref : n.getReferrers()) {
407 if (ref.isNew()) {
408 continue;
409 } else if (ref instanceof Way) {
410 ways.add((Way) ref);
411 } else if (ref instanceof Relation) {
412 rels.add((Relation) ref);
413 }
414 }
415 }
416 ways.stream().filter(w -> !dataSet.containsWay(w)).forEachOrdered(col::add);
417 rels.stream().filter(r -> !dataSet.containsRelation(r)).forEachOrdered(col::add);
418 }
419 return col;
420 }
421 }
422
423 protected class DownloadTask extends AbstractInternalTask {
424 protected final OsmServerReader reader;
425
426 /**
427 * Constructs a new {@code DownloadTask}.
428 * @param settings download settings
429 * @param reader OSM data reader
430 * @param progressMonitor progress monitor
431 * @since 13927
432 */
433 public DownloadTask(DownloadParams settings, OsmServerReader reader, ProgressMonitor progressMonitor) {
434 this(settings, reader, progressMonitor, true);
435 }
436
437 /**
438 * Constructs a new {@code DownloadTask}.
439 * @param settings download settings
440 * @param reader OSM data reader
441 * @param progressMonitor progress monitor
442 * @param zoomAfterDownload If true, the map view will zoom to download area after download
443 * @since 13927
444 */
445 public DownloadTask(DownloadParams settings, OsmServerReader reader, ProgressMonitor progressMonitor, boolean zoomAfterDownload) {
446 super(settings, tr("Downloading data"), progressMonitor, false, zoomAfterDownload);
447 this.reader = reader;
448 }
449
450 protected DataSet parseDataSet() throws OsmTransferException {
451 return reader.parseOsm(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
452 }
453
454 @Override
455 public void realRun() throws IOException, SAXException, OsmTransferException {
456 try {
457 if (isCanceled())
458 return;
459 dataSet = parseDataSet();
460 } catch (OsmTransferException e) {
461 if (isCanceled()) {
462 Logging.info(tr("Ignoring exception because download has been canceled. Exception was: {0}", e.toString()));
463 return;
464 }
465 if (e instanceof OsmTransferCanceledException) {
466 setCanceled(true);
467 return;
468 } else {
469 rememberException(e);
470 }
471 DownloadOsmTask.this.setFailed(true);
472 }
473 }
474
475 @Override
476 protected void finish() {
477 if (isFailed() || isCanceled())
478 return;
479 if (dataSet == null)
480 return; // user canceled download or error occurred
481 if (dataSet.allPrimitives().isEmpty()) {
482 if (warnAboutEmptyArea) {
483 rememberErrorMessage(tr("No data found in this area."));
484 }
485 String remark = dataSet.getRemark();
486 if (remark != null && !remark.isEmpty()) {
487 rememberErrorMessage(remark);
488 }
489 // need to synthesize a download bounds lest the visual indication of downloaded area doesn't work
490 dataSet.addDataSource(new DataSource(currentBounds != null ? currentBounds :
491 new Bounds(LatLon.ZERO), "OpenStreetMap server"));
492 }
493
494 rememberDownloadedData(dataSet);
495 loadData(newLayerName, currentBounds);
496 }
497
498 @Override
499 protected void cancel() {
500 setCanceled(true);
501 if (reader != null) {
502 reader.cancel();
503 }
504 }
505 }
506
507 @Override
508 public String getConfirmationMessage(URL url) {
509 if (url != null) {
510 String urlString = url.toExternalForm();
511 if (urlString.matches(OsmUrlPattern.OSM_API_URL.pattern())) {
512 // TODO: proper i18n after stabilization
513 Collection<String> items = new ArrayList<>();
514 items.add(tr("OSM Server URL:") + ' ' + url.getHost());
515 items.add(tr("Command")+": "+url.getPath());
516 if (url.getQuery() != null) {
517 items.add(tr("Request details: {0}", url.getQuery().replaceAll(",\\s*", ", ")));
518 }
519 return Utils.joinAsHtmlUnorderedList(items);
520 }
521 // TODO: other APIs
522 }
523 return null;
524 }
525}
Note: See TracBrowser for help on using the repository browser.