// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.actions.downloadtasks;
import static org.openstreetmap.josm.tools.I18n.tr;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.DataSource;
import org.openstreetmap.josm.data.ProjectionBounds;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
import org.openstreetmap.josm.gui.PleaseWaitRunnable;
import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
import org.openstreetmap.josm.gui.progress.ProgressMonitor;
import org.openstreetmap.josm.io.BoundingBoxDownloader;
import org.openstreetmap.josm.io.OsmServerLocationReader;
import org.openstreetmap.josm.io.OsmServerReader;
import org.openstreetmap.josm.io.OsmTransferCanceledException;
import org.openstreetmap.josm.io.OsmTransferException;
import org.openstreetmap.josm.tools.Utils;
import org.xml.sax.SAXException;
/**
* Open the download dialog and download the data.
* Run in the worker thread.
*/
public class DownloadOsmTask extends AbstractDownloadTask {
protected static final String PATTERN_OSM_API_URL = "https?://.*/api/0.6/(map|nodes?|ways?|relations?|\\*).*";
protected static final String PATTERN_OVERPASS_API_URL = "https?://.*/interpreter\\?data=.*";
protected static final String PATTERN_OVERPASS_API_XAPI_URL = "https?://.*/xapi(\\?.*\\[@meta\\]|_meta\\?).*";
protected static final String PATTERN_EXTERNAL_OSM_FILE = "https?://.*/.*\\.osm";
protected Bounds currentBounds;
protected DataSet downloadedData;
protected DownloadTask downloadTask;
protected String newLayerName = null;
@Override
public String[] getPatterns() {
if (this.getClass() == DownloadOsmTask.class) {
return new String[]{PATTERN_OSM_API_URL, PATTERN_OVERPASS_API_URL,
PATTERN_OVERPASS_API_XAPI_URL, PATTERN_EXTERNAL_OSM_FILE};
} else {
return super.getPatterns();
}
}
@Override
public String getTitle() {
if (this.getClass() == DownloadOsmTask.class) {
return tr("Download OSM");
} else {
return super.getTitle();
}
}
protected void rememberDownloadedData(DataSet ds) {
this.downloadedData = ds;
}
/**
* Replies the {@link DataSet} containing the downloaded OSM data.
* @return The {@link DataSet} containing the downloaded OSM data.
*/
public DataSet getDownloadedData() {
return downloadedData;
}
@Override
public Future> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
return download(new BoundingBoxDownloader(downloadArea), newLayer, downloadArea, progressMonitor);
}
/**
* Asynchronously launches the download task for a given bounding box.
*
* Set progressMonitor
to null, if the task should create, open, and close a progress monitor.
* Set progressMonitor to {@link NullProgressMonitor#INSTANCE} if progress information is to
* be discarded.
*
* You can wait for the asynchronous download task to finish by synchronizing on the returned
* {@link Future}, but make sure not to freeze up JOSM. Example:
*
* Future<?> future = task.download(...); * // DON'T run this on the Swing EDT or JOSM will freeze * future.get(); // waits for the dowload task to complete ** * The following example uses a pattern which is better suited if a task is launched from * the Swing EDT: *
* final Future<?> future = task.download(...); * Runnable runAfterTask = new Runnable() { * public void run() { * // this is not strictly necessary because of the type of executor service * // Main.worker is initialized with, but it doesn't harm either * // * future.get(); // wait for the download task to complete * doSomethingAfterTheTaskCompleted(); * } * } * Main.worker.submit(runAfterTask); ** @param reader the reader used to parse OSM data (see {@link OsmServerReader#parseOsm}) * @param newLayer true, if the data is to be downloaded into a new layer. If false, the task * selects one of the existing layers as download layer, preferably the active layer. * @param downloadArea the area to download * @param progressMonitor the progressMonitor * @return the future representing the asynchronous task */ public Future> download(OsmServerReader reader, boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) { return download(new DownloadTask(newLayer, reader, progressMonitor), downloadArea); } protected Future> download(DownloadTask downloadTask, Bounds downloadArea) { this.downloadTask = downloadTask; this.currentBounds = new Bounds(downloadArea); // We need submit instead of execute so we can wait for it to finish and get the error // message if necessary. If no one calls getErrorMessage() it just behaves like execute. return Main.worker.submit(downloadTask); } /** * This allows subclasses to perform operations on the URL before {@link #loadUrl} is performed. */ protected String modifyUrlBeforeLoad(String url) { return url; } /** * Loads a given URL from the OSM Server * @param new_layer True if the data should be saved to a new layer * @param url The URL as String */ @Override public Future> loadUrl(boolean new_layer, String url, ProgressMonitor progressMonitor) { url = modifyUrlBeforeLoad(url); downloadTask = new DownloadTask(new_layer, new OsmServerLocationReader(url), progressMonitor); currentBounds = null; // Extract .osm filename from URL to set the new layer name extractOsmFilename("https?://.*/(.*\\.osm)", url); return Main.worker.submit(downloadTask); } protected final void extractOsmFilename(String pattern, String url) { Matcher matcher = Pattern.compile(pattern).matcher(url); newLayerName = matcher.matches() ? matcher.group(1) : null; } @Override public void cancel() { if (downloadTask != null) { downloadTask.cancel(); } } @Override public boolean isSafeForRemotecontrolRequests() { return true; } /** * Superclass of internal download task. * @since 7636 */ public abstract static class AbstractInternalTask extends PleaseWaitRunnable { protected final boolean newLayer; protected DataSet dataSet; /** * Constructs a new {@code AbstractInternalTask}. * * @param newLayer if {@code true}, force download to a new layer * @param title message for the user * @param ignoreException If true, exception will be propagated to calling code. If false then * exception will be thrown directly in EDT. When this runnable is executed using executor framework * then use false unless you read result of task (because exception will get lost if you don't) */ public AbstractInternalTask(boolean newLayer, String title, boolean ignoreException) { super(title, ignoreException); this.newLayer = newLayer; } /** * Constructs a new {@code AbstractInternalTask}. * * @param newLayer if {@code true}, force download to a new layer * @param title message for the user * @param progressMonitor progress monitor * @param ignoreException If true, exception will be propagated to calling code. If false then * exception will be thrown directly in EDT. When this runnable is executed using executor framework * then use false unless you read result of task (because exception will get lost if you don't) */ public AbstractInternalTask(boolean newLayer, String title, ProgressMonitor progressMonitor, boolean ignoreException) { super(title, progressMonitor, ignoreException); this.newLayer = newLayer; } protected OsmDataLayer getEditLayer() { if (!Main.isDisplayingMapView()) return null; return Main.main.getEditLayer(); } protected int getNumDataLayers() { int count = 0; if (!Main.isDisplayingMapView()) return 0; Collection