-
Notifications
You must be signed in to change notification settings - Fork 10
/
DownloadStrategy.java
151 lines (125 loc) · 5.81 KB
/
DownloadStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package org.openstreetmap.josm.plugins.continuosDownload;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.Future;
import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.actions.downloadtasks.AbstractDownloadTask;
import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.gui.layer.GpxLayer;
import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
import org.openstreetmap.josm.gui.progress.ProgressMonitor;
public abstract class DownloadStrategy {
public void fetch(Bounds bbox) {
this.fetch(bbox, OsmDataLayer.class);
this.fetch(bbox, GpxLayer.class);
}
public void fetch(Bounds bbox, Class<?> klass) {
Bounds extendedBox = extend(bbox, Main.pref.getDouble("plugin.continuos_download.extra_download", 0.1));
Collection<Bounds> existing = getExisting(klass);
if (existing.size() == 0)
return;
Collection<Bounds> toFetch = getBoxes(extendedBox, existing,
Main.pref.getInteger("plugin.continuos_download.max_areas", 4));
printDebug(extendedBox, toFetch);
// Try to avoid downloading areas outside the view area unnecessary
Collection<Bounds> t = toFetch;
toFetch = new ArrayList<Bounds>(t.size());
for (Bounds box : t) {
if (box.intersects(bbox)) {
toFetch.add(box);
}
}
download(toFetch, klass);
}
private void printDebug(Bounds bbox, Collection<Bounds> toFetch) {
double areaToDownload = 0;
for (Bounds box : toFetch) {
areaToDownload += box.getArea();
}
double areaDownloaded = 0;
for (Bounds box : getExisting(OsmDataLayer.class)) {
if (box.intersects(bbox))
areaDownloaded += intersection(box, bbox).getArea();
}
double downloadP = (areaToDownload * 100) / (bbox.getArea());
double downloadedP = (areaDownloaded * 100) / (bbox.getArea());
System.out.printf("Getting %.1f%% of area, already have %.1f%%, overlap %.1f%%%n", downloadP, downloadedP,
downloadP + downloadedP - 100);
}
private Bounds intersection(Bounds box1, Bounds box2) {
double minX1 = box1.getMin().getX();
double maxX1 = box1.getMax().getX();
double minY1 = box1.getMin().getY();
double maxY1 = box1.getMax().getY();
double minX2 = box2.getMin().getX();
double maxX2 = box2.getMax().getX();
double minY2 = box2.getMin().getY();
double maxY2 = box2.getMax().getY();
double minX = Math.max(minX1, minX2);
double maxX = Math.min(maxX1, maxX2);
double minY = Math.max(minY1, minY2);
double maxY = Math.min(maxY1, maxY2);
return new Bounds(minY, minX, maxY, maxX);
}
private Collection<Bounds> getExisting(Class<?> klass) {
if (klass.isAssignableFrom(OsmDataLayer.class)) {
OsmDataLayer layer = Main.map.mapView.getEditLayer();
if (layer == null) {
Collection<Layer> layers = Main.map.mapView.getAllLayersAsList();
for (Layer layer1 : layers) {
if (layer1 instanceof OsmDataLayer)
return ((OsmDataLayer) layer1).data.getDataSourceBounds();
}
return Collections.emptyList();
} else {
return layer.data.getDataSourceBounds();
}
} else if (klass.isAssignableFrom(GpxLayer.class)) {
if (!Main.isDisplayingMapView())
return null;
boolean merge = Main.pref.getBoolean("download.gps.mergeWithLocal", false);
Layer active = Main.map.mapView.getActiveLayer();
if (active instanceof GpxLayer && (merge || ((GpxLayer) active).data.fromServer))
return ((GpxLayer) active).data.getDataSourceBounds();
for (GpxLayer l : Main.map.mapView.getLayersOfType(GpxLayer.class)) {
if (merge || l.data.fromServer)
return l.data.getDataSourceBounds();
}
return Collections.emptyList();
} else {
throw new IllegalArgumentException();
}
}
public abstract Collection<Bounds> getBoxes(Bounds bbox, Collection<Bounds> present, int maxAreas);
private void download(Collection<Bounds> bboxes, Class<?> klass) {
for (Bounds bbox : bboxes) {
AbstractDownloadTask task = getDownloadTask(klass);
ProgressMonitor monitor = null;
if (Main.pref.getBoolean("plugin.continuos_download.quiet_download", false)) {
monitor = NullProgressMonitor.INSTANCE;
}
Future<?> future = task.download(false, bbox, monitor);
DownloadPlugin.worker.execute(new PostDownloadHandler(task, future));
}
}
private AbstractDownloadTask getDownloadTask(Class<?> klass) {
if (klass.isAssignableFrom(OsmDataLayer.class))
return new DownloadOsmTask2();
if (klass.isAssignableFrom(GpxLayer.class))
return new DownloadGpsTask();
throw new IllegalArgumentException();
}
static protected Bounds extend(Bounds bbox, double amount) {
LatLon min = bbox.getMin();
LatLon max = bbox.getMax();
double dLat = Math.abs(max.lat() - min.lat()) * amount;
double dLon = Math.abs(max.lon() - min.lon()) * amount;
return new Bounds(min.lat() - dLat, min.lon() - dLon, max.lat() + dLat, max.lon() + dLon);
}
}