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