Ignore:
Timestamp:
2011-12-29T18:07:04+01:00 (12 years ago)
Author:
jttt
Message:

Add precache wms tiles action to gpx layers (it will download wms tiles along track to cache for faster work afterwards)

Location:
trunk/src/org/openstreetmap/josm/io/imagery
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/imagery/Grabber.java

    r4126 r4745  
    44import org.openstreetmap.josm.Main;
    55import org.openstreetmap.josm.data.ProjectionBounds;
    6 import org.openstreetmap.josm.data.coor.EastNorth;
    76import org.openstreetmap.josm.data.imagery.GeorefImage.State;
    8 import org.openstreetmap.josm.data.projection.Projection;
    97import org.openstreetmap.josm.gui.MapView;
    108import org.openstreetmap.josm.gui.layer.WMSLayer;
     
    1311    protected final MapView mv;
    1412    protected final WMSLayer layer;
     13    private final boolean localOnly;
    1514
    1615    protected ProjectionBounds b;
    17     protected Projection proj;
    18     protected double pixelPerDegree;
    19     protected WMSRequest request;
    2016    protected volatile boolean canceled;
    2117
    22     Grabber(MapView mv, WMSLayer layer) {
     18    Grabber(MapView mv, WMSLayer layer, boolean localOnly) {
    2319        this.mv = mv;
    2420        this.layer = layer;
    25     }
    26 
    27     private void updateState(WMSRequest request) {
    28         b = new ProjectionBounds(
    29                 layer.getEastNorth(request.getXIndex(), request.getYIndex()),
    30                 layer.getEastNorth(request.getXIndex() + 1, request.getYIndex() + 1));
    31         if (WMSLayer.PROP_OVERLAP.get()) {
    32             double eastSize =  b.maxEast - b.minEast;
    33             double northSize =  b.maxNorth - b.minNorth;
    34 
    35             double eastCoef = WMSLayer.PROP_OVERLAP_EAST.get() / 100.0;
    36             double northCoef = WMSLayer.PROP_OVERLAP_NORTH.get() / 100.0;
    37 
    38             this.b = new ProjectionBounds(b.getMin(),
    39                     new EastNorth(b.maxEast + eastCoef * eastSize,
    40                             b.maxNorth + northCoef * northSize));
    41         }
    42 
    43         this.proj = Main.getProjection();
    44         this.pixelPerDegree = request.getPixelPerDegree();
    45         this.request = request;
     21        this.localOnly = localOnly;
    4622    }
    4723
     
    6036            if (canceled)
    6137                return;
    62             WMSRequest request = layer.getRequest();
     38            WMSRequest request = layer.getRequest(localOnly);
    6339            if (request == null)
    6440                return;
    65             updateState(request);
    66             if(!loadFromCache(request)){
    67                 attempt(request);
     41            this.b = layer.getBounds(request);
     42            if (request.isPrecacheOnly()) {
     43                if (!layer.cache.hasExactMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth)) {
     44                    attempt(request);
     45                }
     46            } else {
     47                if(!loadFromCache(request)){
     48                    attempt(request);
     49                }
    6850            }
    6951            layer.finishRequest(request);
     
    7759                return;
    7860            try {
    79                 if (!layer.requestIsValid(request))
     61                if (!request.isPrecacheOnly() && !layer.requestIsVisible(request))
    8062                    return;
    8163                fetch(request, i);
  • trunk/src/org/openstreetmap/josm/io/imagery/HTMLGrabber.java

    r4126 r4745  
    2222    public static final StringProperty PROP_BROWSER = new StringProperty("imagery.wms.browser", "webkit-image {0}");
    2323
    24     public HTMLGrabber(MapView mv, WMSLayer layer) {
    25         super(mv, layer);
     24    public HTMLGrabber(MapView mv, WMSLayer layer, boolean localOnly) {
     25        super(mv, layer, localOnly);
    2626    }
    2727
    2828    @Override
    29     protected BufferedImage grab(URL url, int attempt) throws IOException {
     29    protected BufferedImage grab(WMSRequest request, URL url, int attempt) throws IOException {
    3030        String urlstring = url.toExternalForm();
    3131
     
    5252        BufferedImage img = layer.normalizeImage(ImageIO.read(bais));
    5353        bais.reset();
    54         layer.cache.saveToCache(layer.isOverlapEnabled()?img:null, bais, Main.getProjection(), pixelPerDegree, b.minEast, b.minNorth);
     54        layer.cache.saveToCache(layer.isOverlapEnabled()?img:null, bais, Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth);
    5555
    5656        return img;
  • trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java

    r4432 r4745  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.io.imagery;
    3 
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    53
    64import java.awt.image.BufferedImage;
     
    1816import java.text.DecimalFormatSymbols;
    1917import java.text.NumberFormat;
    20 import java.util.ArrayList;
    21 import java.util.List;
    22 import java.util.Map.Entry;
     18import java.util.HashMap;
    2319import java.util.Locale;
    2420import java.util.Map;
    25 import java.util.HashMap;
     21import java.util.Map.Entry;
    2622import java.util.regex.Matcher;
    2723import java.util.regex.Pattern;
    2824
    2925import javax.imageio.ImageIO;
    30 import javax.swing.JOptionPane;
    3126
    3227import org.openstreetmap.josm.Main;
     
    5045    private Map<String, String> props = new HashMap<String, String>();
    5146
    52     public WMSGrabber(MapView mv, WMSLayer layer) {
    53         super(mv, layer);
     47    public WMSGrabber(MapView mv, WMSLayer layer, boolean localOnly) {
     48        super(mv, layer, localOnly);
    5449        this.info = layer.getInfo();
    5550        this.baseURL = info.getUrl();
     
    7772                    b.maxEast, b.maxNorth,
    7873                    width(), height());
    79             request.finish(State.IMAGE, grab(url, attempt));
     74            request.finish(State.IMAGE, grab(request, url, attempt));
    8075
    8176        } catch(Exception e) {
     
    10297
    10398        return new URL(baseURL.replaceAll("\\{proj(\\([^})]+\\))?\\}", myProj)
    104             .replaceAll("\\{bbox\\}", latLonFormat.format(w) + ","
    105                 + latLonFormat.format(s) + ","
    106                 + latLonFormat.format(e) + ","
    107                 + latLonFormat.format(n))
    108             .replaceAll("\\{w\\}", latLonFormat.format(w))
    109             .replaceAll("\\{s\\}", latLonFormat.format(s))
    110             .replaceAll("\\{e\\}", latLonFormat.format(e))
    111             .replaceAll("\\{n\\}", latLonFormat.format(n))
    112             .replaceAll("\\{width\\}", String.valueOf(wi))
    113             .replaceAll("\\{height\\}", String.valueOf(ht))
    114             .replace(" ", "%20"));
     99                .replaceAll("\\{bbox\\}", latLonFormat.format(w) + ","
     100                        + latLonFormat.format(s) + ","
     101                        + latLonFormat.format(e) + ","
     102                        + latLonFormat.format(n))
     103                        .replaceAll("\\{w\\}", latLonFormat.format(w))
     104                        .replaceAll("\\{s\\}", latLonFormat.format(s))
     105                        .replaceAll("\\{e\\}", latLonFormat.format(e))
     106                        .replaceAll("\\{n\\}", latLonFormat.format(n))
     107                        .replaceAll("\\{width\\}", String.valueOf(wi))
     108                        .replaceAll("\\{height\\}", String.valueOf(ht))
     109                        .replace(" ", "%20"));
    115110    }
    116111
    117112    @Override
    118113    public boolean loadFromCache(WMSRequest request) {
    119         BufferedImage cached = layer.cache.getExactMatch(Main.getProjection(), pixelPerDegree, b.minEast, b.minNorth);
     114        BufferedImage cached = layer.cache.getExactMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth);
    120115
    121116        if (cached != null) {
     
    123118            return true;
    124119        } else if (request.isAllowPartialCacheMatch()) {
    125             BufferedImage partialMatch = layer.cache.getPartialMatch(Main.getProjection(), pixelPerDegree, b.minEast, b.minNorth);
     120            BufferedImage partialMatch = layer.cache.getPartialMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth);
    126121            if (partialMatch != null) {
    127122                request.finish(State.PARTLY_IN_CACHE, partialMatch);
     
    138133    }
    139134
    140     protected BufferedImage grab(URL url, int attempt) throws IOException, OsmTransferException {
     135    protected BufferedImage grab(WMSRequest request, URL url, int attempt) throws IOException, OsmTransferException {
    141136        System.out.println("Grabbing WMS " + (attempt > 1? "(attempt " + attempt + ") ":"") + url);
    142137
     
    164159        BufferedImage img = layer.normalizeImage(ImageIO.read(bais));
    165160        bais.reset();
    166         layer.cache.saveToCache(layer.isOverlapEnabled()?img:null, bais, Main.getProjection(), pixelPerDegree, b.minEast, b.minNorth);
     161        layer.cache.saveToCache(layer.isOverlapEnabled()?img:null, bais, Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth);
    167162        return img;
    168163    }
  • trunk/src/org/openstreetmap/josm/io/imagery/WMSRequest.java

    r4065 r4745  
    55
    66import org.openstreetmap.josm.data.imagery.GeorefImage.State;
     7import org.openstreetmap.josm.gui.layer.WMSLayer.PrecacheTask;
    78
    89public class WMSRequest implements Comparable<WMSRequest> {
     
    1112    private final double pixelPerDegree;
    1213    private final boolean real; // Download even if autodownloading is disabled
     14    private final PrecacheTask precacheTask; // Download even when wms tile is not currently visible (precache)
    1315    private final boolean allowPartialCacheMatch;
    1416    private int priority;
     17    private boolean hasExactMatch;
    1518    // Result
    1619    private State state;
     
    1821
    1922    public WMSRequest(int xIndex, int yIndex, double pixelPerDegree, boolean real, boolean allowPartialCacheMatch) {
     23        this(xIndex, yIndex, pixelPerDegree, real, allowPartialCacheMatch, null);
     24    }
     25
     26    public WMSRequest(int xIndex, int yIndex, double pixelPerDegree, boolean real, boolean allowPartialCacheMatch, PrecacheTask precacheTask) {
    2027        this.xIndex = xIndex;
    2128        this.yIndex = yIndex;
    2229        this.pixelPerDegree = pixelPerDegree;
    2330        this.real = real;
     31        this.precacheTask = precacheTask;
    2432        this.allowPartialCacheMatch = allowPartialCacheMatch;
    2533    }
     34
    2635
    2736    public void finish(State state, BufferedImage image) {
     
    99108    public String toString() {
    100109        return "WMSRequest [xIndex=" + xIndex + ", yIndex=" + yIndex
    101         + ", pixelPerDegree=" + pixelPerDegree + "]";
     110                + ", pixelPerDegree=" + pixelPerDegree + "]";
    102111    }
    103112
     
    106115    }
    107116
     117    public boolean isPrecacheOnly() {
     118        return precacheTask != null;
     119    }
     120
     121    public PrecacheTask getPrecacheTask() {
     122        return precacheTask;
     123    }
     124
    108125    public boolean isAllowPartialCacheMatch() {
    109126        return allowPartialCacheMatch;
    110127    }
     128
     129    public boolean hasExactMatch() {
     130        return hasExactMatch;
     131    }
     132
     133    public void setHasExactMatch(boolean hasExactMatch) {
     134        this.hasExactMatch = hasExactMatch;
     135    }
    111136}
Note: See TracChangeset for help on using the changeset viewer.