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

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

see #8606 - Set User-Agent for JMapViewer connections

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