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.font.TextAttribute;
|
---|
18 | import java.awt.geom.Rectangle2D;
|
---|
19 | import java.awt.image.ImageObserver;
|
---|
20 | import java.io.File;
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.net.URI;
|
---|
23 | import java.net.URISyntaxException;
|
---|
24 | import java.util.ArrayList;
|
---|
25 | import java.util.Collections;
|
---|
26 | import java.util.HashMap;
|
---|
27 | import java.util.HashSet;
|
---|
28 | import java.util.LinkedList;
|
---|
29 | import java.util.List;
|
---|
30 | import java.util.Map;
|
---|
31 |
|
---|
32 | import javax.swing.AbstractAction;
|
---|
33 | import javax.swing.Action;
|
---|
34 | import javax.swing.JCheckBoxMenuItem;
|
---|
35 | import javax.swing.JMenuItem;
|
---|
36 | import javax.swing.JPopupMenu;
|
---|
37 | import javax.swing.SwingUtilities;
|
---|
38 |
|
---|
39 | import org.openstreetmap.gui.jmapviewer.BingAerialTileSource;
|
---|
40 | import org.openstreetmap.gui.jmapviewer.Coordinate;
|
---|
41 | import org.openstreetmap.gui.jmapviewer.JobDispatcher;
|
---|
42 | import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
|
---|
43 | import org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader;
|
---|
44 | import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
|
---|
45 | import org.openstreetmap.gui.jmapviewer.ScanexIRSTileSource;
|
---|
46 | import org.openstreetmap.gui.jmapviewer.TMSTileSource;
|
---|
47 | import org.openstreetmap.gui.jmapviewer.TemplatedTMSTileSource;
|
---|
48 | import org.openstreetmap.gui.jmapviewer.Tile;
|
---|
49 | import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
|
---|
50 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
51 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
|
---|
52 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
53 | import org.openstreetmap.josm.Main;
|
---|
54 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
55 | import org.openstreetmap.josm.data.Bounds;
|
---|
56 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
57 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
58 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
59 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
60 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
61 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
62 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
63 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
---|
64 | import org.openstreetmap.josm.gui.MapView;
|
---|
65 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
66 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
67 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Class that displays a slippy map layer.
|
---|
71 | *
|
---|
72 | * @author Frederik Ramm <frederik@remote.org>
|
---|
73 | * @author LuVar <lubomir.varga@freemap.sk>
|
---|
74 | * @author Dave Hansen <dave@sr71.net>
|
---|
75 | * @author Upliner <upliner@gmail.com>
|
---|
76 | *
|
---|
77 | */
|
---|
78 | public class TMSLayer extends ImageryLayer implements ImageObserver, TileLoaderListener {
|
---|
79 | public static final String PREFERENCE_PREFIX = "imagery.tms";
|
---|
80 |
|
---|
81 | public static final int MAX_ZOOM = 30;
|
---|
82 | public static final int MIN_ZOOM = 2;
|
---|
83 | public static final int DEFAULT_MAX_ZOOM = 20;
|
---|
84 | public static final int DEFAULT_MIN_ZOOM = 2;
|
---|
85 |
|
---|
86 | public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
|
---|
87 | public static final BooleanProperty PROP_DEFAULT_AUTOLOAD = new BooleanProperty(PREFERENCE_PREFIX + ".default_autoload", true);
|
---|
88 | public static final IntegerProperty PROP_MIN_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".min_zoom_lvl", DEFAULT_MIN_ZOOM);
|
---|
89 | public static final IntegerProperty PROP_MAX_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".max_zoom_lvl", DEFAULT_MAX_ZOOM);
|
---|
90 | public static final BooleanProperty PROP_DRAW_DEBUG = new BooleanProperty(PREFERENCE_PREFIX + ".draw_debug", false);
|
---|
91 | public static final BooleanProperty PROP_ADD_TO_SLIPPYMAP_CHOOSER = new BooleanProperty(PREFERENCE_PREFIX + ".add_to_slippymap_chooser", true);
|
---|
92 | public static final StringProperty PROP_TILECACHE_DIR;
|
---|
93 |
|
---|
94 | static {
|
---|
95 | String defPath = null;
|
---|
96 | try {
|
---|
97 | defPath = OsmFileCacheTileLoader.getDefaultCacheDir().getAbsolutePath();
|
---|
98 | } catch (SecurityException e) {
|
---|
99 | }
|
---|
100 | PROP_TILECACHE_DIR = new StringProperty(PREFERENCE_PREFIX + ".tilecache_path", defPath);
|
---|
101 | }
|
---|
102 |
|
---|
103 | boolean debug = false;
|
---|
104 | void out(String s)
|
---|
105 | {
|
---|
106 | Main.debug(s);
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected MemoryTileCache tileCache;
|
---|
110 | protected TileSource tileSource;
|
---|
111 | protected TileLoader tileLoader;
|
---|
112 | JobDispatcher jobDispatcher = JobDispatcher.getInstance();
|
---|
113 |
|
---|
114 | HashSet<Tile> tileRequestsOutstanding = new HashSet<Tile>();
|
---|
115 | @Override
|
---|
116 | public synchronized void tileLoadingFinished(Tile tile, boolean success)
|
---|
117 | {
|
---|
118 | if (tile.hasError()) {
|
---|
119 | success = false;
|
---|
120 | tile.setImage(null);
|
---|
121 | }
|
---|
122 | if (sharpenLevel != 0 && success) {
|
---|
123 | tile.setImage(sharpenImage(tile.getImage()));
|
---|
124 | }
|
---|
125 | tile.setLoaded(true);
|
---|
126 | needRedraw = true;
|
---|
127 | Main.map.repaint(100);
|
---|
128 | tileRequestsOutstanding.remove(tile);
|
---|
129 | if (debug) {
|
---|
130 | out("tileLoadingFinished() tile: " + tile + " success: " + success);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | @Override
|
---|
134 | public TileCache getTileCache()
|
---|
135 | {
|
---|
136 | return tileCache;
|
---|
137 | }
|
---|
138 | void clearTileCache()
|
---|
139 | {
|
---|
140 | if (debug) {
|
---|
141 | out("clearing tile storage");
|
---|
142 | }
|
---|
143 | tileCache = new MemoryTileCache();
|
---|
144 | tileCache.setCacheSize(200);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Zoomlevel at which tiles is currently downloaded.
|
---|
149 | * Initial zoom lvl is set to bestZoom
|
---|
150 | */
|
---|
151 | public int currentZoomLevel;
|
---|
152 |
|
---|
153 | private Tile clickedTile;
|
---|
154 | private boolean needRedraw;
|
---|
155 | private JPopupMenu tileOptionMenu;
|
---|
156 | JCheckBoxMenuItem autoZoomPopup;
|
---|
157 | JCheckBoxMenuItem autoLoadPopup;
|
---|
158 | Tile showMetadataTile;
|
---|
159 | private Image attrImage;
|
---|
160 | private String attrTermsUrl;
|
---|
161 | private Rectangle attrImageBounds, attrToUBounds;
|
---|
162 | private static final Font InfoFont = new Font("sansserif", Font.BOLD, 13);
|
---|
163 | private static final Font ATTR_FONT = new Font("Arial", Font.PLAIN, 10);
|
---|
164 | private static final Font ATTR_LINK_FONT;
|
---|
165 | static {
|
---|
166 | HashMap<TextAttribute, Integer> aUnderline = new HashMap<TextAttribute, Integer>();
|
---|
167 | aUnderline.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
|
---|
168 | ATTR_LINK_FONT = ATTR_FONT.deriveFont(aUnderline);
|
---|
169 | }
|
---|
170 |
|
---|
171 | protected boolean autoZoom;
|
---|
172 | protected boolean autoLoad;
|
---|
173 |
|
---|
174 | void redraw()
|
---|
175 | {
|
---|
176 | needRedraw = true;
|
---|
177 | Main.map.repaint();
|
---|
178 | }
|
---|
179 |
|
---|
180 | static int checkMaxZoomLvl(int maxZoomLvl, TileSource ts)
|
---|
181 | {
|
---|
182 | if(maxZoomLvl > MAX_ZOOM) {
|
---|
183 | System.err.println("MaxZoomLvl shouldnt be more than 30! Setting to 30.");
|
---|
184 | maxZoomLvl = MAX_ZOOM;
|
---|
185 | }
|
---|
186 | if(maxZoomLvl < PROP_MIN_ZOOM_LVL.get()) {
|
---|
187 | System.err.println("maxZoomLvl shouldnt be more than minZoomLvl! Setting to minZoomLvl.");
|
---|
188 | maxZoomLvl = PROP_MIN_ZOOM_LVL.get();
|
---|
189 | }
|
---|
190 | if (ts != null && ts.getMaxZoom() != 0 && ts.getMaxZoom() < maxZoomLvl) {
|
---|
191 | maxZoomLvl = ts.getMaxZoom();
|
---|
192 | }
|
---|
193 | return maxZoomLvl;
|
---|
194 | }
|
---|
195 |
|
---|
196 | public static int getMaxZoomLvl(TileSource ts)
|
---|
197 | {
|
---|
198 | return checkMaxZoomLvl(PROP_MAX_ZOOM_LVL.get(), ts);
|
---|
199 | }
|
---|
200 |
|
---|
201 | public static void setMaxZoomLvl(int maxZoomLvl) {
|
---|
202 | maxZoomLvl = checkMaxZoomLvl(maxZoomLvl, null);
|
---|
203 | PROP_MAX_ZOOM_LVL.put(maxZoomLvl);
|
---|
204 | }
|
---|
205 |
|
---|
206 | static int checkMinZoomLvl(int minZoomLvl, TileSource ts)
|
---|
207 | {
|
---|
208 | if(minZoomLvl < MIN_ZOOM) {
|
---|
209 | System.err.println("minZoomLvl shouldnt be lees than "+MIN_ZOOM+"! Setting to that.");
|
---|
210 | minZoomLvl = MIN_ZOOM;
|
---|
211 | }
|
---|
212 | if(minZoomLvl > PROP_MAX_ZOOM_LVL.get()) {
|
---|
213 | System.err.println("minZoomLvl shouldnt be more than maxZoomLvl! Setting to maxZoomLvl.");
|
---|
214 | minZoomLvl = getMaxZoomLvl(ts);
|
---|
215 | }
|
---|
216 | if (ts != null && ts.getMinZoom() > minZoomLvl) {
|
---|
217 | System.err.println("increasomg minZoomLvl to match tile source");
|
---|
218 | minZoomLvl = ts.getMinZoom();
|
---|
219 | }
|
---|
220 | return minZoomLvl;
|
---|
221 | }
|
---|
222 |
|
---|
223 | public static int getMinZoomLvl(TileSource ts)
|
---|
224 | {
|
---|
225 | return checkMinZoomLvl(PROP_MIN_ZOOM_LVL.get(), ts);
|
---|
226 | }
|
---|
227 |
|
---|
228 | public static void setMinZoomLvl(int minZoomLvl) {
|
---|
229 | minZoomLvl = checkMinZoomLvl(minZoomLvl, null);
|
---|
230 | PROP_MIN_ZOOM_LVL.put(minZoomLvl);
|
---|
231 | }
|
---|
232 |
|
---|
233 | public static TileSource getTileSource(ImageryInfo info) {
|
---|
234 | if (info.getImageryType() == ImageryType.TMS) {
|
---|
235 | if(ImageryInfo.isUrlWithPatterns(info.getUrl()))
|
---|
236 | return new TemplatedTMSTileSource(info.getName(), info.getUrl(), info.getMaxZoom());
|
---|
237 | else
|
---|
238 | return new TMSTileSource(info.getName(),info.getUrl(), info.getMaxZoom());
|
---|
239 | } else if (info.getImageryType() == ImageryType.BING)
|
---|
240 | return new BingAerialTileSource();
|
---|
241 | else if (info.getImageryType() == ImageryType.SCANEX)
|
---|
242 | return new ScanexIRSTileSource();
|
---|
243 | return null;
|
---|
244 | }
|
---|
245 |
|
---|
246 | private void initTileSource(TileSource tileSource)
|
---|
247 | {
|
---|
248 | this.tileSource = tileSource;
|
---|
249 | boolean requireAttr = tileSource.requiresAttribution();
|
---|
250 | if(requireAttr) {
|
---|
251 | attrImage = tileSource.getAttributionImage();
|
---|
252 | if(attrImage == null) {
|
---|
253 | System.out.println("Attribution image was null.");
|
---|
254 | } else {
|
---|
255 | System.out.println("Got an attribution image " + attrImage.getHeight(this) + "x" + attrImage.getWidth(this));
|
---|
256 | }
|
---|
257 |
|
---|
258 | attrTermsUrl = tileSource.getTermsOfUseURL();
|
---|
259 | }
|
---|
260 |
|
---|
261 | currentZoomLevel = getBestZoom();
|
---|
262 |
|
---|
263 | clearTileCache();
|
---|
264 | String cachePath = TMSLayer.PROP_TILECACHE_DIR.get();
|
---|
265 | tileLoader = null;
|
---|
266 | if (cachePath != null && !cachePath.isEmpty()) {
|
---|
267 | try {
|
---|
268 | tileLoader = new OsmFileCacheTileLoader(this, new File(cachePath));
|
---|
269 | } catch (IOException e) {
|
---|
270 | }
|
---|
271 | }
|
---|
272 | if (tileLoader == null) {
|
---|
273 | tileLoader = new OsmTileLoader(this);
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | @Override
|
---|
278 | public void setOffset(double dx, double dy) {
|
---|
279 | super.setOffset(dx, dy);
|
---|
280 | needRedraw = true;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Returns average number of screen pixels per tile pixel for current mapview
|
---|
285 | */
|
---|
286 | private double getScaleFactor(int zoom) {
|
---|
287 | if (Main.map == null || Main.map.mapView == null) return 1;
|
---|
288 | MapView mv = Main.map.mapView;
|
---|
289 | LatLon topLeft = mv.getLatLon(0, 0);
|
---|
290 | LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
|
---|
291 | double x1 = tileSource.lonToTileX(topLeft.lon(), zoom);
|
---|
292 | double y1 = tileSource.latToTileY(topLeft.lat(), zoom);
|
---|
293 | double x2 = tileSource.lonToTileX(botRight.lon(), zoom);
|
---|
294 | double y2 = tileSource.latToTileY(botRight.lat(), zoom);
|
---|
295 |
|
---|
296 | int screenPixels = mv.getWidth()*mv.getHeight();
|
---|
297 | double tilePixels = Math.abs((y2-y1)*(x2-x1)*tileSource.getTileSize()*tileSource.getTileSize());
|
---|
298 | if (screenPixels == 0 || tilePixels == 0) return 1;
|
---|
299 | return screenPixels/tilePixels;
|
---|
300 | }
|
---|
301 |
|
---|
302 | private int getBestZoom() {
|
---|
303 | double factor = getScaleFactor(1);
|
---|
304 | double result = Math.log(factor)/Math.log(2)/2+1;
|
---|
305 | int intResult = (int)Math.round(result);
|
---|
306 | if (intResult > getMaxZoomLvl())
|
---|
307 | return getMaxZoomLvl();
|
---|
308 | if (intResult < getMinZoomLvl())
|
---|
309 | return getMinZoomLvl();
|
---|
310 | return intResult;
|
---|
311 | }
|
---|
312 |
|
---|
313 | @SuppressWarnings("serial")
|
---|
314 | public TMSLayer(ImageryInfo info) {
|
---|
315 | super(info);
|
---|
316 |
|
---|
317 | setBackgroundLayer(true);
|
---|
318 | this.setVisible(true);
|
---|
319 |
|
---|
320 | TileSource source = getTileSource(info);
|
---|
321 | if (source == null)
|
---|
322 | throw new IllegalStateException("cannot create TMSLayer with non-TMS ImageryInfo");
|
---|
323 | initTileSource(source);
|
---|
324 |
|
---|
325 | tileOptionMenu = new JPopupMenu();
|
---|
326 |
|
---|
327 | autoZoom = PROP_DEFAULT_AUTOZOOM.get();
|
---|
328 | autoZoomPopup = new JCheckBoxMenuItem();
|
---|
329 | autoZoomPopup.setAction(new AbstractAction(tr("Auto Zoom")) {
|
---|
330 | @Override
|
---|
331 | public void actionPerformed(ActionEvent ae) {
|
---|
332 | autoZoom = !autoZoom;
|
---|
333 | }
|
---|
334 | });
|
---|
335 | autoZoomPopup.setSelected(autoZoom);
|
---|
336 | tileOptionMenu.add(autoZoomPopup);
|
---|
337 |
|
---|
338 | autoLoad = PROP_DEFAULT_AUTOLOAD.get();
|
---|
339 | autoLoadPopup = new JCheckBoxMenuItem();
|
---|
340 | autoLoadPopup.setAction(new AbstractAction(tr("Auto load tiles")) {
|
---|
341 | @Override
|
---|
342 | public void actionPerformed(ActionEvent ae) {
|
---|
343 | autoLoad= !autoLoad;
|
---|
344 | }
|
---|
345 | });
|
---|
346 | autoLoadPopup.setSelected(autoLoad);
|
---|
347 | tileOptionMenu.add(autoLoadPopup);
|
---|
348 |
|
---|
349 | tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Load Tile")) {
|
---|
350 | @Override
|
---|
351 | public void actionPerformed(ActionEvent ae) {
|
---|
352 | if (clickedTile != null) {
|
---|
353 | loadTile(clickedTile);
|
---|
354 | redraw();
|
---|
355 | }
|
---|
356 | }
|
---|
357 | }));
|
---|
358 |
|
---|
359 | tileOptionMenu.add(new JMenuItem(new AbstractAction(
|
---|
360 | tr("Show Tile Info")) {
|
---|
361 | @Override
|
---|
362 | public void actionPerformed(ActionEvent ae) {
|
---|
363 | out("info tile: " + clickedTile);
|
---|
364 | if (clickedTile != null) {
|
---|
365 | showMetadataTile = clickedTile;
|
---|
366 | redraw();
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }));
|
---|
370 |
|
---|
371 | /* FIXME
|
---|
372 | tileOptionMenu.add(new JMenuItem(new AbstractAction(
|
---|
373 | tr("Request Update")) {
|
---|
374 | public void actionPerformed(ActionEvent ae) {
|
---|
375 | if (clickedTile != null) {
|
---|
376 | clickedTile.requestUpdate();
|
---|
377 | redraw();
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }));*/
|
---|
381 |
|
---|
382 | tileOptionMenu.add(new JMenuItem(new AbstractAction(
|
---|
383 | tr("Load All Tiles")) {
|
---|
384 | @Override
|
---|
385 | public void actionPerformed(ActionEvent ae) {
|
---|
386 | loadAllTiles(true);
|
---|
387 | redraw();
|
---|
388 | }
|
---|
389 | }));
|
---|
390 |
|
---|
391 | // increase and decrease commands
|
---|
392 | tileOptionMenu.add(new JMenuItem(
|
---|
393 | new AbstractAction(tr("Increase zoom")) {
|
---|
394 | @Override
|
---|
395 | public void actionPerformed(ActionEvent ae) {
|
---|
396 | increaseZoomLevel();
|
---|
397 | redraw();
|
---|
398 | }
|
---|
399 | }));
|
---|
400 |
|
---|
401 | tileOptionMenu.add(new JMenuItem(
|
---|
402 | new AbstractAction(tr("Decrease zoom")) {
|
---|
403 | @Override
|
---|
404 | public void actionPerformed(ActionEvent ae) {
|
---|
405 | decreaseZoomLevel();
|
---|
406 | redraw();
|
---|
407 | }
|
---|
408 | }));
|
---|
409 |
|
---|
410 | // FIXME: currently ran in errors
|
---|
411 |
|
---|
412 | tileOptionMenu.add(new JMenuItem(
|
---|
413 | new AbstractAction(tr("Snap to tile size")) {
|
---|
414 | @Override
|
---|
415 | public void actionPerformed(ActionEvent ae) {
|
---|
416 | double new_factor = Math.sqrt(getScaleFactor(currentZoomLevel));
|
---|
417 | Main.map.mapView.zoomToFactor(new_factor);
|
---|
418 | redraw();
|
---|
419 | }
|
---|
420 | }));
|
---|
421 | // end of adding menu commands
|
---|
422 |
|
---|
423 | tileOptionMenu.add(new JMenuItem(
|
---|
424 | new AbstractAction(tr("Flush Tile Cache")) {
|
---|
425 | @Override
|
---|
426 | public void actionPerformed(ActionEvent ae) {
|
---|
427 | System.out.print("flushing all tiles...");
|
---|
428 | clearTileCache();
|
---|
429 | System.out.println("done");
|
---|
430 | }
|
---|
431 | }));
|
---|
432 | // end of adding menu commands
|
---|
433 |
|
---|
434 | SwingUtilities.invokeLater(new Runnable() {
|
---|
435 | @Override
|
---|
436 | public void run() {
|
---|
437 | Main.map.mapView.addMouseListener(new MouseAdapter() {
|
---|
438 | @Override
|
---|
439 | public void mouseClicked(MouseEvent e) {
|
---|
440 | if (e.getButton() == MouseEvent.BUTTON3) {
|
---|
441 | clickedTile = getTileForPixelpos(e.getX(), e.getY());
|
---|
442 | tileOptionMenu.show(e.getComponent(), e.getX(), e.getY());
|
---|
443 | } else if (e.getButton() == MouseEvent.BUTTON1) {
|
---|
444 | if(!tileSource.requiresAttribution())
|
---|
445 | return;
|
---|
446 |
|
---|
447 | if(attrImageBounds.contains(e.getPoint())) {
|
---|
448 | try {
|
---|
449 | java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
|
---|
450 | desktop.browse(new URI(tileSource.getAttributionLinkURL()));
|
---|
451 | } catch (IOException e1) {
|
---|
452 | e1.printStackTrace();
|
---|
453 | } catch (URISyntaxException e1) {
|
---|
454 | e1.printStackTrace();
|
---|
455 | }
|
---|
456 | } else if(attrToUBounds.contains(e.getPoint())) {
|
---|
457 | try {
|
---|
458 | java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
|
---|
459 | desktop.browse(new URI(tileSource.getTermsOfUseURL()));
|
---|
460 | } catch (IOException e1) {
|
---|
461 | e1.printStackTrace();
|
---|
462 | } catch (URISyntaxException e1) {
|
---|
463 | e1.printStackTrace();
|
---|
464 | }
|
---|
465 | }
|
---|
466 | }
|
---|
467 | }
|
---|
468 | });
|
---|
469 |
|
---|
470 | MapView.addLayerChangeListener(new LayerChangeListener() {
|
---|
471 | @Override
|
---|
472 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
473 | //
|
---|
474 | }
|
---|
475 |
|
---|
476 | @Override
|
---|
477 | public void layerAdded(Layer newLayer) {
|
---|
478 | //
|
---|
479 | }
|
---|
480 |
|
---|
481 | @Override
|
---|
482 | public void layerRemoved(Layer oldLayer) {
|
---|
483 | MapView.removeLayerChangeListener(this);
|
---|
484 | }
|
---|
485 | });
|
---|
486 | }
|
---|
487 | });
|
---|
488 | }
|
---|
489 |
|
---|
490 | void zoomChanged()
|
---|
491 | {
|
---|
492 | if (debug) {
|
---|
493 | out("zoomChanged(): " + currentZoomLevel);
|
---|
494 | }
|
---|
495 | needRedraw = true;
|
---|
496 | jobDispatcher.cancelOutstandingJobs();
|
---|
497 | tileRequestsOutstanding.clear();
|
---|
498 | }
|
---|
499 |
|
---|
500 | int getMaxZoomLvl()
|
---|
501 | {
|
---|
502 | if (info.getMaxZoom() != 0)
|
---|
503 | return checkMaxZoomLvl(info.getMaxZoom(), tileSource);
|
---|
504 | else
|
---|
505 | return getMaxZoomLvl(tileSource);
|
---|
506 | }
|
---|
507 |
|
---|
508 | int getMinZoomLvl()
|
---|
509 | {
|
---|
510 | return getMinZoomLvl(tileSource);
|
---|
511 | }
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * Zoom in, go closer to map.
|
---|
515 | *
|
---|
516 | * @return true, if zoom increasing was successfull, false othervise
|
---|
517 | */
|
---|
518 | public boolean zoomIncreaseAllowed()
|
---|
519 | {
|
---|
520 | boolean zia = currentZoomLevel < this.getMaxZoomLvl();
|
---|
521 | if (debug) {
|
---|
522 | out("zoomIncreaseAllowed(): " + zia + " " + currentZoomLevel + " vs. " + this.getMaxZoomLvl() );
|
---|
523 | }
|
---|
524 | return zia;
|
---|
525 | }
|
---|
526 | public boolean increaseZoomLevel()
|
---|
527 | {
|
---|
528 | if (zoomIncreaseAllowed()) {
|
---|
529 | currentZoomLevel++;
|
---|
530 | if (debug) {
|
---|
531 | out("increasing zoom level to: " + currentZoomLevel);
|
---|
532 | }
|
---|
533 | zoomChanged();
|
---|
534 | } else {
|
---|
535 | System.err.println("current zoom lvl ("+currentZoomLevel+") couldnt be increased. "+
|
---|
536 | "MaxZoomLvl ("+this.getMaxZoomLvl()+") reached.");
|
---|
537 | return false;
|
---|
538 | }
|
---|
539 | return true;
|
---|
540 | }
|
---|
541 |
|
---|
542 | public boolean setZoomLevel(int zoom)
|
---|
543 | {
|
---|
544 | if (zoom == currentZoomLevel) return true;
|
---|
545 | if (zoom > this.getMaxZoomLvl()) return false;
|
---|
546 | if (zoom < this.getMinZoomLvl()) return false;
|
---|
547 | currentZoomLevel = zoom;
|
---|
548 | zoomChanged();
|
---|
549 | return true;
|
---|
550 | }
|
---|
551 |
|
---|
552 | /**
|
---|
553 | * Zoom out from map.
|
---|
554 | *
|
---|
555 | * @return true, if zoom increasing was successfull, false othervise
|
---|
556 | */
|
---|
557 | public boolean zoomDecreaseAllowed()
|
---|
558 | {
|
---|
559 | return currentZoomLevel > this.getMinZoomLvl();
|
---|
560 | }
|
---|
561 | public boolean decreaseZoomLevel() {
|
---|
562 | int minZoom = this.getMinZoomLvl();
|
---|
563 | if (zoomDecreaseAllowed()) {
|
---|
564 | if (debug) {
|
---|
565 | out("decreasing zoom level to: " + currentZoomLevel);
|
---|
566 | }
|
---|
567 | currentZoomLevel--;
|
---|
568 | zoomChanged();
|
---|
569 | } else {
|
---|
570 | System.err.println("current zoom lvl couldnt be decreased. MinZoomLvl("+minZoom+") reached.");
|
---|
571 | return false;
|
---|
572 | }
|
---|
573 | return true;
|
---|
574 | }
|
---|
575 |
|
---|
576 | /*
|
---|
577 | * We use these for quick, hackish calculations. They
|
---|
578 | * are temporary only and intentionally not inserted
|
---|
579 | * into the tileCache.
|
---|
580 | */
|
---|
581 | synchronized Tile tempCornerTile(Tile t) {
|
---|
582 | int x = t.getXtile() + 1;
|
---|
583 | int y = t.getYtile() + 1;
|
---|
584 | int zoom = t.getZoom();
|
---|
585 | Tile tile = getTile(x, y, zoom);
|
---|
586 | if (tile != null)
|
---|
587 | return tile;
|
---|
588 | return new Tile(tileSource, x, y, zoom);
|
---|
589 | }
|
---|
590 | synchronized Tile getOrCreateTile(int x, int y, int zoom) {
|
---|
591 | Tile tile = getTile(x, y, zoom);
|
---|
592 | if (tile == null) {
|
---|
593 | tile = new Tile(tileSource, x, y, zoom);
|
---|
594 | tileCache.addTile(tile);
|
---|
595 | tile.loadPlaceholderFromCache(tileCache);
|
---|
596 | }
|
---|
597 | return tile;
|
---|
598 | }
|
---|
599 |
|
---|
600 | /*
|
---|
601 | * This can and will return null for tiles that are not
|
---|
602 | * already in the cache.
|
---|
603 | */
|
---|
604 | synchronized Tile getTile(int x, int y, int zoom) {
|
---|
605 | int max = (1 << zoom);
|
---|
606 | if (x < 0 || x >= max || y < 0 || y >= max)
|
---|
607 | return null;
|
---|
608 | Tile tile = tileCache.getTile(tileSource, x, y, zoom);
|
---|
609 | return tile;
|
---|
610 | }
|
---|
611 |
|
---|
612 | synchronized boolean loadTile(Tile tile)
|
---|
613 | {
|
---|
614 | if (tile == null)
|
---|
615 | return false;
|
---|
616 | if (tile.hasError())
|
---|
617 | return false;
|
---|
618 | if (tile.isLoaded())
|
---|
619 | return false;
|
---|
620 | if (tile.isLoading())
|
---|
621 | return false;
|
---|
622 | if (tileRequestsOutstanding.contains(tile))
|
---|
623 | return false;
|
---|
624 | tileRequestsOutstanding.add(tile);
|
---|
625 | jobDispatcher.addJob(tileLoader.createTileLoaderJob(tileSource,
|
---|
626 | tile.getXtile(), tile.getYtile(), tile.getZoom()));
|
---|
627 | return true;
|
---|
628 | }
|
---|
629 |
|
---|
630 | void loadAllTiles(boolean force) {
|
---|
631 | MapView mv = Main.map.mapView;
|
---|
632 | EastNorth topLeft = mv.getEastNorth(0, 0);
|
---|
633 | EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
|
---|
634 |
|
---|
635 | TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
|
---|
636 |
|
---|
637 | // if there is more than 18 tiles on screen in any direction, do not
|
---|
638 | // load all tiles!
|
---|
639 | if (ts.tooLarge()) {
|
---|
640 | System.out.println("Not downloading all tiles because there is more than 18 tiles on an axis!");
|
---|
641 | return;
|
---|
642 | }
|
---|
643 | ts.loadAllTiles(force);
|
---|
644 | }
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * Attempt to approximate how much the image is being scaled. For instance,
|
---|
648 | * a 100x100 image being scaled to 50x50 would return 0.25.
|
---|
649 | */
|
---|
650 | Image lastScaledImage = null;
|
---|
651 | @Override
|
---|
652 | public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
|
---|
653 | boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0);
|
---|
654 | needRedraw = true;
|
---|
655 | if (debug) {
|
---|
656 | out("imageUpdate() done: " + done + " calling repaint");
|
---|
657 | }
|
---|
658 | Main.map.repaint(done ? 0 : 100);
|
---|
659 | return !done;
|
---|
660 | }
|
---|
661 | boolean imageLoaded(Image i) {
|
---|
662 | if (i == null)
|
---|
663 | return false;
|
---|
664 | int status = Toolkit.getDefaultToolkit().checkImage(i, -1, -1, this);
|
---|
665 | if ((status & ALLBITS) != 0)
|
---|
666 | return true;
|
---|
667 | return false;
|
---|
668 | }
|
---|
669 | Image getLoadedTileImage(Tile tile)
|
---|
670 | {
|
---|
671 | if (!tile.isLoaded())
|
---|
672 | return null;
|
---|
673 | Image img = tile.getImage();
|
---|
674 | if (!imageLoaded(img))
|
---|
675 | return null;
|
---|
676 | return img;
|
---|
677 | }
|
---|
678 |
|
---|
679 | LatLon tileLatLon(Tile t)
|
---|
680 | {
|
---|
681 | int zoom = t.getZoom();
|
---|
682 | return new LatLon(tileSource.tileYToLat(t.getYtile(), zoom),
|
---|
683 | tileSource.tileXToLon(t.getXtile(), zoom));
|
---|
684 | }
|
---|
685 |
|
---|
686 | Rectangle tileToRect(Tile t1)
|
---|
687 | {
|
---|
688 | /*
|
---|
689 | * We need to get a box in which to draw, so advance by one tile in
|
---|
690 | * each direction to find the other corner of the box.
|
---|
691 | * Note: this somewhat pollutes the tile cache
|
---|
692 | */
|
---|
693 | Tile t2 = tempCornerTile(t1);
|
---|
694 | Rectangle rect = new Rectangle(pixelPos(t1));
|
---|
695 | rect.add(pixelPos(t2));
|
---|
696 | return rect;
|
---|
697 | }
|
---|
698 |
|
---|
699 | // 'source' is the pixel coordinates for the area that
|
---|
700 | // the img is capable of filling in. However, we probably
|
---|
701 | // only want a portion of it.
|
---|
702 | //
|
---|
703 | // 'border' is the screen cordinates that need to be drawn.
|
---|
704 | // We must not draw outside of it.
|
---|
705 | void drawImageInside(Graphics g, Image sourceImg, Rectangle source, Rectangle border)
|
---|
706 | {
|
---|
707 | Rectangle target = source;
|
---|
708 |
|
---|
709 | // If a border is specified, only draw the intersection
|
---|
710 | // if what we have combined with what we are supposed
|
---|
711 | // to draw.
|
---|
712 | if (border != null) {
|
---|
713 | target = source.intersection(border);
|
---|
714 | if (debug) {
|
---|
715 | out("source: " + source + "\nborder: " + border + "\nintersection: " + target);
|
---|
716 | }
|
---|
717 | }
|
---|
718 |
|
---|
719 | // All of the rectangles are in screen coordinates. We need
|
---|
720 | // to how these correlate to the sourceImg pixels. We could
|
---|
721 | // avoid doing this by scaling the image up to the 'source' size,
|
---|
722 | // but this should be cheaper.
|
---|
723 | //
|
---|
724 | // In some projections, x any y are scaled differently enough to
|
---|
725 | // cause a pixel or two of fudge. Calculate them separately.
|
---|
726 | double imageYScaling = sourceImg.getHeight(this) / source.getHeight();
|
---|
727 | double imageXScaling = sourceImg.getWidth(this) / source.getWidth();
|
---|
728 |
|
---|
729 | // How many pixels into the 'source' rectangle are we drawing?
|
---|
730 | int screen_x_offset = target.x - source.x;
|
---|
731 | int screen_y_offset = target.y - source.y;
|
---|
732 | // And how many pixels into the image itself does that
|
---|
733 | // correlate to?
|
---|
734 | int img_x_offset = (int)(screen_x_offset * imageXScaling);
|
---|
735 | int img_y_offset = (int)(screen_y_offset * imageYScaling);
|
---|
736 | // Now calculate the other corner of the image that we need
|
---|
737 | // by scaling the 'target' rectangle's dimensions.
|
---|
738 | int img_x_end = img_x_offset + (int)(target.getWidth() * imageXScaling);
|
---|
739 | int img_y_end = img_y_offset + (int)(target.getHeight() * imageYScaling);
|
---|
740 |
|
---|
741 | if (debug) {
|
---|
742 | out("drawing image into target rect: " + target);
|
---|
743 | }
|
---|
744 | g.drawImage(sourceImg,
|
---|
745 | target.x, target.y,
|
---|
746 | target.x + target.width, target.y + target.height,
|
---|
747 | img_x_offset, img_y_offset,
|
---|
748 | img_x_end, img_y_end,
|
---|
749 | this);
|
---|
750 | if (PROP_FADE_AMOUNT.get() != 0) {
|
---|
751 | // dimm by painting opaque rect...
|
---|
752 | g.setColor(getFadeColorWithAlpha());
|
---|
753 | g.fillRect(target.x, target.y,
|
---|
754 | target.width, target.height);
|
---|
755 | }
|
---|
756 | }
|
---|
757 | // This function is called for several zoom levels, not just
|
---|
758 | // the current one. It should not trigger any tiles to be
|
---|
759 | // downloaded. It should also avoid polluting the tile cache
|
---|
760 | // with any tiles since these tiles are not mandatory.
|
---|
761 | //
|
---|
762 | // The "border" tile tells us the boundaries of where we may
|
---|
763 | // draw. It will not be from the zoom level that is being
|
---|
764 | // drawn currently. If drawing the displayZoomLevel,
|
---|
765 | // border is null and we draw the entire tile set.
|
---|
766 | List<Tile> paintTileImages(Graphics g, TileSet ts, int zoom, Tile border) {
|
---|
767 | if (zoom <= 0) return Collections.emptyList();
|
---|
768 | Rectangle borderRect = null;
|
---|
769 | if (border != null) {
|
---|
770 | borderRect = tileToRect(border);
|
---|
771 | }
|
---|
772 | List<Tile> missedTiles = new LinkedList<Tile>();
|
---|
773 | for (Tile tile : ts.allTiles()) {
|
---|
774 | Image img = getLoadedTileImage(tile);
|
---|
775 | if (img == null || tile.hasError()) {
|
---|
776 | if (debug) {
|
---|
777 | out("missed tile: " + tile);
|
---|
778 | }
|
---|
779 | missedTiles.add(tile);
|
---|
780 | continue;
|
---|
781 | }
|
---|
782 | Rectangle sourceRect = tileToRect(tile);
|
---|
783 | if (borderRect != null && !sourceRect.intersects(borderRect)) {
|
---|
784 | continue;
|
---|
785 | }
|
---|
786 | drawImageInside(g, img, sourceRect, borderRect);
|
---|
787 | }// end of for
|
---|
788 | return missedTiles;
|
---|
789 | }
|
---|
790 |
|
---|
791 | void myDrawString(Graphics g, String text, int x, int y) {
|
---|
792 | Color oldColor = g.getColor();
|
---|
793 | g.setColor(Color.black);
|
---|
794 | g.drawString(text,x+1,y+1);
|
---|
795 | g.setColor(oldColor);
|
---|
796 | g.drawString(text,x,y);
|
---|
797 | }
|
---|
798 |
|
---|
799 | void paintTileText(TileSet ts, Tile tile, Graphics g, MapView mv, int zoom, Tile t) {
|
---|
800 | int fontHeight = g.getFontMetrics().getHeight();
|
---|
801 | if (tile == null)
|
---|
802 | return;
|
---|
803 | Point p = pixelPos(t);
|
---|
804 | int texty = p.y + 2 + fontHeight;
|
---|
805 |
|
---|
806 | if (PROP_DRAW_DEBUG.get()) {
|
---|
807 | myDrawString(g, "x=" + t.getXtile() + " y=" + t.getYtile() + " z=" + zoom + "", p.x + 2, texty);
|
---|
808 | texty += 1 + fontHeight;
|
---|
809 | if ((t.getXtile() % 32 == 0) && (t.getYtile() % 32 == 0)) {
|
---|
810 | myDrawString(g, "x=" + t.getXtile() / 32 + " y=" + t.getYtile() / 32 + " z=7", p.x + 2, texty);
|
---|
811 | texty += 1 + fontHeight;
|
---|
812 | }
|
---|
813 | }// end of if draw debug
|
---|
814 |
|
---|
815 | if (tile == showMetadataTile) {
|
---|
816 | String md = tile.toString();
|
---|
817 | if (md != null) {
|
---|
818 | myDrawString(g, md, p.x + 2, texty);
|
---|
819 | texty += 1 + fontHeight;
|
---|
820 | }
|
---|
821 | Map<String, String> meta = tile.getMetadata();
|
---|
822 | if (meta != null) {
|
---|
823 | for (Map.Entry<String, String> entry : meta.entrySet()) {
|
---|
824 | myDrawString(g, entry.getKey() + ": " + entry.getValue(), p.x + 2, texty);
|
---|
825 | texty += 1 + fontHeight;
|
---|
826 | }
|
---|
827 | }
|
---|
828 | }
|
---|
829 |
|
---|
830 | String tileStatus = tile.getStatus();
|
---|
831 | if (!tile.isLoaded() && PROP_DRAW_DEBUG.get()) {
|
---|
832 | myDrawString(g, tr("image " + tileStatus), p.x + 2, texty);
|
---|
833 | texty += 1 + fontHeight;
|
---|
834 | }
|
---|
835 |
|
---|
836 | if (tile.hasError()) {
|
---|
837 | myDrawString(g, tr("Error") + ": " + tr(tile.getErrorMessage()), p.x + 2, texty);
|
---|
838 | texty += 1 + fontHeight;
|
---|
839 | }
|
---|
840 |
|
---|
841 | int xCursor = -1;
|
---|
842 | int yCursor = -1;
|
---|
843 | if (PROP_DRAW_DEBUG.get()) {
|
---|
844 | if (yCursor < t.getYtile()) {
|
---|
845 | if (t.getYtile() % 32 == 31) {
|
---|
846 | g.fillRect(0, p.y - 1, mv.getWidth(), 3);
|
---|
847 | } else {
|
---|
848 | g.drawLine(0, p.y, mv.getWidth(), p.y);
|
---|
849 | }
|
---|
850 | yCursor = t.getYtile();
|
---|
851 | }
|
---|
852 | // This draws the vertical lines for the entire
|
---|
853 | // column. Only draw them for the top tile in
|
---|
854 | // the column.
|
---|
855 | if (xCursor < t.getXtile()) {
|
---|
856 | if (t.getXtile() % 32 == 0) {
|
---|
857 | // level 7 tile boundary
|
---|
858 | g.fillRect(p.x - 1, 0, 3, mv.getHeight());
|
---|
859 | } else {
|
---|
860 | g.drawLine(p.x, 0, p.x, mv.getHeight());
|
---|
861 | }
|
---|
862 | xCursor = t.getXtile();
|
---|
863 | }
|
---|
864 | }
|
---|
865 | }
|
---|
866 |
|
---|
867 | private Point pixelPos(LatLon ll) {
|
---|
868 | return Main.map.mapView.getPoint(Main.proj.latlon2eastNorth(ll).add(getDx(), getDy()));
|
---|
869 | }
|
---|
870 | private Point pixelPos(Tile t) {
|
---|
871 | double lon = tileSource.tileXToLon(t.getXtile(), t.getZoom());
|
---|
872 | LatLon tmpLL = new LatLon(tileSource.tileYToLat(t.getYtile(), t.getZoom()), lon);
|
---|
873 | return pixelPos(tmpLL);
|
---|
874 | }
|
---|
875 | private LatLon getShiftedLatLon(EastNorth en) {
|
---|
876 | return Main.proj.eastNorth2latlon(en.add(-getDx(), -getDy()));
|
---|
877 | }
|
---|
878 | private Coordinate getShiftedCoord(EastNorth en) {
|
---|
879 | LatLon ll = getShiftedLatLon(en);
|
---|
880 | return new Coordinate(ll.lat(),ll.lon());
|
---|
881 | }
|
---|
882 | private final TileSet nullTileSet = new TileSet((LatLon)null, (LatLon)null, 0);
|
---|
883 | private class TileSet {
|
---|
884 | int x0, x1, y0, y1;
|
---|
885 | int zoom;
|
---|
886 | int tileMax = -1;
|
---|
887 |
|
---|
888 | /**
|
---|
889 | * Create a TileSet by EastNorth bbox taking a layer shift in account
|
---|
890 | */
|
---|
891 | TileSet(EastNorth topLeft, EastNorth botRight, int zoom) {
|
---|
892 | this(getShiftedLatLon(topLeft), getShiftedLatLon(botRight),zoom);
|
---|
893 | }
|
---|
894 |
|
---|
895 | /**
|
---|
896 | * Create a TileSet by known LatLon bbox without layer shift correction
|
---|
897 | */
|
---|
898 | TileSet(LatLon topLeft, LatLon botRight, int zoom) {
|
---|
899 | this.zoom = zoom;
|
---|
900 | if (zoom == 0)
|
---|
901 | return;
|
---|
902 |
|
---|
903 | x0 = (int)tileSource.lonToTileX(topLeft.lon(), zoom);
|
---|
904 | y0 = (int)tileSource.latToTileY(topLeft.lat(), zoom);
|
---|
905 | x1 = (int)tileSource.lonToTileX(botRight.lon(), zoom);
|
---|
906 | y1 = (int)tileSource.latToTileY(botRight.lat(), zoom);
|
---|
907 | if (x0 > x1) {
|
---|
908 | int tmp = x0;
|
---|
909 | x0 = x1;
|
---|
910 | x1 = tmp;
|
---|
911 | }
|
---|
912 | if (y0 > y1) {
|
---|
913 | int tmp = y0;
|
---|
914 | y0 = y1;
|
---|
915 | y1 = tmp;
|
---|
916 | }
|
---|
917 | tileMax = (int)Math.pow(2.0, zoom);
|
---|
918 | if (x0 < 0) {
|
---|
919 | x0 = 0;
|
---|
920 | }
|
---|
921 | if (y0 < 0) {
|
---|
922 | y0 = 0;
|
---|
923 | }
|
---|
924 | if (x1 > tileMax) {
|
---|
925 | x1 = tileMax;
|
---|
926 | }
|
---|
927 | if (y1 > tileMax) {
|
---|
928 | y1 = tileMax;
|
---|
929 | }
|
---|
930 | }
|
---|
931 | boolean tooSmall() {
|
---|
932 | return this.tilesSpanned() < 2.1;
|
---|
933 | }
|
---|
934 | boolean tooLarge() {
|
---|
935 | return this.tilesSpanned() > 10;
|
---|
936 | }
|
---|
937 | boolean insane() {
|
---|
938 | return this.tilesSpanned() > 100;
|
---|
939 | }
|
---|
940 | double tilesSpanned() {
|
---|
941 | return Math.sqrt(1.0 * this.size());
|
---|
942 | }
|
---|
943 |
|
---|
944 | int size() {
|
---|
945 | int x_span = x1 - x0 + 1;
|
---|
946 | int y_span = y1 - y0 + 1;
|
---|
947 | return x_span * y_span;
|
---|
948 | }
|
---|
949 |
|
---|
950 | /*
|
---|
951 | * Get all tiles represented by this TileSet that are
|
---|
952 | * already in the tileCache.
|
---|
953 | */
|
---|
954 | List<Tile> allTiles()
|
---|
955 | {
|
---|
956 | return this.allTiles(false);
|
---|
957 | }
|
---|
958 | private List<Tile> allTiles(boolean create)
|
---|
959 | {
|
---|
960 | // Tileset is either empty or too large
|
---|
961 | if (zoom == 0 || this.insane())
|
---|
962 | return Collections.emptyList();
|
---|
963 | List<Tile> ret = new ArrayList<Tile>();
|
---|
964 | for (int x = x0; x <= x1; x++) {
|
---|
965 | for (int y = y0; y <= y1; y++) {
|
---|
966 | Tile t;
|
---|
967 | if (create) {
|
---|
968 | t = getOrCreateTile(x % tileMax, y % tileMax, zoom);
|
---|
969 | } else {
|
---|
970 | t = getTile(x % tileMax, y % tileMax, zoom);
|
---|
971 | }
|
---|
972 | if (t != null) {
|
---|
973 | ret.add(t);
|
---|
974 | }
|
---|
975 | }
|
---|
976 | }
|
---|
977 | return ret;
|
---|
978 | }
|
---|
979 |
|
---|
980 | void loadAllTiles(boolean force)
|
---|
981 | {
|
---|
982 | List<Tile> tiles = this.allTiles(true);
|
---|
983 | if (!autoLoad && !force)
|
---|
984 | return;
|
---|
985 | int nr_queued = 0;
|
---|
986 | for (Tile t : tiles) {
|
---|
987 | if (loadTile(t)) {
|
---|
988 | nr_queued++;
|
---|
989 | }
|
---|
990 | }
|
---|
991 | if (debug)
|
---|
992 | if (nr_queued > 0) {
|
---|
993 | out("queued to load: " + nr_queued + "/" + tiles.size() + " tiles at zoom: " + zoom);
|
---|
994 | }
|
---|
995 | }
|
---|
996 | }
|
---|
997 |
|
---|
998 |
|
---|
999 | private static class TileSetInfo {
|
---|
1000 | public boolean hasVisibleTiles = false;
|
---|
1001 | public boolean hasOverzoomedTiles = false;
|
---|
1002 | public boolean hasLoadingTiles = false;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | private static TileSetInfo getTileSetInfo(TileSet ts) {
|
---|
1006 | List<Tile> allTiles = ts.allTiles();
|
---|
1007 | TileSetInfo result = new TileSetInfo();
|
---|
1008 | result.hasLoadingTiles = allTiles.size() < ts.size();
|
---|
1009 | for (Tile t : allTiles) {
|
---|
1010 | if (t.isLoaded()) {
|
---|
1011 | if (!t.hasError()) {
|
---|
1012 | result.hasVisibleTiles = true;
|
---|
1013 | }
|
---|
1014 | if ("no-tile".equals(t.getValue("tile-info"))) {
|
---|
1015 | result.hasOverzoomedTiles = true;
|
---|
1016 | }
|
---|
1017 | } else {
|
---|
1018 | result.hasLoadingTiles = true;
|
---|
1019 | }
|
---|
1020 | }
|
---|
1021 | return result;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | private class DeepTileSet {
|
---|
1025 | final EastNorth topLeft, botRight;
|
---|
1026 | final int minZoom, maxZoom;
|
---|
1027 | private final TileSet[] tileSets;
|
---|
1028 | private final TileSetInfo[] tileSetInfos;
|
---|
1029 | public DeepTileSet(EastNorth topLeft, EastNorth botRight, int minZoom, int maxZoom) {
|
---|
1030 | this.topLeft = topLeft;
|
---|
1031 | this.botRight = botRight;
|
---|
1032 | this.minZoom = minZoom;
|
---|
1033 | this.maxZoom = maxZoom;
|
---|
1034 | this.tileSets = new TileSet[maxZoom - minZoom + 1];
|
---|
1035 | this.tileSetInfos = new TileSetInfo[maxZoom - minZoom + 1];
|
---|
1036 | }
|
---|
1037 | public TileSet getTileSet(int zoom) {
|
---|
1038 | if (zoom < minZoom)
|
---|
1039 | return nullTileSet;
|
---|
1040 | TileSet ts = tileSets[zoom-minZoom];
|
---|
1041 | if (ts == null) {
|
---|
1042 | ts = new TileSet(topLeft, botRight, zoom);
|
---|
1043 | tileSets[zoom-minZoom] = ts;
|
---|
1044 | }
|
---|
1045 | return ts;
|
---|
1046 | }
|
---|
1047 | public TileSetInfo getTileSetInfo(int zoom) {
|
---|
1048 | if (zoom < minZoom)
|
---|
1049 | return new TileSetInfo();
|
---|
1050 | TileSetInfo tsi = tileSetInfos[zoom-minZoom];
|
---|
1051 | if (tsi == null) {
|
---|
1052 | tsi = TMSLayer.getTileSetInfo(getTileSet(zoom));
|
---|
1053 | tileSetInfos[zoom-minZoom] = tsi;
|
---|
1054 | }
|
---|
1055 | return tsi;
|
---|
1056 | }
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | /**
|
---|
1060 | */
|
---|
1061 | @Override
|
---|
1062 | public void paint(Graphics2D g, MapView mv, Bounds bounds) {
|
---|
1063 | //long start = System.currentTimeMillis();
|
---|
1064 | EastNorth topLeft = mv.getEastNorth(0, 0);
|
---|
1065 | EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
|
---|
1066 |
|
---|
1067 | if (botRight.east() == 0.0 || botRight.north() == 0) {
|
---|
1068 | Main.debug("still initializing??");
|
---|
1069 | // probably still initializing
|
---|
1070 | return;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | needRedraw = false;
|
---|
1074 |
|
---|
1075 | int zoom = currentZoomLevel;
|
---|
1076 | if (autoZoom) {
|
---|
1077 | double pixelScaling = getScaleFactor(zoom);
|
---|
1078 | if (pixelScaling > 3 || pixelScaling < 0.45) {
|
---|
1079 | zoom = getBestZoom();
|
---|
1080 | }
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | DeepTileSet dts = new DeepTileSet(topLeft, botRight, getMinZoomLvl(), zoom);
|
---|
1084 | TileSet ts = dts.getTileSet(zoom);
|
---|
1085 |
|
---|
1086 | int displayZoomLevel = zoom;
|
---|
1087 |
|
---|
1088 | boolean noTilesAtZoom = false;
|
---|
1089 | if (autoZoom && autoLoad) {
|
---|
1090 | // Auto-detection of tilesource maxzoom (currently fully works only for Bing)
|
---|
1091 | TileSetInfo tsi = dts.getTileSetInfo(zoom);
|
---|
1092 | if (!tsi.hasVisibleTiles && (!tsi.hasLoadingTiles || tsi.hasOverzoomedTiles)) {
|
---|
1093 | noTilesAtZoom = true;
|
---|
1094 | }
|
---|
1095 | // Find highest zoom level with at least one visible tile
|
---|
1096 | while (displayZoomLevel > dts.minZoom &&
|
---|
1097 | !dts.getTileSetInfo(displayZoomLevel).hasVisibleTiles) {
|
---|
1098 | displayZoomLevel--;
|
---|
1099 | }
|
---|
1100 | // Do binary search between currentZoomLevel and displayZoomLevel
|
---|
1101 | while (zoom > displayZoomLevel && !tsi.hasVisibleTiles && tsi.hasOverzoomedTiles){
|
---|
1102 | zoom = (zoom + displayZoomLevel)/2;
|
---|
1103 | tsi = dts.getTileSetInfo(zoom);
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | setZoomLevel(zoom);
|
---|
1107 |
|
---|
1108 | // If all tiles at displayZoomLevel is loaded, load all tiles at next zoom level
|
---|
1109 | // to make sure there're really no more zoom levels
|
---|
1110 | if (zoom == displayZoomLevel && !tsi.hasLoadingTiles && zoom < dts.maxZoom) {
|
---|
1111 | zoom++;
|
---|
1112 | tsi = dts.getTileSetInfo(zoom);
|
---|
1113 | }
|
---|
1114 | // When we have overzoomed tiles and all tiles at current zoomlevel is loaded,
|
---|
1115 | // load tiles at previovus zoomlevels until we have all tiles on screen is loaded.
|
---|
1116 | while (zoom > dts.minZoom && tsi.hasOverzoomedTiles && !tsi.hasLoadingTiles) {
|
---|
1117 | zoom--;
|
---|
1118 | tsi = dts.getTileSetInfo(zoom);
|
---|
1119 | }
|
---|
1120 | ts = dts.getTileSet(zoom);
|
---|
1121 | } else if (autoZoom) {
|
---|
1122 | setZoomLevel(zoom);
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | // Too many tiles... refuse to download
|
---|
1126 | if (!ts.tooLarge()) {
|
---|
1127 | //out("size: " + ts.size() + " spanned: " + ts.tilesSpanned());
|
---|
1128 | ts.loadAllTiles(false);
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | if (displayZoomLevel != zoom) {
|
---|
1132 | ts = dts.getTileSet(displayZoomLevel);
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | g.setColor(Color.DARK_GRAY);
|
---|
1136 |
|
---|
1137 | List<Tile> missedTiles = this.paintTileImages(g, ts, displayZoomLevel, null);
|
---|
1138 | int otherZooms[] = { -1, 1, -2, 2, -3, -4, -5};
|
---|
1139 | for (int zoomOffset : otherZooms) {
|
---|
1140 | if (!autoZoom) {
|
---|
1141 | break;
|
---|
1142 | }
|
---|
1143 | if (!autoLoad) {
|
---|
1144 | break;
|
---|
1145 | }
|
---|
1146 | int newzoom = displayZoomLevel + zoomOffset;
|
---|
1147 | if (missedTiles.size() <= 0) {
|
---|
1148 | break;
|
---|
1149 | }
|
---|
1150 | List<Tile> newlyMissedTiles = new LinkedList<Tile>();
|
---|
1151 | for (Tile missed : missedTiles) {
|
---|
1152 | if ("no-tile".equals(missed.getValue("tile-info")) && zoomOffset > 0) {
|
---|
1153 | // Don't try to paint from higher zoom levels when tile is overzoomed
|
---|
1154 | newlyMissedTiles.add(missed);
|
---|
1155 | continue;
|
---|
1156 | }
|
---|
1157 | Tile t2 = tempCornerTile(missed);
|
---|
1158 | LatLon topLeft2 = tileLatLon(missed);
|
---|
1159 | LatLon botRight2 = tileLatLon(t2);
|
---|
1160 | TileSet ts2 = new TileSet(topLeft2, botRight2, newzoom);
|
---|
1161 | if (ts2.tooLarge()) {
|
---|
1162 | continue;
|
---|
1163 | }
|
---|
1164 | newlyMissedTiles.addAll(this.paintTileImages(g, ts2, newzoom, missed));
|
---|
1165 | }
|
---|
1166 | missedTiles = newlyMissedTiles;
|
---|
1167 | }
|
---|
1168 | if (debug && missedTiles.size() > 0) {
|
---|
1169 | out("still missed "+missedTiles.size()+" in the end");
|
---|
1170 | }
|
---|
1171 | g.setColor(Color.red);
|
---|
1172 | g.setFont(InfoFont);
|
---|
1173 |
|
---|
1174 | // The current zoom tileset is guaranteed to have all of
|
---|
1175 | // its tiles
|
---|
1176 | for (Tile t : ts.allTiles()) {
|
---|
1177 | this.paintTileText(ts, t, g, mv, displayZoomLevel, t);
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | if (tileSource.requiresAttribution()) {
|
---|
1181 | // Draw attribution
|
---|
1182 | Font font = g.getFont();
|
---|
1183 | g.setFont(ATTR_LINK_FONT);
|
---|
1184 | g.setColor(Color.white);
|
---|
1185 |
|
---|
1186 | // Draw terms of use text
|
---|
1187 | Rectangle2D termsStringBounds = g.getFontMetrics().getStringBounds("Background Terms of Use", g);
|
---|
1188 | int textHeight = (int) termsStringBounds.getHeight() - 5;
|
---|
1189 | int textWidth = (int) termsStringBounds.getWidth();
|
---|
1190 | int termsTextY = mv.getHeight() - textHeight;
|
---|
1191 | if(attrTermsUrl != null) {
|
---|
1192 | int x = 2;
|
---|
1193 | int y = mv.getHeight() - textHeight;
|
---|
1194 | attrToUBounds = new Rectangle(x, y, textWidth, textHeight);
|
---|
1195 | myDrawString(g, "Background Terms of Use", x, y);
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | // Draw attribution logo
|
---|
1199 | int imgWidth = attrImage.getWidth(this);
|
---|
1200 | if(attrImage != null) {
|
---|
1201 | int x = 2;
|
---|
1202 | int height = attrImage.getHeight(this);
|
---|
1203 | int y = termsTextY - height - textHeight - 5;
|
---|
1204 | attrImageBounds = new Rectangle(x, y, imgWidth, height);
|
---|
1205 | g.drawImage(attrImage, x, y, this);
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | g.setFont(ATTR_FONT);
|
---|
1209 | String attributionText = tileSource.getAttributionText(displayZoomLevel,
|
---|
1210 | getShiftedCoord(topLeft), getShiftedCoord(botRight));
|
---|
1211 | Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(attributionText, g);
|
---|
1212 | {
|
---|
1213 | int x = mv.getWidth() - (int) stringBounds.getWidth();
|
---|
1214 | int y = mv.getHeight() - textHeight;
|
---|
1215 | myDrawString(g, attributionText, x, y);
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | g.setFont(font);
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | //g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120);
|
---|
1222 | g.setColor(Color.lightGray);
|
---|
1223 | if (!autoZoom) {
|
---|
1224 | if (ts.insane()) {
|
---|
1225 | myDrawString(g, tr("zoom in to load any tiles"), 120, 120);
|
---|
1226 | } else if (ts.tooLarge()) {
|
---|
1227 | myDrawString(g, tr("zoom in to load more tiles"), 120, 120);
|
---|
1228 | } else if (ts.tooSmall()) {
|
---|
1229 | myDrawString(g, tr("increase zoom level to see more detail"), 120, 120);
|
---|
1230 | }
|
---|
1231 | }
|
---|
1232 | if (noTilesAtZoom) {
|
---|
1233 | myDrawString(g, tr("No tiles at this zoom level"), 120, 120);
|
---|
1234 | }
|
---|
1235 | if (debug) {
|
---|
1236 | myDrawString(g, tr("Current zoom: {0}", currentZoomLevel), 50, 140);
|
---|
1237 | myDrawString(g, tr("Display zoom: {0}", displayZoomLevel), 50, 155);
|
---|
1238 | myDrawString(g, tr("Pixel scale: {0}", getScaleFactor(currentZoomLevel)), 50, 170);
|
---|
1239 | myDrawString(g, tr("Best zoom: {0}", Math.log(getScaleFactor(1))/Math.log(2)/2+1), 50, 185);
|
---|
1240 | }
|
---|
1241 | }// end of paint method
|
---|
1242 |
|
---|
1243 | /**
|
---|
1244 | * This isn't very efficient, but it is only used when the
|
---|
1245 | * user right-clicks on the map.
|
---|
1246 | */
|
---|
1247 | Tile getTileForPixelpos(int px, int py) {
|
---|
1248 | if (debug) {
|
---|
1249 | out("getTileForPixelpos("+px+", "+py+")");
|
---|
1250 | }
|
---|
1251 | MapView mv = Main.map.mapView;
|
---|
1252 | Point clicked = new Point(px, py);
|
---|
1253 | EastNorth topLeft = mv.getEastNorth(0, 0);
|
---|
1254 | EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
|
---|
1255 | int z = currentZoomLevel;
|
---|
1256 | TileSet ts = new TileSet(topLeft, botRight, z);
|
---|
1257 |
|
---|
1258 | if (!ts.tooLarge()) {
|
---|
1259 | ts.loadAllTiles(false); // make sure there are tile objects for all tiles
|
---|
1260 | }
|
---|
1261 | Tile clickedTile = null;
|
---|
1262 | for (Tile t1 : ts.allTiles()) {
|
---|
1263 | Tile t2 = tempCornerTile(t1);
|
---|
1264 | Rectangle r = new Rectangle(pixelPos(t1));
|
---|
1265 | r.add(pixelPos(t2));
|
---|
1266 | if (debug) {
|
---|
1267 | out("r: " + r + " clicked: " + clicked);
|
---|
1268 | }
|
---|
1269 | if (!r.contains(clicked)) {
|
---|
1270 | continue;
|
---|
1271 | }
|
---|
1272 | clickedTile = t1;
|
---|
1273 | break;
|
---|
1274 | }
|
---|
1275 | if (clickedTile == null)
|
---|
1276 | return null;
|
---|
1277 | System.out.println("clicked on tile: " + clickedTile.getXtile() + " " + clickedTile.getYtile() +
|
---|
1278 | " currentZoomLevel: " + currentZoomLevel);
|
---|
1279 | return clickedTile;
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 | @Override
|
---|
1283 | public Action[] getMenuEntries() {
|
---|
1284 | return new Action[] {
|
---|
1285 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
1286 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
1287 | SeparatorLayerAction.INSTANCE,
|
---|
1288 | // color,
|
---|
1289 | new OffsetAction(),
|
---|
1290 | new RenameLayerAction(this.getAssociatedFile(), this),
|
---|
1291 | SeparatorLayerAction.INSTANCE,
|
---|
1292 | new LayerListPopup.InfoAction(this) };
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | @Override
|
---|
1296 | public String getToolTipText() {
|
---|
1297 | return null;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | @Override
|
---|
1301 | public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | @Override
|
---|
1305 | public boolean isChanged() {
|
---|
1306 | return needRedraw;
|
---|
1307 | }
|
---|
1308 | }
|
---|