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