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