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<GpxData> { 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; 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 final OsmServerReader reader; 101 private GpxData rawData; 102 private final boolean newLayer; 103 104 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 rememberDownloadedData(rawData); 131 if (rawData == null) 132 return; 133 String name = newLayerName != null ? newLayerName : tr("Downloaded GPX Data"); 134 135 GpxImporterData layers = GpxImporter.loadLayers(rawData, reader.isGpxParsedProperly(), name, 136 tr("Markers from {0}", name)); 137 138 GpxLayer gpxLayer = addOrMergeLayer(layers.getGpxLayer(), findGpxMergeLayer()); 139 addOrMergeLayer(layers.getMarkerLayer(), findMarkerMergeLayer(gpxLayer)); 140 141 layers.getPostLayerTask().run(); 142 } 143 144 private <L extends Layer> L addOrMergeLayer(L layer, L mergeLayer) { 145 if (layer == null) return null; 146 if (newLayer || mergeLayer == null) { 147 if (Main.main != null) { 148 Main.main.addLayer(layer); 149 } 150 return layer; 151 } else { 152 mergeLayer.mergeFrom(layer); 153 if (Main.map != null) { 154 Main.map.repaint(); 155 } 156 return mergeLayer; 157 } 158 } 159 160 private GpxLayer findGpxMergeLayer() { 161 if (!Main.isDisplayingMapView()) 162 return null; 163 boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false); 164 Layer active = Main.map.mapView.getActiveLayer(); 165 if (active instanceof GpxLayer && (merge || ((GpxLayer) active).data.fromServer)) 166 return (GpxLayer) active; 167 for (GpxLayer l : Main.map.mapView.getLayersOfType(GpxLayer.class)) { 168 if (merge || l.data.fromServer) 169 return l; 170 } 171 return null; 172 } 173 174 private MarkerLayer findMarkerMergeLayer(GpxLayer fromLayer) { 175 if (!Main.isDisplayingMapView()) 176 return null; 177 for (MarkerLayer l : Main.map.mapView.getLayersOfType(MarkerLayer.class)) { 178 if (fromLayer != null && l.fromLayer == fromLayer) 179 return l; 180 } 181 return null; 182 } 183 184 @Override 185 protected void cancel() { 186 setCanceled(true); 187 if (reader != null) { 188 reader.cancel(); 189 } 190 } 191 192 @Override 193 public ProgressTaskId canRunInBackground() { 194 return ProgressTaskIds.DOWNLOAD_GPS; 195 } 196 } 197 198 @Override 199 public String getConfirmationMessage(URL url) { 200 // TODO 201 return null; 202 } 203 204 @Override 205 public boolean isSafeForRemotecontrolRequests() { 206 return true; 207 } 208 209 /** 210 * Determines if the given URL denotes an OSM gpx-related API call. 211 * @param url The url to check 212 * @return true if the url matches "Trace ID" API call or "Trackpoints bbox" API call, false otherwise 213 * @see GpxData#fromServer 214 * @since 5745 215 */ 216 public static final boolean isFromServer(String url) { 217 return url != null && (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_TRACKPOINTS_BBOX)); 218 } 219}