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