001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Color;
007import java.awt.Font;
008import java.awt.Graphics;
009import java.awt.Graphics2D;
010import java.awt.Image;
011import java.awt.Point;
012import java.awt.Rectangle;
013import java.awt.Toolkit;
014import java.awt.event.ActionEvent;
015import java.awt.event.MouseAdapter;
016import java.awt.event.MouseEvent;
017import java.awt.image.ImageObserver;
018import java.io.File;
019import java.io.IOException;
020import java.io.StringReader;
021import java.net.URL;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.LinkedList;
026import java.util.List;
027import java.util.Map;
028import java.util.Map.Entry;
029import java.util.Scanner;
030import java.util.Set;
031import java.util.concurrent.Callable;
032import java.util.regex.Matcher;
033import java.util.regex.Pattern;
034
035import javax.swing.AbstractAction;
036import javax.swing.Action;
037import javax.swing.JCheckBoxMenuItem;
038import javax.swing.JMenuItem;
039import javax.swing.JOptionPane;
040import javax.swing.JPopupMenu;
041
042import org.openstreetmap.gui.jmapviewer.AttributionSupport;
043import org.openstreetmap.gui.jmapviewer.Coordinate;
044import org.openstreetmap.gui.jmapviewer.JobDispatcher;
045import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
046import org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader;
047import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
048import org.openstreetmap.gui.jmapviewer.Tile;
049import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader;
050import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
051import org.openstreetmap.gui.jmapviewer.interfaces.TileClearController;
052import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
053import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
054import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
055import org.openstreetmap.gui.jmapviewer.tilesources.ScanexTileSource;
056import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
057import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
058import org.openstreetmap.josm.Main;
059import org.openstreetmap.josm.actions.RenameLayerAction;
060import org.openstreetmap.josm.data.Bounds;
061import org.openstreetmap.josm.data.Version;
062import org.openstreetmap.josm.data.coor.EastNorth;
063import org.openstreetmap.josm.data.coor.LatLon;
064import org.openstreetmap.josm.data.imagery.ImageryInfo;
065import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
066import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
067import org.openstreetmap.josm.data.preferences.BooleanProperty;
068import org.openstreetmap.josm.data.preferences.IntegerProperty;
069import org.openstreetmap.josm.data.preferences.StringProperty;
070import org.openstreetmap.josm.data.projection.Projection;
071import org.openstreetmap.josm.gui.MapFrame;
072import org.openstreetmap.josm.gui.MapView;
073import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
074import org.openstreetmap.josm.gui.PleaseWaitRunnable;
075import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
076import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
077import org.openstreetmap.josm.gui.progress.ProgressMonitor;
078import org.openstreetmap.josm.gui.progress.ProgressMonitor.CancelListener;
079import org.openstreetmap.josm.io.CacheCustomContent;
080import org.openstreetmap.josm.io.OsmTransferException;
081import org.openstreetmap.josm.io.UTFInputStreamReader;
082import org.openstreetmap.josm.tools.Utils;
083import org.xml.sax.InputSource;
084import org.xml.sax.SAXException;
085
086/**
087 * Class that displays a slippy map layer.
088 *
089 * @author Frederik Ramm
090 * @author LuVar <lubomir.varga@freemap.sk>
091 * @author Dave Hansen <dave@sr71.net>
092 * @author Upliner <upliner@gmail.com>
093 *
094 */
095public class TMSLayer extends ImageryLayer implements ImageObserver, TileLoaderListener {
096    public static final String PREFERENCE_PREFIX   = "imagery.tms";
097
098    public static final int MAX_ZOOM = 30;
099    public static final int MIN_ZOOM = 2;
100    public static final int DEFAULT_MAX_ZOOM = 20;
101    public static final int DEFAULT_MIN_ZOOM = 2;
102
103    public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
104    public static final BooleanProperty PROP_DEFAULT_AUTOLOAD = new BooleanProperty(PREFERENCE_PREFIX + ".default_autoload", true);
105    public static final BooleanProperty PROP_DEFAULT_SHOWERRORS = new BooleanProperty(PREFERENCE_PREFIX + ".default_showerrors", true);
106    public static final IntegerProperty PROP_MIN_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".min_zoom_lvl", DEFAULT_MIN_ZOOM);
107    public static final IntegerProperty PROP_MAX_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".max_zoom_lvl", DEFAULT_MAX_ZOOM);
108    //public static final BooleanProperty PROP_DRAW_DEBUG = new BooleanProperty(PREFERENCE_PREFIX + ".draw_debug", false);
109    public static final BooleanProperty PROP_ADD_TO_SLIPPYMAP_CHOOSER = new BooleanProperty(PREFERENCE_PREFIX + ".add_to_slippymap_chooser", true);
110    public static final IntegerProperty PROP_TMS_JOBS = new IntegerProperty("tmsloader.maxjobs", 25);
111    public static final StringProperty PROP_TILECACHE_DIR;
112
113    static {
114        String defPath = null;
115        try {
116            defPath = OsmFileCacheTileLoader.getDefaultCacheDir().getAbsolutePath();
117        } catch (SecurityException e) {
118            Main.warn(e);
119        }
120        PROP_TILECACHE_DIR = new StringProperty(PREFERENCE_PREFIX + ".tilecache_path", defPath);
121    }
122
123    public interface TileLoaderFactory {
124        OsmTileLoader makeTileLoader(TileLoaderListener listener);
125    }
126
127    protected MemoryTileCache tileCache;
128    protected TileSource tileSource;
129    protected OsmTileLoader tileLoader;
130
131    public static TileLoaderFactory loaderFactory = new TileLoaderFactory() {
132        @Override
133        public OsmTileLoader makeTileLoader(TileLoaderListener listener) {
134            String cachePath = TMSLayer.PROP_TILECACHE_DIR.get();
135            if (cachePath != null && !cachePath.isEmpty()) {
136                try {
137                    OsmFileCacheTileLoader loader = new OsmFileCacheTileLoader(listener, new File(cachePath));
138                    loader.headers.put("User-Agent", Version.getInstance().getFullAgentString());
139                    return loader;
140                } catch (IOException e) {
141                    Main.warn(e);
142                }
143            }
144            return null;
145        }
146    };
147
148    /**
149     * Plugins that wish to set custom tile loader should call this method
150     */
151    public static void setCustomTileLoaderFactory(TileLoaderFactory loaderFactory) {
152        TMSLayer.loaderFactory = loaderFactory;
153    }
154
155    private Set<Tile> tileRequestsOutstanding = new HashSet<>();
156
157    @Override
158    public synchronized void tileLoadingFinished(Tile tile, boolean success) {
159        if (tile.hasError()) {
160            success = false;
161            tile.setImage(null);
162        }
163        if (sharpenLevel != 0 && success) {
164            tile.setImage(sharpenImage(tile.getImage()));
165        }
166        tile.setLoaded(true);
167        needRedraw = true;
168        if (Main.map != null) {
169            Main.map.repaint(100);
170        }
171        tileRequestsOutstanding.remove(tile);
172        if (Main.isDebugEnabled()) {
173            Main.debug("tileLoadingFinished() tile: " + tile + " success: " + success);
174        }
175    }
176
177    @Override
178    public TileCache getTileCache() {
179        return tileCache;
180    }
181
182    private static class TmsTileClearController implements TileClearController, CancelListener {
183
184        private final ProgressMonitor monitor;
185        private boolean cancel = false;
186
187        public TmsTileClearController(ProgressMonitor monitor) {
188            this.monitor = monitor;
189            this.monitor.addCancelListener(this);
190        }
191
192        @Override
193        public void initClearDir(File dir) {
194        }
195
196        @Override
197        public void initClearFiles(File[] files) {
198            monitor.setTicksCount(files.length);
199            monitor.setTicks(0);
200        }
201
202        @Override
203        public boolean cancel() {
204            return cancel;
205        }
206
207        @Override
208        public void fileDeleted(File file) {
209            monitor.setTicks(monitor.getTicks()+1);
210        }
211
212        @Override
213        public void clearFinished() {
214            monitor.finishTask();
215        }
216
217        @Override
218        public void operationCanceled() {
219            cancel = true;
220        }
221    }
222
223    /**
224     * Clears the tile cache.
225     *
226     * If the current tileLoader is an instance of OsmTileLoader, a new
227     * TmsTileClearController is created and passed to the according clearCache
228     * method.
229     *
230     * @param monitor
231     * @see MemoryTileCache#clear()
232     * @see OsmFileCacheTileLoader#clearCache(org.openstreetmap.gui.jmapviewer.interfaces.TileSource, org.openstreetmap.gui.jmapviewer.interfaces.TileClearController)
233     */
234    void clearTileCache(ProgressMonitor monitor) {
235        tileCache.clear();
236        if (tileLoader instanceof CachedTileLoader) {
237            ((CachedTileLoader)tileLoader).clearCache(tileSource, new TmsTileClearController(monitor));
238        }
239    }
240
241    /**
242     * Zoomlevel at which tiles is currently downloaded.
243     * Initial zoom lvl is set to bestZoom
244     */
245    public int currentZoomLevel;
246
247    private Tile clickedTile;
248    private boolean needRedraw;
249    private JPopupMenu tileOptionMenu;
250    JCheckBoxMenuItem autoZoomPopup;
251    JCheckBoxMenuItem autoLoadPopup;
252    JCheckBoxMenuItem showErrorsPopup;
253    Tile showMetadataTile;
254    private AttributionSupport attribution = new AttributionSupport();
255    private static final Font InfoFont = new Font("sansserif", Font.BOLD, 13);
256
257    protected boolean autoZoom;
258    protected boolean autoLoad;
259    protected boolean showErrors;
260
261    /**
262     * Initiates a repaint of Main.map
263     *
264     * @see Main#map
265     * @see MapFrame#repaint()
266     */
267    void redraw() {
268        needRedraw = true;
269        Main.map.repaint();
270    }
271
272    static int checkMaxZoomLvl(int maxZoomLvl, TileSource ts) {
273        if(maxZoomLvl > MAX_ZOOM) {
274            maxZoomLvl = MAX_ZOOM;
275        }
276        if(maxZoomLvl < PROP_MIN_ZOOM_LVL.get()) {
277            maxZoomLvl = PROP_MIN_ZOOM_LVL.get();
278        }
279        if (ts != null && ts.getMaxZoom() != 0 && ts.getMaxZoom() < maxZoomLvl) {
280            maxZoomLvl = ts.getMaxZoom();
281        }
282        return maxZoomLvl;
283    }
284
285    public static int getMaxZoomLvl(TileSource ts) {
286        return checkMaxZoomLvl(PROP_MAX_ZOOM_LVL.get(), ts);
287    }
288
289    public static void setMaxZoomLvl(int maxZoomLvl) {
290        maxZoomLvl = checkMaxZoomLvl(maxZoomLvl, null);
291        PROP_MAX_ZOOM_LVL.put(maxZoomLvl);
292    }
293
294    static int checkMinZoomLvl(int minZoomLvl, TileSource ts) {
295        if(minZoomLvl < MIN_ZOOM) {
296            /*Main.debug("Min. zoom level should not be less than "+MIN_ZOOM+"! Setting to that.");*/
297            minZoomLvl = MIN_ZOOM;
298        }
299        if(minZoomLvl > PROP_MAX_ZOOM_LVL.get()) {
300            /*Main.debug("Min. zoom level should not be more than Max. zoom level! Setting to Max.");*/
301            minZoomLvl = getMaxZoomLvl(ts);
302        }
303        if (ts != null && ts.getMinZoom() > minZoomLvl) {
304            /*Main.debug("Increasing min. zoom level to match tile source");*/
305            minZoomLvl = ts.getMinZoom();
306        }
307        return minZoomLvl;
308    }
309
310    public static int getMinZoomLvl(TileSource ts) {
311        return checkMinZoomLvl(PROP_MIN_ZOOM_LVL.get(), ts);
312    }
313
314    public static void setMinZoomLvl(int minZoomLvl) {
315        minZoomLvl = checkMinZoomLvl(minZoomLvl, null);
316        PROP_MIN_ZOOM_LVL.put(minZoomLvl);
317    }
318
319    private static class CachedAttributionBingAerialTileSource extends BingAerialTileSource {
320
321        class BingAttributionData extends CacheCustomContent<IOException> {
322
323            public BingAttributionData() {
324                super("bing.attribution.xml", CacheCustomContent.INTERVAL_HOURLY);
325            }
326
327            @Override
328            protected byte[] updateData() throws IOException {
329                URL u = getAttributionUrl();
330                try (Scanner scanner = new Scanner(UTFInputStreamReader.create(Utils.openURL(u)))) {
331                    String r = scanner.useDelimiter("\\A").next();
332                    Main.info("Successfully loaded Bing attribution data.");
333                    return r.getBytes("UTF-8");
334                }
335            }
336        }
337
338        @Override
339        protected Callable<List<Attribution>> getAttributionLoaderCallable() {
340            return new Callable<List<Attribution>>() {
341
342                @Override
343                public List<Attribution> call() throws Exception {
344                    BingAttributionData attributionLoader = new BingAttributionData();
345                    int waitTimeSec = 1;
346                    while (true) {
347                        try {
348                            String xml = attributionLoader.updateIfRequiredString();
349                            return parseAttributionText(new InputSource(new StringReader((xml))));
350                        } catch (IOException ex) {
351                            Main.warn("Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds.");
352                            Thread.sleep(waitTimeSec * 1000L);
353                            waitTimeSec *= 2;
354                        }
355                    }
356                }
357            };
358        }
359    }
360
361    /**
362     * Creates and returns a new TileSource instance depending on the {@link ImageryType}
363     * of the passed ImageryInfo object.
364     *
365     * If no appropriate TileSource is found, null is returned.
366     * Currently supported ImageryType are {@link ImageryType#TMS},
367     * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
368     *
369     * @param info
370     * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
371     * @throws IllegalArgumentException
372     */
373    public static TileSource getTileSource(ImageryInfo info) throws IllegalArgumentException {
374        if (info.getImageryType() == ImageryType.TMS) {
375            checkUrl(info.getUrl());
376            TMSTileSource t = new TemplatedTMSTileSource(info.getName(), info.getUrl(), info.getMinZoom(), info.getMaxZoom());
377            info.setAttribution(t);
378            return t;
379        } else if (info.getImageryType() == ImageryType.BING)
380            return new CachedAttributionBingAerialTileSource();
381        else if (info.getImageryType() == ImageryType.SCANEX) {
382            return new ScanexTileSource(info.getName(), info.getUrl(), info.getMaxZoom());
383        }
384        return null;
385    }
386
387    public static void checkUrl(String url) throws IllegalArgumentException {
388        if (url == null) {
389            throw new IllegalArgumentException();
390        } else {
391            Matcher m = Pattern.compile("\\{[^}]*\\}").matcher(url);
392            while (m.find()) {
393                boolean isSupportedPattern = false;
394                for (String pattern : TemplatedTMSTileSource.ALL_PATTERNS) {
395                    if (m.group().matches(pattern)) {
396                        isSupportedPattern = true;
397                        break;
398                    }
399                }
400                if (!isSupportedPattern) {
401                    throw new IllegalArgumentException(tr("{0} is not a valid TMS argument. Please check this server URL:\n{1}", m.group(), url));
402                }
403            }
404        }
405    }
406
407    private void initTileSource(TileSource tileSource) {
408        this.tileSource = tileSource;
409        attribution.initialize(tileSource);
410
411        currentZoomLevel = getBestZoom();
412
413        tileCache = new MemoryTileCache();
414
415        tileLoader = loaderFactory.makeTileLoader(this);
416        if (tileLoader == null) {
417            tileLoader = new OsmTileLoader(this);
418        }
419        tileLoader.timeoutConnect = Main.pref.getInteger("socket.timeout.connect",15) * 1000;
420        tileLoader.timeoutRead = Main.pref.getInteger("socket.timeout.read", 30) * 1000;
421        if (tileSource instanceof TemplatedTMSTileSource) {
422            for(Entry<String, String> e : ((TemplatedTMSTileSource)tileSource).getHeaders().entrySet()) {
423                tileLoader.headers.put(e.getKey(), e.getValue());
424            }
425        }
426        tileLoader.headers.put("User-Agent", Version.getInstance().getFullAgentString());
427    }
428
429    @Override
430    public void setOffset(double dx, double dy) {
431        super.setOffset(dx, dy);
432        needRedraw = true;
433    }
434
435    /**
436     * Returns average number of screen pixels per tile pixel for current mapview
437     */
438    private double getScaleFactor(int zoom) {
439        if (!Main.isDisplayingMapView()) return 1;
440        MapView mv = Main.map.mapView;
441        LatLon topLeft = mv.getLatLon(0, 0);
442        LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
443        double x1 = tileSource.lonToTileX(topLeft.lon(), zoom);
444        double y1 = tileSource.latToTileY(topLeft.lat(), zoom);
445        double x2 = tileSource.lonToTileX(botRight.lon(), zoom);
446        double y2 = tileSource.latToTileY(botRight.lat(), zoom);
447
448        int screenPixels = mv.getWidth()*mv.getHeight();
449        double tilePixels = Math.abs((y2-y1)*(x2-x1)*tileSource.getTileSize()*tileSource.getTileSize());
450        if (screenPixels == 0 || tilePixels == 0) return 1;
451        return screenPixels/tilePixels;
452    }
453
454    private final int getBestZoom() {
455        double factor = getScaleFactor(1);
456        double result = Math.log(factor)/Math.log(2)/2+1;
457        // In general, smaller zoom levels are more readable.  We prefer big,
458        // block, pixelated (but readable) map text to small, smeared,
459        // unreadable underzoomed text.  So, use .floor() instead of rounding
460        // to skew things a bit toward the lower zooms.
461        int intResult = (int)Math.floor(result);
462        if (intResult > getMaxZoomLvl())
463            return getMaxZoomLvl();
464        if (intResult < getMinZoomLvl())
465            return getMinZoomLvl();
466        return intResult;
467    }
468
469    /**
470     * Function to set the maximum number of workers for tile loading to the value defined
471     * in preferences.
472     */
473    public static void setMaxWorkers() {
474        JobDispatcher.setMaxWorkers(PROP_TMS_JOBS.get());
475        JobDispatcher.getInstance().setLIFO(true);
476    }
477
478    @SuppressWarnings("serial")
479    public TMSLayer(ImageryInfo info) {
480        super(info);
481
482        setMaxWorkers();
483        if(!isProjectionSupported(Main.getProjection())) {
484            JOptionPane.showMessageDialog(Main.parent,
485                tr("TMS layers do not support the projection {0}.\n{1}\n"
486                + "Change the projection or remove the layer.",
487                Main.getProjection().toCode(), nameSupportedProjections()),
488                tr("Warning"),
489                JOptionPane.WARNING_MESSAGE);
490        }
491
492        setBackgroundLayer(true);
493        this.setVisible(true);
494
495        TileSource source = getTileSource(info);
496        if (source == null)
497            throw new IllegalStateException("Cannot create TMSLayer with non-TMS ImageryInfo");
498        initTileSource(source);
499    }
500
501    /**
502     * Adds a context menu to the mapView.
503     */
504    @Override
505    public void hookUpMapView() {
506        tileOptionMenu = new JPopupMenu();
507
508        autoZoom = PROP_DEFAULT_AUTOZOOM.get();
509        autoZoomPopup = new JCheckBoxMenuItem();
510        autoZoomPopup.setAction(new AbstractAction(tr("Auto Zoom")) {
511            @Override
512            public void actionPerformed(ActionEvent ae) {
513                autoZoom = !autoZoom;
514            }
515        });
516        autoZoomPopup.setSelected(autoZoom);
517        tileOptionMenu.add(autoZoomPopup);
518
519        autoLoad = PROP_DEFAULT_AUTOLOAD.get();
520        autoLoadPopup = new JCheckBoxMenuItem();
521        autoLoadPopup.setAction(new AbstractAction(tr("Auto load tiles")) {
522            @Override
523            public void actionPerformed(ActionEvent ae) {
524                autoLoad= !autoLoad;
525            }
526        });
527        autoLoadPopup.setSelected(autoLoad);
528        tileOptionMenu.add(autoLoadPopup);
529
530        showErrors = PROP_DEFAULT_SHOWERRORS.get();
531        showErrorsPopup = new JCheckBoxMenuItem();
532        showErrorsPopup.setAction(new AbstractAction(tr("Show Errors")) {
533            @Override
534            public void actionPerformed(ActionEvent ae) {
535                showErrors = !showErrors;
536            }
537        });
538        showErrorsPopup.setSelected(showErrors);
539        tileOptionMenu.add(showErrorsPopup);
540
541        tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Load Tile")) {
542            @Override
543            public void actionPerformed(ActionEvent ae) {
544                if (clickedTile != null) {
545                    loadTile(clickedTile, true);
546                    redraw();
547                }
548            }
549        }));
550
551        tileOptionMenu.add(new JMenuItem(new AbstractAction(
552                tr("Show Tile Info")) {
553            @Override
554            public void actionPerformed(ActionEvent ae) {
555                if (clickedTile != null) {
556                    showMetadataTile = clickedTile;
557                    redraw();
558                }
559            }
560        }));
561
562        /* FIXME
563        tileOptionMenu.add(new JMenuItem(new AbstractAction(
564                tr("Request Update")) {
565            public void actionPerformed(ActionEvent ae) {
566                if (clickedTile != null) {
567                    clickedTile.requestUpdate();
568                    redraw();
569                }
570            }
571        }));*/
572
573        tileOptionMenu.add(new JMenuItem(new AbstractAction(
574                tr("Load All Tiles")) {
575            @Override
576            public void actionPerformed(ActionEvent ae) {
577                loadAllTiles(true);
578                redraw();
579            }
580        }));
581
582        tileOptionMenu.add(new JMenuItem(new AbstractAction(
583                tr("Load All Error Tiles")) {
584            @Override
585            public void actionPerformed(ActionEvent ae) {
586                loadAllErrorTiles(true);
587                redraw();
588            }
589        }));
590
591        // increase and decrease commands
592        tileOptionMenu.add(new JMenuItem(new AbstractAction(
593                tr("Increase zoom")) {
594            @Override
595            public void actionPerformed(ActionEvent ae) {
596                increaseZoomLevel();
597                redraw();
598            }
599        }));
600
601        tileOptionMenu.add(new JMenuItem(new AbstractAction(
602                tr("Decrease zoom")) {
603            @Override
604            public void actionPerformed(ActionEvent ae) {
605                decreaseZoomLevel();
606                redraw();
607            }
608        }));
609
610        tileOptionMenu.add(new JMenuItem(new AbstractAction(
611                tr("Snap to tile size")) {
612            @Override
613            public void actionPerformed(ActionEvent ae) {
614                double new_factor = Math.sqrt(getScaleFactor(currentZoomLevel));
615                Main.map.mapView.zoomToFactor(new_factor);
616                redraw();
617            }
618        }));
619
620        tileOptionMenu.add(new JMenuItem(new AbstractAction(
621                tr("Flush Tile Cache")) {
622            @Override
623            public void actionPerformed(ActionEvent ae) {
624                new PleaseWaitRunnable(tr("Flush Tile Cache")) {
625                    @Override
626                    protected void realRun() throws SAXException, IOException,
627                            OsmTransferException {
628                        clearTileCache(getProgressMonitor());
629                    }
630
631                    @Override
632                    protected void finish() {
633                    }
634
635                    @Override
636                    protected void cancel() {
637                    }
638                }.run();
639            }
640        }));
641
642        final MouseAdapter adapter = new MouseAdapter() {
643            @Override
644            public void mouseClicked(MouseEvent e) {
645                if (!isVisible()) return;
646                if (e.getButton() == MouseEvent.BUTTON3) {
647                    clickedTile = getTileForPixelpos(e.getX(), e.getY());
648                    tileOptionMenu.show(e.getComponent(), e.getX(), e.getY());
649                } else if (e.getButton() == MouseEvent.BUTTON1) {
650                    attribution.handleAttribution(e.getPoint(), true);
651                }
652            }
653        };
654        Main.map.mapView.addMouseListener(adapter);
655
656        MapView.addLayerChangeListener(new LayerChangeListener() {
657            @Override
658            public void activeLayerChange(Layer oldLayer, Layer newLayer) {
659                //
660            }
661
662            @Override
663            public void layerAdded(Layer newLayer) {
664                //
665            }
666
667            @Override
668            public void layerRemoved(Layer oldLayer) {
669                if (oldLayer == TMSLayer.this) {
670                    Main.map.mapView.removeMouseListener(adapter);
671                    MapView.removeLayerChangeListener(this);
672                }
673            }
674        });
675    }
676
677    void zoomChanged() {
678        if (Main.isDebugEnabled()) {
679            Main.debug("zoomChanged(): " + currentZoomLevel);
680        }
681        needRedraw = true;
682        JobDispatcher.getInstance().cancelOutstandingJobs();
683        tileRequestsOutstanding.clear();
684    }
685
686    int getMaxZoomLvl() {
687        if (info.getMaxZoom() != 0)
688            return checkMaxZoomLvl(info.getMaxZoom(), tileSource);
689        else
690            return getMaxZoomLvl(tileSource);
691    }
692
693    int getMinZoomLvl() {
694        return getMinZoomLvl(tileSource);
695    }
696
697    /**
698     * Zoom in, go closer to map.
699     *
700     * @return    true, if zoom increasing was successfull, false othervise
701     */
702    public boolean zoomIncreaseAllowed() {
703        boolean zia = currentZoomLevel < this.getMaxZoomLvl();
704        if (Main.isDebugEnabled()) {
705            Main.debug("zoomIncreaseAllowed(): " + zia + " " + currentZoomLevel + " vs. " + this.getMaxZoomLvl() );
706        }
707        return zia;
708    }
709
710    public boolean increaseZoomLevel() {
711        if (zoomIncreaseAllowed()) {
712            currentZoomLevel++;
713            if (Main.isDebugEnabled()) {
714                Main.debug("increasing zoom level to: " + currentZoomLevel);
715            }
716            zoomChanged();
717        } else {
718            Main.warn("Current zoom level ("+currentZoomLevel+") could not be increased. "+
719                    "Max.zZoom Level "+this.getMaxZoomLvl()+" reached.");
720            return false;
721        }
722        return true;
723    }
724
725    public boolean setZoomLevel(int zoom) {
726        if (zoom == currentZoomLevel) return true;
727        if (zoom > this.getMaxZoomLvl()) return false;
728        if (zoom < this.getMinZoomLvl()) return false;
729        currentZoomLevel = zoom;
730        zoomChanged();
731        return true;
732    }
733
734    /**
735     * Check if zooming out is allowed
736     *
737     * @return    true, if zooming out is allowed (currentZoomLevel &gt; minZoomLevel)
738     */
739    public boolean zoomDecreaseAllowed() {
740        return currentZoomLevel > this.getMinZoomLvl();
741    }
742
743    /**
744     * Zoom out from map.
745     *
746     * @return    true, if zoom increasing was successfull, false othervise
747     */
748    public boolean decreaseZoomLevel() {
749        //int minZoom = this.getMinZoomLvl();
750        if (zoomDecreaseAllowed()) {
751            if (Main.isDebugEnabled()) {
752                Main.debug("decreasing zoom level to: " + currentZoomLevel);
753            }
754            currentZoomLevel--;
755            zoomChanged();
756        } else {
757            /*Main.debug("Current zoom level could not be decreased. Min. zoom level "+minZoom+" reached.");*/
758            return false;
759        }
760        return true;
761    }
762
763    /*
764     * We use these for quick, hackish calculations.  They
765     * are temporary only and intentionally not inserted
766     * into the tileCache.
767     */
768    synchronized Tile tempCornerTile(Tile t) {
769        int x = t.getXtile() + 1;
770        int y = t.getYtile() + 1;
771        int zoom = t.getZoom();
772        Tile tile = getTile(x, y, zoom);
773        if (tile != null)
774            return tile;
775        return new Tile(tileSource, x, y, zoom);
776    }
777
778    synchronized Tile getOrCreateTile(int x, int y, int zoom) {
779        Tile tile = getTile(x, y, zoom);
780        if (tile == null) {
781            tile = new Tile(tileSource, x, y, zoom);
782            tileCache.addTile(tile);
783            tile.loadPlaceholderFromCache(tileCache);
784        }
785        return tile;
786    }
787
788    /*
789     * This can and will return null for tiles that are not
790     * already in the cache.
791     */
792    synchronized Tile getTile(int x, int y, int zoom) {
793        int max = (1 << zoom);
794        if (x < 0 || x >= max || y < 0 || y >= max)
795            return null;
796        return tileCache.getTile(tileSource, x, y, zoom);
797    }
798
799    synchronized boolean loadTile(Tile tile, boolean force) {
800        if (tile == null)
801            return false;
802        if (!force && (tile.hasError() || tile.isLoaded()))
803            return false;
804        if (tile.isLoading())
805            return false;
806        if (tileRequestsOutstanding.contains(tile))
807            return false;
808        tileRequestsOutstanding.add(tile);
809        JobDispatcher.getInstance().addJob(tileLoader.createTileLoaderJob(tile));
810        return true;
811    }
812
813    void loadAllTiles(boolean force) {
814        MapView mv = Main.map.mapView;
815        EastNorth topLeft = mv.getEastNorth(0, 0);
816        EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
817
818        TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
819
820        // if there is more than 18 tiles on screen in any direction, do not
821        // load all tiles!
822        if (ts.tooLarge()) {
823            Main.warn("Not downloading all tiles because there is more than 18 tiles on an axis!");
824            return;
825        }
826        ts.loadAllTiles(force);
827    }
828
829    void loadAllErrorTiles(boolean force) {
830        MapView mv = Main.map.mapView;
831        EastNorth topLeft = mv.getEastNorth(0, 0);
832        EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
833
834        TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
835
836        ts.loadAllErrorTiles(force);
837    }
838
839    /*
840     * Attempt to approximate how much the image is being scaled. For instance,
841     * a 100x100 image being scaled to 50x50 would return 0.25.
842     */
843    Image lastScaledImage = null;
844    @Override
845    public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
846        boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0);
847        needRedraw = true;
848        if (Main.isDebugEnabled()) {
849            Main.debug("imageUpdate() done: " + done + " calling repaint");
850        }
851        Main.map.repaint(done ? 0 : 100);
852        return !done;
853    }
854
855    boolean imageLoaded(Image i) {
856        if (i == null)
857            return false;
858        int status = Toolkit.getDefaultToolkit().checkImage(i, -1, -1, this);
859        if ((status & ALLBITS) != 0)
860            return true;
861        return false;
862    }
863
864    /**
865     * Returns the image for the given tile if both tile and image are loaded.
866     * Otherwise returns  null.
867     *
868     * @param tile the Tile for which the image should be returned
869     * @return  the image of the tile or null.
870     */
871    Image getLoadedTileImage(Tile tile) {
872        if (!tile.isLoaded())
873            return null;
874        Image img = tile.getImage();
875        if (!imageLoaded(img))
876            return null;
877        return img;
878    }
879
880    LatLon tileLatLon(Tile t) {
881        int zoom = t.getZoom();
882        return new LatLon(tileSource.tileYToLat(t.getYtile(), zoom),
883                tileSource.tileXToLon(t.getXtile(), zoom));
884    }
885
886    Rectangle tileToRect(Tile t1) {
887        /*
888         * We need to get a box in which to draw, so advance by one tile in
889         * each direction to find the other corner of the box.
890         * Note: this somewhat pollutes the tile cache
891         */
892        Tile t2 = tempCornerTile(t1);
893        Rectangle rect = new Rectangle(pixelPos(t1));
894        rect.add(pixelPos(t2));
895        return rect;
896    }
897
898    // 'source' is the pixel coordinates for the area that
899    // the img is capable of filling in.  However, we probably
900    // only want a portion of it.
901    //
902    // 'border' is the screen cordinates that need to be drawn.
903    //  We must not draw outside of it.
904    void drawImageInside(Graphics g, Image sourceImg, Rectangle source, Rectangle border) {
905        Rectangle target = source;
906
907        // If a border is specified, only draw the intersection
908        // if what we have combined with what we are supposed
909        // to draw.
910        if (border != null) {
911            target = source.intersection(border);
912            if (Main.isDebugEnabled()) {
913                Main.debug("source: " + source + "\nborder: " + border + "\nintersection: " + target);
914            }
915        }
916
917        // All of the rectangles are in screen coordinates.  We need
918        // to how these correlate to the sourceImg pixels.  We could
919        // avoid doing this by scaling the image up to the 'source' size,
920        // but this should be cheaper.
921        //
922        // In some projections, x any y are scaled differently enough to
923        // cause a pixel or two of fudge.  Calculate them separately.
924        double imageYScaling = sourceImg.getHeight(this) / source.getHeight();
925        double imageXScaling = sourceImg.getWidth(this) / source.getWidth();
926
927        // How many pixels into the 'source' rectangle are we drawing?
928        int screen_x_offset = target.x - source.x;
929        int screen_y_offset = target.y - source.y;
930        // And how many pixels into the image itself does that
931        // correlate to?
932        int img_x_offset = (int)(screen_x_offset * imageXScaling);
933        int img_y_offset = (int)(screen_y_offset * imageYScaling);
934        // Now calculate the other corner of the image that we need
935        // by scaling the 'target' rectangle's dimensions.
936        int img_x_end   = img_x_offset + (int)(target.getWidth() * imageXScaling);
937        int img_y_end   = img_y_offset + (int)(target.getHeight() * imageYScaling);
938
939        if (Main.isDebugEnabled()) {
940            Main.debug("drawing image into target rect: " + target);
941        }
942        g.drawImage(sourceImg,
943                target.x, target.y,
944                target.x + target.width, target.y + target.height,
945                img_x_offset, img_y_offset,
946                img_x_end, img_y_end,
947                this);
948        if (PROP_FADE_AMOUNT.get() != 0) {
949            // dimm by painting opaque rect...
950            g.setColor(getFadeColorWithAlpha());
951            g.fillRect(target.x, target.y,
952                    target.width, target.height);
953        }
954    }
955
956    // This function is called for several zoom levels, not just
957    // the current one.  It should not trigger any tiles to be
958    // downloaded.  It should also avoid polluting the tile cache
959    // with any tiles since these tiles are not mandatory.
960    //
961    // The "border" tile tells us the boundaries of where we may
962    // draw.  It will not be from the zoom level that is being
963    // drawn currently.  If drawing the displayZoomLevel,
964    // border is null and we draw the entire tile set.
965    List<Tile> paintTileImages(Graphics g, TileSet ts, int zoom, Tile border) {
966        if (zoom <= 0) return Collections.emptyList();
967        Rectangle borderRect = null;
968        if (border != null) {
969            borderRect = tileToRect(border);
970        }
971        List<Tile> missedTiles = new LinkedList<>();
972        // The callers of this code *require* that we return any tiles
973        // that we do not draw in missedTiles.  ts.allExistingTiles() by
974        // default will only return already-existing tiles.  However, we
975        // need to return *all* tiles to the callers, so force creation
976        // here.
977        //boolean forceTileCreation = true;
978        for (Tile tile : ts.allTilesCreate()) {
979            Image img = getLoadedTileImage(tile);
980            if (img == null || tile.hasError()) {
981                if (Main.isDebugEnabled()) {
982                    Main.debug("missed tile: " + tile);
983                }
984                missedTiles.add(tile);
985                continue;
986            }
987            Rectangle sourceRect = tileToRect(tile);
988            if (borderRect != null && !sourceRect.intersects(borderRect)) {
989                continue;
990            }
991            drawImageInside(g, img, sourceRect, borderRect);
992        }
993        return missedTiles;
994    }
995
996    void myDrawString(Graphics g, String text, int x, int y) {
997        Color oldColor = g.getColor();
998        g.setColor(Color.black);
999        g.drawString(text,x+1,y+1);
1000        g.setColor(oldColor);
1001        g.drawString(text,x,y);
1002    }
1003
1004    void paintTileText(TileSet ts, Tile tile, Graphics g, MapView mv, int zoom, Tile t) {
1005        int fontHeight = g.getFontMetrics().getHeight();
1006        if (tile == null)
1007            return;
1008        Point p = pixelPos(t);
1009        int texty = p.y + 2 + fontHeight;
1010
1011        /*if (PROP_DRAW_DEBUG.get()) {
1012            myDrawString(g, "x=" + t.getXtile() + " y=" + t.getYtile() + " z=" + zoom + "", p.x + 2, texty);
1013            texty += 1 + fontHeight;
1014            if ((t.getXtile() % 32 == 0) && (t.getYtile() % 32 == 0)) {
1015                myDrawString(g, "x=" + t.getXtile() / 32 + " y=" + t.getYtile() / 32 + " z=7", p.x + 2, texty);
1016                texty += 1 + fontHeight;
1017            }
1018        }*/
1019
1020        if (tile == showMetadataTile) {
1021            String md = tile.toString();
1022            if (md != null) {
1023                myDrawString(g, md, p.x + 2, texty);
1024                texty += 1 + fontHeight;
1025            }
1026            Map<String, String> meta = tile.getMetadata();
1027            if (meta != null) {
1028                for (Map.Entry<String, String> entry : meta.entrySet()) {
1029                    myDrawString(g, entry.getKey() + ": " + entry.getValue(), p.x + 2, texty);
1030                    texty += 1 + fontHeight;
1031                }
1032            }
1033        }
1034
1035        /*String tileStatus = tile.getStatus();
1036        if (!tile.isLoaded() && PROP_DRAW_DEBUG.get()) {
1037            myDrawString(g, tr("image " + tileStatus), p.x + 2, texty);
1038            texty += 1 + fontHeight;
1039        }*/
1040
1041        if (tile.hasError() && showErrors) {
1042            myDrawString(g, tr("Error") + ": " + tr(tile.getErrorMessage()), p.x + 2, texty);
1043            texty += 1 + fontHeight;
1044        }
1045
1046        /*int xCursor = -1;
1047        int yCursor = -1;
1048        if (PROP_DRAW_DEBUG.get()) {
1049            if (yCursor < t.getYtile()) {
1050                if (t.getYtile() % 32 == 31) {
1051                    g.fillRect(0, p.y - 1, mv.getWidth(), 3);
1052                } else {
1053                    g.drawLine(0, p.y, mv.getWidth(), p.y);
1054                }
1055                yCursor = t.getYtile();
1056            }
1057            // This draws the vertical lines for the entire
1058            // column. Only draw them for the top tile in
1059            // the column.
1060            if (xCursor < t.getXtile()) {
1061                if (t.getXtile() % 32 == 0) {
1062                    // level 7 tile boundary
1063                    g.fillRect(p.x - 1, 0, 3, mv.getHeight());
1064                } else {
1065                    g.drawLine(p.x, 0, p.x, mv.getHeight());
1066                }
1067                xCursor = t.getXtile();
1068            }
1069        }*/
1070    }
1071
1072    private Point pixelPos(LatLon ll) {
1073        return Main.map.mapView.getPoint(Main.getProjection().latlon2eastNorth(ll).add(getDx(), getDy()));
1074    }
1075
1076    private Point pixelPos(Tile t) {
1077        double lon = tileSource.tileXToLon(t.getXtile(), t.getZoom());
1078        LatLon tmpLL = new LatLon(tileSource.tileYToLat(t.getYtile(), t.getZoom()), lon);
1079        return pixelPos(tmpLL);
1080    }
1081
1082    private LatLon getShiftedLatLon(EastNorth en) {
1083        return Main.getProjection().eastNorth2latlon(en.add(-getDx(), -getDy()));
1084    }
1085
1086    private Coordinate getShiftedCoord(EastNorth en) {
1087        LatLon ll = getShiftedLatLon(en);
1088        return new Coordinate(ll.lat(),ll.lon());
1089    }
1090
1091    private final TileSet nullTileSet = new TileSet((LatLon)null, (LatLon)null, 0);
1092    private class TileSet {
1093        int x0, x1, y0, y1;
1094        int zoom;
1095        int tileMax = -1;
1096
1097        /**
1098         * Create a TileSet by EastNorth bbox taking a layer shift in account
1099         */
1100        TileSet(EastNorth topLeft, EastNorth botRight, int zoom) {
1101            this(getShiftedLatLon(topLeft), getShiftedLatLon(botRight),zoom);
1102        }
1103
1104        /**
1105         * Create a TileSet by known LatLon bbox without layer shift correction
1106         */
1107        TileSet(LatLon topLeft, LatLon botRight, int zoom) {
1108            this.zoom = zoom;
1109            if (zoom == 0)
1110                return;
1111
1112            x0 = (int)tileSource.lonToTileX(topLeft.lon(),  zoom);
1113            y0 = (int)tileSource.latToTileY(topLeft.lat(),  zoom);
1114            x1 = (int)tileSource.lonToTileX(botRight.lon(), zoom);
1115            y1 = (int)tileSource.latToTileY(botRight.lat(), zoom);
1116            if (x0 > x1) {
1117                int tmp = x0;
1118                x0 = x1;
1119                x1 = tmp;
1120            }
1121            if (y0 > y1) {
1122                int tmp = y0;
1123                y0 = y1;
1124                y1 = tmp;
1125            }
1126            tileMax = (int)Math.pow(2.0, zoom);
1127            if (x0 < 0) {
1128                x0 = 0;
1129            }
1130            if (y0 < 0) {
1131                y0 = 0;
1132            }
1133            if (x1 > tileMax) {
1134                x1 = tileMax;
1135            }
1136            if (y1 > tileMax) {
1137                y1 = tileMax;
1138            }
1139        }
1140
1141        boolean tooSmall() {
1142            return this.tilesSpanned() < 2.1;
1143        }
1144
1145        boolean tooLarge() {
1146            return this.tilesSpanned() > 10;
1147        }
1148
1149        boolean insane() {
1150            return this.tilesSpanned() > 100;
1151        }
1152
1153        double tilesSpanned() {
1154            return Math.sqrt(1.0 * this.size());
1155        }
1156
1157        int size() {
1158            int x_span = x1 - x0 + 1;
1159            int y_span = y1 - y0 + 1;
1160            return x_span * y_span;
1161        }
1162
1163        /*
1164         * Get all tiles represented by this TileSet that are
1165         * already in the tileCache.
1166         */
1167        List<Tile> allExistingTiles() {
1168            return this.__allTiles(false);
1169        }
1170
1171        List<Tile> allTilesCreate() {
1172            return this.__allTiles(true);
1173        }
1174
1175        private List<Tile> __allTiles(boolean create) {
1176            // Tileset is either empty or too large
1177            if (zoom == 0 || this.insane())
1178                return Collections.emptyList();
1179            List<Tile> ret = new ArrayList<>();
1180            for (int x = x0; x <= x1; x++) {
1181                for (int y = y0; y <= y1; y++) {
1182                    Tile t;
1183                    if (create) {
1184                        t = getOrCreateTile(x % tileMax, y % tileMax, zoom);
1185                    } else {
1186                        t = getTile(x % tileMax, y % tileMax, zoom);
1187                    }
1188                    if (t != null) {
1189                        ret.add(t);
1190                    }
1191                }
1192            }
1193            return ret;
1194        }
1195
1196        private List<Tile> allLoadedTiles() {
1197            List<Tile> ret = new ArrayList<>();
1198            for (Tile t : this.allExistingTiles()) {
1199                if (t.isLoaded())
1200                    ret.add(t);
1201            }
1202            return ret;
1203        }
1204
1205        void loadAllTiles(boolean force) {
1206            if (!autoLoad && !force)
1207                return;
1208            for (Tile t : this.allTilesCreate()) {
1209                loadTile(t, false);
1210            }
1211        }
1212
1213        void loadAllErrorTiles(boolean force) {
1214            if (!autoLoad && !force)
1215                return;
1216            for (Tile t : this.allTilesCreate()) {
1217                if (t.hasError()) {
1218                    loadTile(t, true);
1219                }
1220            }
1221        }
1222    }
1223
1224
1225    private static class TileSetInfo {
1226        public boolean hasVisibleTiles = false;
1227        public boolean hasOverzoomedTiles = false;
1228        public boolean hasLoadingTiles = false;
1229    }
1230
1231    private static TileSetInfo getTileSetInfo(TileSet ts) {
1232        List<Tile> allTiles = ts.allExistingTiles();
1233        TileSetInfo result = new TileSetInfo();
1234        result.hasLoadingTiles = allTiles.size() < ts.size();
1235        for (Tile t : allTiles) {
1236            if (t.isLoaded()) {
1237                if (!t.hasError()) {
1238                    result.hasVisibleTiles = true;
1239                }
1240                if ("no-tile".equals(t.getValue("tile-info"))) {
1241                    result.hasOverzoomedTiles = true;
1242                }
1243            } else {
1244                result.hasLoadingTiles = true;
1245            }
1246        }
1247        return result;
1248    }
1249
1250    private class DeepTileSet {
1251        final EastNorth topLeft, botRight;
1252        final int minZoom, maxZoom;
1253        private final TileSet[] tileSets;
1254        private final TileSetInfo[] tileSetInfos;
1255        public DeepTileSet(EastNorth topLeft, EastNorth botRight, int minZoom, int maxZoom) {
1256            this.topLeft = topLeft;
1257            this.botRight = botRight;
1258            this.minZoom = minZoom;
1259            this.maxZoom = maxZoom;
1260            this.tileSets = new TileSet[maxZoom - minZoom + 1];
1261            this.tileSetInfos = new TileSetInfo[maxZoom - minZoom + 1];
1262        }
1263        public TileSet getTileSet(int zoom) {
1264            if (zoom < minZoom)
1265                return nullTileSet;
1266            TileSet ts = tileSets[zoom-minZoom];
1267            if (ts == null) {
1268                ts = new TileSet(topLeft, botRight, zoom);
1269                tileSets[zoom-minZoom] = ts;
1270            }
1271            return ts;
1272        }
1273        public TileSetInfo getTileSetInfo(int zoom) {
1274            if (zoom < minZoom)
1275                return new TileSetInfo();
1276            TileSetInfo tsi = tileSetInfos[zoom-minZoom];
1277            if (tsi == null) {
1278                tsi = TMSLayer.getTileSetInfo(getTileSet(zoom));
1279                tileSetInfos[zoom-minZoom] = tsi;
1280            }
1281            return tsi;
1282        }
1283    }
1284
1285    @Override
1286    public void paint(Graphics2D g, MapView mv, Bounds bounds) {
1287        //long start = System.currentTimeMillis();
1288        EastNorth topLeft = mv.getEastNorth(0, 0);
1289        EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
1290
1291        if (botRight.east() == 0.0 || botRight.north() == 0) {
1292            /*Main.debug("still initializing??");*/
1293            // probably still initializing
1294            return;
1295        }
1296
1297        needRedraw = false;
1298
1299        int zoom = currentZoomLevel;
1300        if (autoZoom) {
1301            double pixelScaling = getScaleFactor(zoom);
1302            if (pixelScaling > 3 || pixelScaling < 0.7) {
1303                zoom = getBestZoom();
1304            }
1305        }
1306
1307        DeepTileSet dts = new DeepTileSet(topLeft, botRight, getMinZoomLvl(), zoom);
1308        TileSet ts = dts.getTileSet(zoom);
1309
1310        int displayZoomLevel = zoom;
1311
1312        boolean noTilesAtZoom = false;
1313        if (autoZoom && autoLoad) {
1314            // Auto-detection of tilesource maxzoom (currently fully works only for Bing)
1315            TileSetInfo tsi = dts.getTileSetInfo(zoom);
1316            if (!tsi.hasVisibleTiles && (!tsi.hasLoadingTiles || tsi.hasOverzoomedTiles)) {
1317                noTilesAtZoom = true;
1318            }
1319            // Find highest zoom level with at least one visible tile
1320            for (int tmpZoom = zoom; tmpZoom > dts.minZoom; tmpZoom--) {
1321                if (dts.getTileSetInfo(tmpZoom).hasVisibleTiles) {
1322                    displayZoomLevel = tmpZoom;
1323                    break;
1324                }
1325            }
1326            // Do binary search between currentZoomLevel and displayZoomLevel
1327            while (zoom > displayZoomLevel && !tsi.hasVisibleTiles && tsi.hasOverzoomedTiles){
1328                zoom = (zoom + displayZoomLevel)/2;
1329                tsi = dts.getTileSetInfo(zoom);
1330            }
1331
1332            setZoomLevel(zoom);
1333
1334            // If all tiles at displayZoomLevel is loaded, load all tiles at next zoom level
1335            // to make sure there're really no more zoom levels
1336            if (zoom == displayZoomLevel && !tsi.hasLoadingTiles && zoom < dts.maxZoom) {
1337                zoom++;
1338                tsi = dts.getTileSetInfo(zoom);
1339            }
1340            // When we have overzoomed tiles and all tiles at current zoomlevel is loaded,
1341            // load tiles at previovus zoomlevels until we have all tiles on screen is loaded.
1342            while (zoom > dts.minZoom && tsi.hasOverzoomedTiles && !tsi.hasLoadingTiles) {
1343                zoom--;
1344                tsi = dts.getTileSetInfo(zoom);
1345            }
1346            ts = dts.getTileSet(zoom);
1347        } else if (autoZoom) {
1348            setZoomLevel(zoom);
1349        }
1350
1351        // Too many tiles... refuse to download
1352        if (!ts.tooLarge()) {
1353            //Main.debug("size: " + ts.size() + " spanned: " + ts.tilesSpanned());
1354            ts.loadAllTiles(false);
1355        }
1356
1357        if (displayZoomLevel != zoom) {
1358            ts = dts.getTileSet(displayZoomLevel);
1359        }
1360
1361        g.setColor(Color.DARK_GRAY);
1362
1363        List<Tile> missedTiles = this.paintTileImages(g, ts, displayZoomLevel, null);
1364        int[] otherZooms = { -1, 1, -2, 2, -3, -4, -5};
1365        for (int zoomOffset : otherZooms) {
1366            if (!autoZoom) {
1367                break;
1368            }
1369            int newzoom = displayZoomLevel + zoomOffset;
1370            if (newzoom < MIN_ZOOM) {
1371                continue;
1372            }
1373            if (missedTiles.size() <= 0) {
1374                break;
1375            }
1376            List<Tile> newlyMissedTiles = new LinkedList<>();
1377            for (Tile missed : missedTiles) {
1378                if ("no-tile".equals(missed.getValue("tile-info")) && zoomOffset > 0) {
1379                    // Don't try to paint from higher zoom levels when tile is overzoomed
1380                    newlyMissedTiles.add(missed);
1381                    continue;
1382                }
1383                Tile t2 = tempCornerTile(missed);
1384                LatLon topLeft2  = tileLatLon(missed);
1385                LatLon botRight2 = tileLatLon(t2);
1386                TileSet ts2 = new TileSet(topLeft2, botRight2, newzoom);
1387                // Instantiating large TileSets is expensive.  If there
1388                // are no loaded tiles, don't bother even trying.
1389                if (ts2.allLoadedTiles().isEmpty()) {
1390                    newlyMissedTiles.add(missed);
1391                    continue;
1392                }
1393                if (ts2.tooLarge()) {
1394                    continue;
1395                }
1396                newlyMissedTiles.addAll(this.paintTileImages(g, ts2, newzoom, missed));
1397            }
1398            missedTiles = newlyMissedTiles;
1399        }
1400        if (Main.isDebugEnabled() && missedTiles.size() > 0) {
1401            Main.debug("still missed "+missedTiles.size()+" in the end");
1402        }
1403        g.setColor(Color.red);
1404        g.setFont(InfoFont);
1405
1406        // The current zoom tileset should have all of its tiles
1407        // due to the loadAllTiles(), unless it to tooLarge()
1408        for (Tile t : ts.allExistingTiles()) {
1409            this.paintTileText(ts, t, g, mv, displayZoomLevel, t);
1410        }
1411
1412        attribution.paintAttribution(g, mv.getWidth(), mv.getHeight(), getShiftedCoord(topLeft), getShiftedCoord(botRight), displayZoomLevel, this);
1413
1414        //g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120);
1415        g.setColor(Color.lightGray);
1416        if (!autoZoom) {
1417            if (ts.insane()) {
1418                myDrawString(g, tr("zoom in to load any tiles"), 120, 120);
1419            } else if (ts.tooLarge()) {
1420                myDrawString(g, tr("zoom in to load more tiles"), 120, 120);
1421            } else if (ts.tooSmall()) {
1422                myDrawString(g, tr("increase zoom level to see more detail"), 120, 120);
1423            }
1424        }
1425        if (noTilesAtZoom) {
1426            myDrawString(g, tr("No tiles at this zoom level"), 120, 120);
1427        }
1428        if (Main.isDebugEnabled()) {
1429            myDrawString(g, tr("Current zoom: {0}", currentZoomLevel), 50, 140);
1430            myDrawString(g, tr("Display zoom: {0}", displayZoomLevel), 50, 155);
1431            myDrawString(g, tr("Pixel scale: {0}", getScaleFactor(currentZoomLevel)), 50, 170);
1432            myDrawString(g, tr("Best zoom: {0}", Math.log(getScaleFactor(1))/Math.log(2)/2+1), 50, 185);
1433        }
1434    }
1435
1436    /**
1437     * This isn't very efficient, but it is only used when the
1438     * user right-clicks on the map.
1439     */
1440    Tile getTileForPixelpos(int px, int py) {
1441        if (Main.isDebugEnabled()) {
1442            Main.debug("getTileForPixelpos("+px+", "+py+")");
1443        }
1444        MapView mv = Main.map.mapView;
1445        Point clicked = new Point(px, py);
1446        EastNorth topLeft = mv.getEastNorth(0, 0);
1447        EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
1448        int z = currentZoomLevel;
1449        TileSet ts = new TileSet(topLeft, botRight, z);
1450
1451        if (!ts.tooLarge()) {
1452            ts.loadAllTiles(false); // make sure there are tile objects for all tiles
1453        }
1454        Tile clickedTile = null;
1455        for (Tile t1 : ts.allExistingTiles()) {
1456            Tile t2 = tempCornerTile(t1);
1457            Rectangle r = new Rectangle(pixelPos(t1));
1458            r.add(pixelPos(t2));
1459            if (Main.isDebugEnabled()) {
1460                Main.debug("r: " + r + " clicked: " + clicked);
1461            }
1462            if (!r.contains(clicked)) {
1463                continue;
1464            }
1465            clickedTile  = t1;
1466            break;
1467        }
1468        if (clickedTile == null)
1469            return null;
1470        /*Main.debug("Clicked on tile: " + clickedTile.getXtile() + " " + clickedTile.getYtile() +
1471                " currentZoomLevel: " + currentZoomLevel);*/
1472        return clickedTile;
1473    }
1474
1475    @Override
1476    public Action[] getMenuEntries() {
1477        return new Action[] {
1478                LayerListDialog.getInstance().createShowHideLayerAction(),
1479                LayerListDialog.getInstance().createDeleteLayerAction(),
1480                SeparatorLayerAction.INSTANCE,
1481                // color,
1482                new OffsetAction(),
1483                new RenameLayerAction(this.getAssociatedFile(), this),
1484                SeparatorLayerAction.INSTANCE,
1485                new LayerListPopup.InfoAction(this) };
1486    }
1487
1488    @Override
1489    public String getToolTipText() {
1490        return tr("TMS layer ({0}), downloading in zoom {1}", getName(), currentZoomLevel);
1491    }
1492
1493    @Override
1494    public void visitBoundingBox(BoundingXYVisitor v) {
1495    }
1496
1497    @Override
1498    public boolean isChanged() {
1499        return needRedraw;
1500    }
1501
1502    @Override
1503    public final boolean isProjectionSupported(Projection proj) {
1504        return "EPSG:3857".equals(proj.toCode()) || "EPSG:4326".equals(proj.toCode());
1505    }
1506
1507    @Override
1508    public final String nameSupportedProjections() {
1509        return tr("EPSG:4326 and Mercator projection are supported");
1510    }
1511}