source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java@ 6920

Last change on this file since 6920 was 6920, checked in by Don-vip, 10 years ago

fix #9778, fix #9806 - access OSM API and JOSM website in HTTPS by default + other HTTPS links where applicable + update CONTRIBUTION

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.net.URL;
8import java.util.concurrent.Future;
9import java.util.regex.Matcher;
10import java.util.regex.Pattern;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.Bounds;
14import org.openstreetmap.josm.data.Bounds.ParseMethod;
15import org.openstreetmap.josm.data.gpx.GpxData;
16import org.openstreetmap.josm.gui.PleaseWaitRunnable;
17import org.openstreetmap.josm.gui.layer.GpxLayer;
18import org.openstreetmap.josm.gui.layer.Layer;
19import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
21import org.openstreetmap.josm.gui.progress.ProgressTaskId;
22import org.openstreetmap.josm.gui.progress.ProgressTaskIds;
23import org.openstreetmap.josm.io.BoundingBoxDownloader;
24import org.openstreetmap.josm.io.GpxImporter;
25import org.openstreetmap.josm.io.GpxImporter.GpxImporterData;
26import org.openstreetmap.josm.io.OsmServerLocationReader;
27import org.openstreetmap.josm.io.OsmServerReader;
28import org.openstreetmap.josm.io.OsmTransferException;
29import org.openstreetmap.josm.tools.CheckParameterUtil;
30import org.xml.sax.SAXException;
31
32/**
33 * Task allowing to download GPS data.
34 */
35public class DownloadGpsTask extends AbstractDownloadTask {
36
37 private DownloadTask downloadTask;
38
39 private static final String PATTERN_TRACE_ID = "https?://.*(osm|openstreetmap).org/trace/\\p{Digit}+/data";
40
41 private static final String PATTERN_TRACKPOINTS_BBOX = "https?://.*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*";
42
43 private static final String PATTERN_EXTERNAL_GPX_SCRIPT = "https?://.*exportgpx.*";
44 private static final String PATTERN_EXTERNAL_GPX_FILE = "https?://.*/(.*\\.gpx)";
45
46 protected String newLayerName = null;
47
48 @Override
49 public String[] getPatterns() {
50 return new String[] {PATTERN_EXTERNAL_GPX_FILE, PATTERN_EXTERNAL_GPX_SCRIPT, PATTERN_TRACE_ID, PATTERN_TRACKPOINTS_BBOX};
51 }
52
53 @Override
54 public String getTitle() {
55 return tr("Download GPS");
56 }
57
58 @Override
59 public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
60 downloadTask = new DownloadTask(newLayer,
61 new BoundingBoxDownloader(downloadArea), progressMonitor);
62 // We need submit instead of execute so we can wait for it to finish and get the error
63 // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
64 return Main.worker.submit(downloadTask);
65 }
66
67 @Override
68 public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
69 CheckParameterUtil.ensureParameterNotNull(url, "url");
70 if (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT) || url.matches(PATTERN_EXTERNAL_GPX_FILE)) {
71 downloadTask = new DownloadTask(newLayer,
72 new OsmServerLocationReader(url), progressMonitor);
73 // Extract .gpx filename from URL to set the new layer name
74 Matcher matcher = Pattern.compile(PATTERN_EXTERNAL_GPX_FILE).matcher(url);
75 newLayerName = matcher.matches() ? matcher.group(1) : null;
76 // We need submit instead of execute so we can wait for it to finish and get the error
77 // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
78 return Main.worker.submit(downloadTask);
79
80 } else if (url.matches(PATTERN_TRACKPOINTS_BBOX)) {
81 String[] table = url.split("\\?|=|&");
82 for (int i = 0; i<table.length; i++) {
83 if (table[i].equals("bbox") && i<table.length-1 )
84 return download(newLayer, new Bounds(table[i+1], ",", ParseMethod.LEFT_BOTTOM_RIGHT_TOP), progressMonitor);
85 }
86 }
87 return null;
88 }
89
90 @Override
91 public void cancel() {
92 if (downloadTask != null) {
93 downloadTask.cancel();
94 }
95 }
96
97 class DownloadTask extends PleaseWaitRunnable {
98 private OsmServerReader reader;
99 private GpxData rawData;
100 private final boolean newLayer;
101
102 public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) {
103 super(tr("Downloading GPS data"));
104 this.reader = reader;
105 this.newLayer = newLayer;
106 }
107
108 @Override public void realRun() throws IOException, SAXException, OsmTransferException {
109 try {
110 if (isCanceled())
111 return;
112 ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
113 rawData = reader.parseRawGps(subMonitor);
114 } catch(Exception e) {
115 if (isCanceled())
116 return;
117 if (e instanceof OsmTransferException) {
118 rememberException(e);
119 } else {
120 rememberException(new OsmTransferException(e));
121 }
122 }
123 }
124
125 @Override protected void finish() {
126 if (isCanceled() || isFailed())
127 return;
128 if (rawData == null)
129 return;
130 String name = newLayerName != null ? newLayerName : tr("Downloaded GPX Data");
131
132 GpxImporterData layers = GpxImporter.loadLayers(rawData, reader.isGpxParsedProperly(), name, tr("Markers from {0}", name));
133
134 GpxLayer gpxLayer = addOrMergeLayer(layers.getGpxLayer(), findGpxMergeLayer());
135 addOrMergeLayer(layers.getMarkerLayer(), findMarkerMergeLayer(gpxLayer));
136
137 layers.getPostLayerTask().run();
138 }
139
140 private <L extends Layer> L addOrMergeLayer(L layer, L mergeLayer) {
141 if (layer == null) return null;
142 if (newLayer || mergeLayer == null) {
143 Main.main.addLayer(layer);
144 return layer;
145 } else {
146 mergeLayer.mergeFrom(layer);
147 Main.map.repaint();
148 return mergeLayer;
149 }
150 }
151
152 private GpxLayer findGpxMergeLayer() {
153 if (!Main.isDisplayingMapView())
154 return null;
155 boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
156 Layer active = Main.map.mapView.getActiveLayer();
157 if (active instanceof GpxLayer && (merge || ((GpxLayer)active).data.fromServer))
158 return (GpxLayer) active;
159 for (GpxLayer l : Main.map.mapView.getLayersOfType(GpxLayer.class)) {
160 if (merge || l.data.fromServer)
161 return l;
162 }
163 return null;
164 }
165
166 private MarkerLayer findMarkerMergeLayer(GpxLayer fromLayer) {
167 if (!Main.isDisplayingMapView())
168 return null;
169 for (MarkerLayer l : Main.map.mapView.getLayersOfType(MarkerLayer.class)) {
170 if (fromLayer != null && l.fromLayer == fromLayer)
171 return l;
172 }
173 return null;
174 }
175
176 @Override protected void cancel() {
177 setCanceled(true);
178 if (reader != null) {
179 reader.cancel();
180 }
181 }
182
183 @Override
184 public ProgressTaskId canRunInBackground() {
185 return ProgressTaskIds.DOWNLOAD_GPS;
186 }
187 }
188
189 @Override
190 public String getConfirmationMessage(URL url) {
191 // TODO
192 return null;
193 }
194
195 /**
196 * Determines if the given URL denotes an OSM gpx-related API call.
197 * @param url The url to check
198 * @return true if the url matches "Trace ID" API call or "Trackpoints bbox" API call, false otherwise
199 * @see GpxData#fromServer
200 * @since 5745
201 */
202 public static final boolean isFromServer(String url) {
203 return url != null && (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_TRACKPOINTS_BBOX));
204 }
205}
Note: See TracBrowser for help on using the repository browser.