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