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

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

code cleanup / robustness in edit layer handling

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