1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.concurrent.Future;
|
---|
8 | import java.util.regex.Matcher;
|
---|
9 | import java.util.regex.Pattern;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.Bounds;
|
---|
13 | import org.openstreetmap.josm.data.Bounds.ParseMethod;
|
---|
14 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
15 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
16 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
17 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressTaskIds;
|
---|
21 | import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
---|
22 | import org.openstreetmap.josm.io.OsmServerLocationReader;
|
---|
23 | import org.openstreetmap.josm.io.OsmServerReader;
|
---|
24 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
25 | import org.xml.sax.SAXException;
|
---|
26 |
|
---|
27 | public class DownloadGpsTask extends AbstractDownloadTask {
|
---|
28 |
|
---|
29 | private DownloadTask downloadTask;
|
---|
30 |
|
---|
31 | private static final String PATTERN_TRACE_ID = "http://.*openstreetmap.org/trace/\\p{Digit}+/data";
|
---|
32 |
|
---|
33 | private static final String PATTERN_TRACKPOINTS_BBOX = "http://.*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*";
|
---|
34 |
|
---|
35 | private static final String PATTERN_EXTERNAL_GPX_SCRIPT = "http://.*exportgpx.*";
|
---|
36 | private static final String PATTERN_EXTERNAL_GPX_FILE = "http://.*/(.*\\.gpx)";
|
---|
37 |
|
---|
38 | protected String newLayerName = null;
|
---|
39 |
|
---|
40 | public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
|
---|
41 | downloadTask = new DownloadTask(newLayer,
|
---|
42 | new BoundingBoxDownloader(downloadArea), progressMonitor);
|
---|
43 | // We need submit instead of execute so we can wait for it to finish and get the error
|
---|
44 | // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
|
---|
45 | return Main.worker.submit(downloadTask);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
|
---|
49 | if (url != null && (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT) || url.matches(PATTERN_EXTERNAL_GPX_FILE))) {
|
---|
50 | downloadTask = new DownloadTask(newLayer,
|
---|
51 | new OsmServerLocationReader(url), progressMonitor);
|
---|
52 | // Extract .gpx filename from URL to set the new layer name
|
---|
53 | Matcher matcher = Pattern.compile(PATTERN_EXTERNAL_GPX_FILE).matcher(url);
|
---|
54 | newLayerName = matcher.matches() ? matcher.group(1) : null;
|
---|
55 | // We need submit instead of execute so we can wait for it to finish and get the error
|
---|
56 | // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
|
---|
57 | return Main.worker.submit(downloadTask);
|
---|
58 |
|
---|
59 | } else if (url != null && url.matches(PATTERN_TRACKPOINTS_BBOX)) {
|
---|
60 | String[] table = url.split("\\?|=|&");
|
---|
61 | for (int i = 0; i<table.length; i++) {
|
---|
62 | if (table[i].equals("bbox") && i<table.length-1 )
|
---|
63 | return download(newLayer, new Bounds(table[i+1], ",", ParseMethod.LEFT_BOTTOM_RIGHT_TOP), progressMonitor);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | return null;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* (non-Javadoc)
|
---|
70 | * @see org.openstreetmap.josm.actions.downloadtasks.DownloadTask#acceptsUrl(java.lang.String)
|
---|
71 | */
|
---|
72 | @Override
|
---|
73 | public boolean acceptsUrl(String url) {
|
---|
74 | return url != null && (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_TRACKPOINTS_BBOX)
|
---|
75 | || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT) || url.matches(PATTERN_EXTERNAL_GPX_FILE));
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void cancel() {
|
---|
79 | if (downloadTask != null) {
|
---|
80 | downloadTask.cancel();
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | class DownloadTask extends PleaseWaitRunnable {
|
---|
85 | private OsmServerReader reader;
|
---|
86 | private GpxData rawData;
|
---|
87 | private final boolean newLayer;
|
---|
88 |
|
---|
89 | public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) {
|
---|
90 | super(tr("Downloading GPS data"));
|
---|
91 | this.reader = reader;
|
---|
92 | this.newLayer = newLayer;
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override public void realRun() throws IOException, SAXException, OsmTransferException {
|
---|
96 | try {
|
---|
97 | if (isCanceled())
|
---|
98 | return;
|
---|
99 | rawData = reader.parseRawGps(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
100 | } catch(Exception e) {
|
---|
101 | if (isCanceled())
|
---|
102 | return;
|
---|
103 | if (e instanceof OsmTransferException) {
|
---|
104 | rememberException(e);
|
---|
105 | } else {
|
---|
106 | rememberException(new OsmTransferException(e));
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override protected void finish() {
|
---|
112 | if (isCanceled() || isFailed())
|
---|
113 | return;
|
---|
114 | if (rawData == null)
|
---|
115 | return;
|
---|
116 | String name = newLayerName != null ? newLayerName : tr("Downloaded GPX Data");
|
---|
117 | GpxLayer layer = new GpxLayer(rawData, name);
|
---|
118 | Layer x = findMergeLayer();
|
---|
119 | if (newLayer || x == null) {
|
---|
120 | Main.main.addLayer(layer);
|
---|
121 | } else {
|
---|
122 | x.mergeFrom(layer);
|
---|
123 | Main.map.repaint();
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | private Layer findMergeLayer() {
|
---|
128 | boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
|
---|
129 | if (!Main.isDisplayingMapView())
|
---|
130 | return null;
|
---|
131 | Layer active = Main.map.mapView.getActiveLayer();
|
---|
132 | if (active != null && active instanceof GpxLayer && (merge || ((GpxLayer)active).data.fromServer))
|
---|
133 | return active;
|
---|
134 | for (Layer l : Main.map.mapView.getAllLayers()) {
|
---|
135 | if (l instanceof GpxLayer && (merge || ((GpxLayer)l).data.fromServer))
|
---|
136 | return l;
|
---|
137 | }
|
---|
138 | return null;
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override protected void cancel() {
|
---|
142 | setCanceled(true);
|
---|
143 | if (reader != null) {
|
---|
144 | reader.cancel();
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public ProgressTaskId canRunInBackground() {
|
---|
150 | return ProgressTaskIds.DOWNLOAD_GPS;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|