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