001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions.downloadtasks;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.IOException;
007import java.net.URL;
008import java.util.concurrent.Future;
009import java.util.regex.Matcher;
010import java.util.regex.Pattern;
011
012import org.openstreetmap.josm.Main;
013import org.openstreetmap.josm.data.Bounds;
014import org.openstreetmap.josm.data.Bounds.ParseMethod;
015import org.openstreetmap.josm.data.gpx.GpxData;
016import org.openstreetmap.josm.gui.PleaseWaitRunnable;
017import org.openstreetmap.josm.gui.layer.GpxLayer;
018import org.openstreetmap.josm.gui.layer.Layer;
019import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
020import org.openstreetmap.josm.gui.progress.ProgressMonitor;
021import org.openstreetmap.josm.gui.progress.ProgressTaskId;
022import org.openstreetmap.josm.gui.progress.ProgressTaskIds;
023import org.openstreetmap.josm.io.BoundingBoxDownloader;
024import org.openstreetmap.josm.io.GpxImporter;
025import org.openstreetmap.josm.io.GpxImporter.GpxImporterData;
026import org.openstreetmap.josm.io.OsmServerLocationReader;
027import org.openstreetmap.josm.io.OsmServerReader;
028import org.openstreetmap.josm.io.OsmTransferException;
029import org.openstreetmap.josm.tools.CheckParameterUtil;
030import org.xml.sax.SAXException;
031
032/**
033 * Task allowing to download GPS data.
034 */
035public class DownloadGpsTask extends AbstractDownloadTask {
036
037    private DownloadTask downloadTask;
038
039    private static final String PATTERN_TRACE_ID = "https?://.*(osm|openstreetmap).org/trace/\\p{Digit}+/data";
040
041    private static final String PATTERN_TRACKPOINTS_BBOX = "https?://.*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*";
042
043    private static final String PATTERN_EXTERNAL_GPX_SCRIPT = "https?://.*exportgpx.*";
044    private static final String PATTERN_EXTERNAL_GPX_FILE = "https?://.*/(.*\\.gpx)";
045
046    protected String newLayerName = null;
047
048    @Override
049    public String[] getPatterns() {
050        return new String[] {PATTERN_EXTERNAL_GPX_FILE, PATTERN_EXTERNAL_GPX_SCRIPT, PATTERN_TRACE_ID, PATTERN_TRACKPOINTS_BBOX};
051    }
052
053    @Override
054    public String getTitle() {
055        return tr("Download GPS");
056    }
057
058    @Override
059    public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
060        downloadTask = new DownloadTask(newLayer,
061                new BoundingBoxDownloader(downloadArea), progressMonitor);
062        // We need submit instead of execute so we can wait for it to finish and get the error
063        // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
064        return Main.worker.submit(downloadTask);
065    }
066
067    @Override
068    public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
069        CheckParameterUtil.ensureParameterNotNull(url, "url");
070        if (url.matches(PATTERN_TRACE_ID)
071         || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT)
072         || url.matches(PATTERN_EXTERNAL_GPX_FILE)) {
073            downloadTask = new DownloadTask(newLayer,
074                    new OsmServerLocationReader(url), progressMonitor);
075            // Extract .gpx filename from URL to set the new layer name
076            Matcher matcher = Pattern.compile(PATTERN_EXTERNAL_GPX_FILE).matcher(url);
077            newLayerName = matcher.matches() ? matcher.group(1) : null;
078            // We need submit instead of execute so we can wait for it to finish and get the error
079            // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
080            return Main.worker.submit(downloadTask);
081
082        } else if (url.matches(PATTERN_TRACKPOINTS_BBOX)) {
083            String[] table = url.split("\\?|=|&");
084            for (int i = 0; i<table.length; i++) {
085                if ("bbox".equals(table[i]) && i<table.length-1 )
086                    return download(newLayer, new Bounds(table[i+1], ",", ParseMethod.LEFT_BOTTOM_RIGHT_TOP), progressMonitor);
087            }
088        }
089        return null;
090    }
091
092    @Override
093    public void cancel() {
094        if (downloadTask != null) {
095            downloadTask.cancel();
096        }
097    }
098
099    class DownloadTask extends PleaseWaitRunnable {
100        private OsmServerReader reader;
101        private GpxData rawData;
102        private final boolean newLayer;
103
104        public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) {
105            super(tr("Downloading GPS data"), progressMonitor, false);
106            this.reader = reader;
107            this.newLayer = newLayer;
108        }
109
110        @Override
111        public void realRun() throws IOException, SAXException, OsmTransferException {
112            try {
113                if (isCanceled())
114                    return;
115                ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
116                rawData = reader.parseRawGps(subMonitor);
117            } catch(Exception e) {
118                if (isCanceled())
119                    return;
120                if (e instanceof OsmTransferException) {
121                    rememberException(e);
122                } else {
123                    rememberException(new OsmTransferException(e));
124                }
125            }
126        }
127
128        @Override
129        protected void finish() {
130            if (isCanceled() || isFailed())
131                return;
132            if (rawData == null)
133                return;
134            String name = newLayerName != null ? newLayerName : tr("Downloaded GPX Data");
135
136            GpxImporterData layers = GpxImporter.loadLayers(rawData, reader.isGpxParsedProperly(), name,
137                    tr("Markers from {0}", name));
138
139            GpxLayer gpxLayer = addOrMergeLayer(layers.getGpxLayer(), findGpxMergeLayer());
140            addOrMergeLayer(layers.getMarkerLayer(), findMarkerMergeLayer(gpxLayer));
141
142            layers.getPostLayerTask().run();
143        }
144
145        private <L extends Layer> L addOrMergeLayer(L layer, L mergeLayer) {
146            if (layer == null) return null;
147            if (newLayer || mergeLayer == null) {
148                Main.main.addLayer(layer);
149                return layer;
150            } else {
151                mergeLayer.mergeFrom(layer);
152                Main.map.repaint();
153                return mergeLayer;
154            }
155        }
156
157        private GpxLayer findGpxMergeLayer() {
158            if (!Main.isDisplayingMapView())
159                return null;
160            boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
161            Layer active = Main.map.mapView.getActiveLayer();
162            if (active instanceof GpxLayer && (merge || ((GpxLayer)active).data.fromServer))
163                return (GpxLayer) active;
164            for (GpxLayer l : Main.map.mapView.getLayersOfType(GpxLayer.class)) {
165                if (merge || l.data.fromServer)
166                    return l;
167            }
168            return null;
169        }
170
171        private MarkerLayer findMarkerMergeLayer(GpxLayer fromLayer) {
172            if (!Main.isDisplayingMapView())
173                return null;
174            for (MarkerLayer l : Main.map.mapView.getLayersOfType(MarkerLayer.class)) {
175                if (fromLayer != null && l.fromLayer == fromLayer)
176                    return l;
177            }
178            return null;
179        }
180
181        @Override protected void cancel() {
182            setCanceled(true);
183            if (reader != null) {
184                reader.cancel();
185            }
186        }
187
188        @Override
189        public ProgressTaskId canRunInBackground() {
190            return ProgressTaskIds.DOWNLOAD_GPS;
191        }
192    }
193
194    @Override
195    public String getConfirmationMessage(URL url) {
196        // TODO
197        return null;
198    }
199
200    @Override
201    public boolean isSafeForRemotecontrolRequests() {
202        return true;
203    }
204
205    /**
206     * Determines if the given URL denotes an OSM gpx-related API call.
207     * @param url The url to check
208     * @return true if the url matches "Trace ID" API call or "Trackpoints bbox" API call, false otherwise
209     * @see GpxData#fromServer
210     * @since 5745
211     */
212    public static final boolean isFromServer(String url) {
213        return url != null && (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_TRACKPOINTS_BBOX));
214    }
215}