Ticket #2310: wms.patch

File wms.patch, 4.5 KB (added by xeen, 16 years ago)

I still couldn't find the time to put it into a real core class that other plugins may use as well. This is meant as an intermediate fix to make WMS more user friendly. Fixes #2310 (this one), #2307 (blank tiles) and probably #2350 (null pointer exception)

  • wmsplugin/src/wmsplugin/Cache.java

     
    77import java.security.MessageDigest;
    88import java.util.Date;
    99import java.util.Iterator;
     10import java.util.Random;
    1011import java.util.Set;
    1112import java.util.TreeMap;
    1213import javax.imageio.*;
     
    2324    private boolean disabled = false;
    2425    // If the cache is full, we don't want to delete just one file
    2526    private final int cleanUpThreshold = 20;
     27    // After how many file-writes do we want to check if the cache needs emptying?
     28    private final int cleanUpInterval = 5;
    2629
    2730    Cache() {
    2831        this(Main.pref.get("wmsplugin.cache.path", WMSPlugin.getPrefsPath() + "cache"));
     
    7275            System.out.println(e.getMessage());
    7376        }
    7477
    75         // Clean up must be called manually
     78        checkCleanUp();
    7679    }
    7780
    7881    public BufferedImage saveImg(String ident, BufferedImage image, boolean passThrough) {
     
    8083        return image;
    8184    }
    8285
     86    public void checkCleanUp() {
     87        // The Cache class isn't persistent in its current implementation,
     88        // therefore clean up on random intervals, but not every write
     89        if(new Random().nextInt(cleanUpInterval) == 0)
     90            cleanUp();
     91    }
     92
    8393    public void cleanUp() {
    8494        if(disabled) return;
    8595
     
    111121        }
    112122    }
    113123
     124    public void deleteSmallFiles(int size) {
     125        if(disabled) return;
     126        for(File f : getFiles()) {
     127            if(f.length() < size)
     128                f.delete();
     129        }
     130    }
     131
    114132    private long getDirSize() {
     133        if(disabled) return -1;
    115134        long dirsize = 0;
    116135
    117136        for(File f : getFiles())
     
    120139    }
    121140
    122141    private File[] getFiles() {
     142        if(disabled) return null;
    123143        return dir.listFiles(
    124144            new FileFilter() {
    125145                public boolean accept(File file) {
  • wmsplugin/src/wmsplugin/WMSLayer.java

     
    7979        WMSGrabber.getProjection(baseURL, true);
    8080        mv = Main.map.mapView;
    8181        getPPD();
    82        
     82
    8383        executor = Executors.newFixedThreadPool(3);
    8484    }
    85    
     85
    8686    public void destroy() {
    87         try { 
    88             executor.shutdown(); 
     87        try {
     88            executor.shutdown();
    8989        // Might not be initalized, so catch NullPointer as well
    9090        } catch(Exception x) {}
    9191    }
     
    171171            JOptionPane.showMessageDialog(Main.parent, tr("The requested area is too big. Please zoom in a little, or change resolution"));
    172172            return;
    173173        }
    174        
     174
    175175        for(int x = bminx; x<bmaxx; ++x)
    176176            for(int y = bminy; y<bmaxy; ++y){
    177177                GeorefImage img = images[modulo(x,dax)][modulo(y,day)];
     
    183183                    executor.submit(gr);
    184184            }
    185185        }
    186 
    187         new wmsplugin.Cache().cleanUp();
    188186    }
    189187
    190188    @Override public void visitBoundingBox(BoundingXYVisitor v) {
     
    247245            mv.repaint();
    248246        }
    249247    }
    250    
     248
    251249    public class reloadErrorTilesAction extends AbstractAction {
    252250        public reloadErrorTilesAction() {
    253251            super(tr("Reload erroneous tiles"));
    254252        }
    255253        public void actionPerformed(ActionEvent ev) {
     254            // Delete small files, because they're probably blank tiles.
     255            // See https://josm.openstreetmap.de/ticket/2307
     256            new wmsplugin.Cache().deleteSmallFiles(2048);
     257
    256258            for (int x = 0; x < dax; ++x) {
    257259                for (int y = 0; y < day; ++y) {
    258260                    GeorefImage img = images[modulo(x,dax)][modulo(y,day)];
     
    265267            }
    266268        }
    267269    }
    268    
     270
    269271    public class ToggleAlphaAction extends AbstractAction {
    270272        public ToggleAlphaAction() {
    271273            super(tr("Alpha channel"));
     
    274276            JCheckBoxMenuItem checkbox = (JCheckBoxMenuItem) ev.getSource();
    275277            boolean alphaChannel = checkbox.isSelected();
    276278            Main.pref.put("wmsplugin.alpha_channel", alphaChannel);
    277            
     279
    278280            // clear all resized cached instances and repaint the layer
    279281            for (int x = 0; x < dax; ++x) {
    280282                for (int y = 0; y < day; ++y) {