1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.Bounds;
|
---|
9 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
10 | import org.openstreetmap.josm.data.osm.DataSetMerger;
|
---|
11 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
12 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
13 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
14 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
15 | import org.openstreetmap.josm.io.OverpassDownloadReader;
|
---|
16 | import org.xml.sax.SAXException;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Download OSM data from Overpass API
|
---|
20 | *
|
---|
21 | */
|
---|
22 | public class DownloadFromOverpassTask extends PleaseWaitRunnable {
|
---|
23 | private boolean canceled;
|
---|
24 | private final String request;
|
---|
25 | private final DataSet ds;
|
---|
26 | private Exception lastException;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructor
|
---|
30 | * @param request the overpass query
|
---|
31 | * @param ds the {@code DataSet} instance that should contain the downloaded data
|
---|
32 | * @param monitor ProgressMonitor to use or null to create a new one.
|
---|
33 | */
|
---|
34 | public DownloadFromOverpassTask(String request, DataSet ds, ProgressMonitor monitor) {
|
---|
35 | super(tr("Download objects via Overpass API"), monitor, false);
|
---|
36 | this.request = request;
|
---|
37 | this.ds = ds;
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | protected void cancel() {
|
---|
42 | canceled = true;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
47 | try {
|
---|
48 | OverpassDownloadReader reader = new OverpassDownloadReader(new Bounds(0, 0, 0, 0),
|
---|
49 | OverpassDownloadReader.OVERPASS_SERVER.get(), request);
|
---|
50 | DataSet tmpDs = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
|
---|
51 | if (!canceled) {
|
---|
52 | new DataSetMerger(ds, tmpDs).merge();
|
---|
53 | }
|
---|
54 | } catch (OsmTransferException e) {
|
---|
55 | if (canceled)
|
---|
56 | return;
|
---|
57 | lastException = e;
|
---|
58 | }
|
---|
59 |
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | protected void finish() {
|
---|
64 | if (canceled)
|
---|
65 | return;
|
---|
66 | if (lastException != null) {
|
---|
67 | ExceptionDialogUtil.explainException(lastException);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|