| 1 | package wmsplugin;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.io.IOException;
|
|---|
| 6 | import java.util.*;
|
|---|
| 7 | import org.openstreetmap.josm.Main;
|
|---|
| 8 | import org.openstreetmap.josm.actions.DownloadAction;
|
|---|
| 9 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | public class DownloadWMSTask extends PleaseWaitRunnable {
|
|---|
| 14 |
|
|---|
| 15 | private WMSLayer wmsLayer;
|
|---|
| 16 | private double minlat, minlon, maxlat, maxlon;
|
|---|
| 17 |
|
|---|
| 18 | /* whether our layer was already added. */
|
|---|
| 19 | private boolean layerAdded = false;
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | public DownloadWMSTask(String name, String wmsurl) {
|
|---|
| 23 |
|
|---|
| 24 | super(tr("Downloading " + name));
|
|---|
| 25 |
|
|---|
| 26 | // simply check if we already have a layer created. if not, create; if yes, reuse.
|
|---|
| 27 |
|
|---|
| 28 | if (wmsLayer == null) {
|
|---|
| 29 |
|
|---|
| 30 | if (wmsurl.matches("(?i).*layers=npeoocmap.*") || wmsurl.matches("(?i).*layers=npe.*") ){
|
|---|
| 31 | //then we use the OSGBLayer
|
|---|
| 32 | this.wmsLayer= new OSGBLayer(name, wmsurl);
|
|---|
| 33 | } else {
|
|---|
| 34 | this.wmsLayer = new WMSLayer(name, wmsurl);
|
|---|
| 35 |
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | @Override public void realRun() throws IOException {
|
|---|
| 41 | wmsLayer.grab(minlat,minlon,maxlat,maxlon);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | @Override protected void finish() {
|
|---|
| 45 |
|
|---|
| 46 | // BUG if layer is deleted, wmsLayer is not null and layerAdded remains true!
|
|---|
| 47 | // FIXED, see below
|
|---|
| 48 |
|
|---|
| 49 | layerAdded = false;
|
|---|
| 50 | for (Iterator it = Main.map.mapView.getAllLayers().iterator(); it.hasNext(); ) {
|
|---|
| 51 | Object element = it.next();
|
|---|
| 52 |
|
|---|
| 53 | if (element.equals(wmsLayer)) layerAdded = true;
|
|---|
| 54 |
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | if ((wmsLayer != null) && (!layerAdded))
|
|---|
| 59 | {
|
|---|
| 60 | Main.main.addLayer(wmsLayer);
|
|---|
| 61 | layerAdded = true;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | @Override protected void cancel() {
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | public void download(DownloadAction action, double minlat, double minlon, double maxlat, double maxlon) {
|
|---|
| 69 | this.minlat=minlat;
|
|---|
| 70 | this.minlon=minlon;
|
|---|
| 71 | this.maxlat=maxlat;
|
|---|
| 72 | this.maxlon=maxlon;
|
|---|
| 73 | Main.worker.execute(this);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|