source: josm/trunk/src/org/openstreetmap/josm/gui/layer/TMSLayer.java@ 8510

Last change on this file since 8510 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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