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