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