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