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

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

see #8570, #7406 - I/O refactorization:

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