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

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

see #8902 - c-like array definitions changed to java-like (patch by shinigami)

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