Skip to content

Commit

Permalink
Add support for gpx layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnonthgol committed Oct 2, 2014
1 parent 727f873 commit 3cd1b31
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
public class BoxStrategy extends DownloadStrategy {

@Override
public Collection<Bounds> getBoxes(Bounds bbox, int maxBoxes) {
Collection<Box> existing = Box.merge(fromBounds(getExisting()));
public Collection<Bounds> getBoxes(Bounds bbox, Collection<Bounds> present, int maxBoxes) {
Collection<Box> existing = Box.merge(fromBounds(present));
Collection<Box> bits = Box.merge(fromBounds(bbox).subtract_all(existing));
Collection<Box> toFetch = optimalPart(maxBoxes, bits);
return toBounds(Box.merge(toFetch));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@
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.DataSource;
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> toFetch = getBoxes(extendedBox,
Collection<Bounds> toFetch = getBoxes(extendedBox, getExisting(klass),
Main.pref.getInteger("plugin.continuos_download.max_areas", 4));

printDebug(extendedBox, toFetch);
Expand All @@ -31,7 +40,7 @@ public void fetch(Bounds bbox) {
}
}

download(toFetch);
download(toFetch, klass);
}

private void printDebug(Bounds bbox, Collection<Bounds> toFetch) {
Expand All @@ -41,7 +50,7 @@ private void printDebug(Bounds bbox, Collection<Bounds> toFetch) {
}

double areaDownloaded = 0;
for (Bounds box : getExisting()) {
for (Bounds box : getExisting(OsmDataLayer.class)) {
if (box.intersects(bbox))
areaDownloaded += intersection(box, bbox).getArea();
}
Expand Down Expand Up @@ -72,23 +81,41 @@ private Bounds intersection(Bounds box1, Bounds box2) {
return new Bounds(minY, minX, maxY, maxX);
}

protected Collection<Bounds> getExisting() {
if (Main.map.mapView.getEditLayer() == null)
return Collections.emptySet();
ArrayList<Bounds> r = new ArrayList<Bounds>();

for (DataSource dataSource : Main.map.mapView.getEditLayer().data.dataSources) {
r.add(dataSource.bounds);
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();
}

return r;
}

public abstract Collection<Bounds> getBoxes(Bounds bbox, int maxAreas);
public abstract Collection<Bounds> getBoxes(Bounds bbox, Collection<Bounds> present, int maxAreas);

protected void download(Collection<Bounds> bboxes) {
private void download(Collection<Bounds> bboxes, Class<?> klass) {
for (Bounds bbox : bboxes) {
DownloadOsmTask2 task = new DownloadOsmTask2();
AbstractDownloadTask task = getDownloadTask(klass);

ProgressMonitor monitor = null;
if (Main.pref.getBoolean("plugin.continuos_download.quiet_download", false)) {
Expand All @@ -100,6 +127,14 @@ protected void download(Collection<Bounds> bboxes) {
}
}

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class SimpleStrategy extends DownloadStrategy {

@Override
public Collection<Bounds> getBoxes(Bounds bbox, int maxBoxes) {
public Collection<Bounds> getBoxes(Bounds bbox, Collection<Bounds> present, int maxBoxes) {
return Collections.singleton(bbox);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public void test2() {
Collection<Bounds> existing = new ArrayList<Bounds>();
existing.add(new Bounds(0, 0, 1, 1));

BoxStrategy strat = new Scaffold(existing);
BoxStrategy strat = new BoxStrategy();

Collection<Bounds> r = strat.getBoxes(new Bounds(0, -1, 1, 2), 3);
Collection<Bounds> r = strat.getBoxes(new Bounds(0, -1, 1, 2), existing, 3);

assertEquals(2, r.size());
for (Bounds b : r) {
Expand All @@ -58,17 +58,4 @@ public void testStress() {
assertTrue(System.currentTimeMillis() < t0 + 2000);
}

static class Scaffold extends BoxStrategy {
Collection<Bounds> existing;

Scaffold(Collection<Bounds> e) {
existing = e;
}

@Override
protected Collection<Bounds> getExisting() {
return existing;
}
}

}

0 comments on commit 3cd1b31

Please sign in to comment.