| 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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
|---|
| 6 |
|
|---|
| 7 | import java.io.IOException;
|
|---|
| 8 | import java.net.URL;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 | import java.util.concurrent.Future;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.JOptionPane;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 15 | import org.openstreetmap.josm.data.DataSource;
|
|---|
| 16 | import org.openstreetmap.josm.data.ProjectionBounds;
|
|---|
| 17 | import org.openstreetmap.josm.data.ViewportData;
|
|---|
| 18 | import org.openstreetmap.josm.data.notes.Note;
|
|---|
| 19 | import org.openstreetmap.josm.data.osm.NoteData;
|
|---|
| 20 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
|---|
| 21 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 22 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 23 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 24 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
|---|
| 25 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
|---|
| 26 | import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
|---|
| 27 | import org.openstreetmap.josm.io.BoundingBoxDownloader.MoreNotesException;
|
|---|
| 28 | import org.openstreetmap.josm.io.Compression;
|
|---|
| 29 | import org.openstreetmap.josm.io.OsmApi;
|
|---|
| 30 | import org.openstreetmap.josm.io.OsmServerLocationReader;
|
|---|
| 31 | import org.openstreetmap.josm.io.OsmServerReader;
|
|---|
| 32 | import org.openstreetmap.josm.io.OsmTransferException;
|
|---|
| 33 | import org.openstreetmap.josm.io.UrlPatterns.NoteUrlPattern;
|
|---|
| 34 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 35 | import org.xml.sax.SAXException;
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * General task for downloading OSM notes.
|
|---|
| 39 | * <p>
|
|---|
| 40 | * It handles two URL patterns: API call and dump file export.
|
|---|
| 41 | * @since 7531
|
|---|
| 42 | */
|
|---|
| 43 | public class DownloadNotesTask extends AbstractDownloadTask<NoteData> {
|
|---|
| 44 |
|
|---|
| 45 | /** Property defining the number of notes to be downloaded */
|
|---|
| 46 | public static final IntegerProperty DOWNLOAD_LIMIT = new IntegerProperty("osm.notes.downloadLimit", 1000);
|
|---|
| 47 | /** Property defining number of days a bug needs to be closed to no longer be downloaded */
|
|---|
| 48 | public static final IntegerProperty DAYS_CLOSED = new IntegerProperty("osm.notes.daysClosed", 7);
|
|---|
| 49 |
|
|---|
| 50 | private static final String PATTERN_COMPRESS = "https?://.*/(.*\\.osn.(gz|xz|bz2?|zip))";
|
|---|
| 51 | private static final String NO_NOTES_FOUND = tr("No notes found in this area.");
|
|---|
| 52 | static {
|
|---|
| 53 | PostDownloadHandler.addNoDataErrorMessage(NO_NOTES_FOUND);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | private DownloadTask downloadTask;
|
|---|
| 57 | private NoteLayer noteLayer;
|
|---|
| 58 |
|
|---|
| 59 | /** This allows subclasses to ignore this warning */
|
|---|
| 60 | protected boolean warnAboutEmptyArea = true;
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Download a specific note by its id.
|
|---|
| 64 | * @param id Note identifier
|
|---|
| 65 | * @param progressMonitor progress monitor
|
|---|
| 66 | * @return the future representing the asynchronous task
|
|---|
| 67 | */
|
|---|
| 68 | public Future<?> download(long id, ProgressMonitor progressMonitor) {
|
|---|
| 69 | final String url = OsmApi.getOsmApi().getBaseUrl() + "notes/" + id;
|
|---|
| 70 | downloadTask = new DownloadRawUrlTask(new OsmServerLocationReader(url), progressMonitor);
|
|---|
| 71 | return MainApplication.worker.submit(downloadTask);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | @Override
|
|---|
| 75 | public Future<?> download(DownloadParams settings, Bounds downloadArea, ProgressMonitor progressMonitor) {
|
|---|
| 76 | downloadTask = new DownloadBoundingBoxTask(downloadArea, progressMonitor);
|
|---|
| 77 | return MainApplication.worker.submit(downloadTask);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | @Override
|
|---|
| 81 | public Future<?> loadUrl(DownloadParams settings, String url, ProgressMonitor progressMonitor) {
|
|---|
| 82 | if (url.matches(PATTERN_COMPRESS)) {
|
|---|
| 83 | downloadTask = new DownloadCompressedRawUrlTask(new OsmServerLocationReader(url), progressMonitor, Compression.byExtension(url));
|
|---|
| 84 | } else {
|
|---|
| 85 | downloadTask = new DownloadRawUrlTask(new OsmServerLocationReader(url), progressMonitor);
|
|---|
| 86 | }
|
|---|
| 87 | return MainApplication.worker.submit(downloadTask);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | @Override
|
|---|
| 91 | public void cancel() {
|
|---|
| 92 | if (downloadTask != null) {
|
|---|
| 93 | downloadTask.cancel();
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | @Override
|
|---|
| 98 | public String getConfirmationMessage(URL url) {
|
|---|
| 99 | return null;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | @Override
|
|---|
| 103 | public String getTitle() {
|
|---|
| 104 | return tr("Download OSM Notes");
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | @Override
|
|---|
| 108 | public String[] getPatterns() {
|
|---|
| 109 | return patterns(NoteUrlPattern.class);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | @Override
|
|---|
| 113 | public boolean isSafeForRemotecontrolRequests() {
|
|---|
| 114 | return true;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | @Override
|
|---|
| 118 | public ProjectionBounds getDownloadProjectionBounds() {
|
|---|
| 119 | return noteLayer != null ? noteLayer.getViewProjectionBounds() : null;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | abstract class DownloadTask extends PleaseWaitRunnable {
|
|---|
| 123 | protected OsmServerReader reader;
|
|---|
| 124 | protected List<Note> notesData;
|
|---|
| 125 |
|
|---|
| 126 | DownloadTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
|
|---|
| 127 | super(tr("Downloading notes"), progressMonitor, false);
|
|---|
| 128 | this.reader = reader;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | @Override
|
|---|
| 132 | protected void finish() {
|
|---|
| 133 | rememberDownloadedData(new NoteData(notesData));
|
|---|
| 134 | if (isCanceled() || isFailed() || notesData == null) {
|
|---|
| 135 | return;
|
|---|
| 136 | }
|
|---|
| 137 | if (notesData.isEmpty()) {
|
|---|
| 138 | if (warnAboutEmptyArea) {
|
|---|
| 139 | rememberErrorMessage(NO_NOTES_FOUND);
|
|---|
| 140 | }
|
|---|
| 141 | return;
|
|---|
| 142 | }
|
|---|
| 143 | if (Logging.isDebugEnabled()) {
|
|---|
| 144 | Logging.debug("Notes downloaded: {0}", notesData.size());
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | noteLayer = new NoteLayer(notesData, tr("Notes"));
|
|---|
| 148 | if (this instanceof DownloadBoundingBoxTask && ((DownloadBoundingBoxTask) this).bounds != null) {
|
|---|
| 149 | noteLayer.getNoteData().addDataSource(new DataSource(((DownloadBoundingBoxTask) this).bounds, "OpenStreetMap server"));
|
|---|
| 150 | }
|
|---|
| 151 | NoteLayer l = MainApplication.getLayerManager().getNoteLayer();
|
|---|
| 152 | if (l != null) {
|
|---|
| 153 | l.mergeFrom(noteLayer);
|
|---|
| 154 | MapFrame map = MainApplication.getMap();
|
|---|
| 155 | if (map != null && zoomAfterDownload) {
|
|---|
| 156 | map.mapView.scheduleZoomTo(new ViewportData(noteLayer.getViewProjectionBounds()));
|
|---|
| 157 | }
|
|---|
| 158 | } else {
|
|---|
| 159 | MainApplication.getLayerManager().addLayer(noteLayer, zoomAfterDownload);
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | @Override
|
|---|
| 164 | protected void cancel() {
|
|---|
| 165 | setCanceled(true);
|
|---|
| 166 | if (reader != null) {
|
|---|
| 167 | reader.cancel();
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | @Override
|
|---|
| 172 | public abstract void realRun() throws IOException, SAXException, OsmTransferException;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | class DownloadBoundingBoxTask extends DownloadTask {
|
|---|
| 176 | private Bounds bounds;
|
|---|
| 177 |
|
|---|
| 178 | DownloadBoundingBoxTask(Bounds bounds, ProgressMonitor progressMonitor) {
|
|---|
| 179 | this(new BoundingBoxDownloader(bounds), progressMonitor);
|
|---|
| 180 | this.bounds = bounds;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | DownloadBoundingBoxTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
|
|---|
| 184 | super(reader, progressMonitor);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | @Override
|
|---|
| 188 | public void realRun() throws IOException, SAXException, OsmTransferException {
|
|---|
| 189 | if (isCanceled()) {
|
|---|
| 190 | return;
|
|---|
| 191 | }
|
|---|
| 192 | ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
|
|---|
| 193 | try {
|
|---|
| 194 | notesData = reader.parseNotes(DOWNLOAD_LIMIT.get(), DAYS_CLOSED.get(), subMonitor);
|
|---|
| 195 | } catch (MoreNotesException e) {
|
|---|
| 196 | Logging.debug(e);
|
|---|
| 197 | notesData = e.notes;
|
|---|
| 198 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), "<html>"
|
|---|
| 199 | + trn("{0} note has been downloaded.", "{0} notes have been downloaded.", e.limit, e.limit)
|
|---|
| 200 | + "<br>"
|
|---|
| 201 | + tr("Since the download limit was {0}, there might be more notes to download.", e.limit)
|
|---|
| 202 | + "<br>"
|
|---|
| 203 | + tr("Request a smaller area to make sure that all notes are being downloaded.")
|
|---|
| 204 | + "</html>",
|
|---|
| 205 | tr("More notes to download"), JOptionPane.INFORMATION_MESSAGE);
|
|---|
| 206 | } catch (OsmTransferException e) {
|
|---|
| 207 | if (isCanceled())
|
|---|
| 208 | return;
|
|---|
| 209 | rememberException(e);
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | class DownloadRawUrlTask extends DownloadTask {
|
|---|
| 215 |
|
|---|
| 216 | DownloadRawUrlTask(OsmServerReader reader, ProgressMonitor progressMonitor) {
|
|---|
| 217 | super(reader, progressMonitor);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | @Override
|
|---|
| 221 | public void realRun() throws IOException, SAXException, OsmTransferException {
|
|---|
| 222 | if (isCanceled()) {
|
|---|
| 223 | return;
|
|---|
| 224 | }
|
|---|
| 225 | ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
|
|---|
| 226 | try {
|
|---|
| 227 | notesData = reader.parseRawNotes(subMonitor);
|
|---|
| 228 | } catch (OsmTransferException e) {
|
|---|
| 229 | if (isCanceled())
|
|---|
| 230 | return;
|
|---|
| 231 | rememberException(e);
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | class DownloadCompressedRawUrlTask extends DownloadTask {
|
|---|
| 237 |
|
|---|
| 238 | private final Compression compression;
|
|---|
| 239 |
|
|---|
| 240 | DownloadCompressedRawUrlTask(OsmServerReader reader, ProgressMonitor progressMonitor, Compression compression) {
|
|---|
| 241 | super(reader, progressMonitor);
|
|---|
| 242 | this.compression = compression;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | @Override
|
|---|
| 246 | public void realRun() throws IOException, SAXException, OsmTransferException {
|
|---|
| 247 | if (isCanceled()) {
|
|---|
| 248 | return;
|
|---|
| 249 | }
|
|---|
| 250 | ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
|
|---|
| 251 | try {
|
|---|
| 252 | notesData = reader.parseRawNotes(subMonitor, compression);
|
|---|
| 253 | } catch (OsmTransferException e) {
|
|---|
| 254 | if (isCanceled())
|
|---|
| 255 | return;
|
|---|
| 256 | rememberException(e);
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|