1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.bbox;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.concurrent.TimeUnit;
|
---|
10 | import java.util.stream.Collectors;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.gui.jmapviewer.JMapViewer;
|
---|
15 | import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
|
---|
16 | import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
|
---|
17 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
18 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
19 | import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
|
---|
20 | import org.openstreetmap.josm.data.Version;
|
---|
21 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
22 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
23 | import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
|
---|
24 | import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
|
---|
25 | import org.openstreetmap.josm.data.preferences.StringProperty;
|
---|
26 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
27 | import org.openstreetmap.josm.gui.Notification;
|
---|
28 | import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;
|
---|
29 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
30 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
---|
31 | import org.openstreetmap.josm.tools.Logging;
|
---|
32 | import org.openstreetmap.josm.tools.Utils;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * An extension of {@link JMapViewer} that implements JOSM-specific tile loading mechanisms.
|
---|
36 | * @since 15145
|
---|
37 | */
|
---|
38 | public class JosmMapViewer extends JMapViewer {
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * A list of tile sources that can be used for displaying the map.
|
---|
42 | */
|
---|
43 | @FunctionalInterface
|
---|
44 | public interface TileSourceProvider {
|
---|
45 | /**
|
---|
46 | * Gets the tile sources that can be displayed
|
---|
47 | * @return The tile sources
|
---|
48 | */
|
---|
49 | List<TileSource> getTileSources();
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * TileSource provider.
|
---|
54 | */
|
---|
55 | public abstract static class AbstractImageryInfoBasedTileSourceProvider implements TileSourceProvider {
|
---|
56 | /**
|
---|
57 | * Returns the list of imagery infos backing tile sources.
|
---|
58 | * @return the list of imagery infos backing tile sources
|
---|
59 | */
|
---|
60 | public abstract List<ImageryInfo> getImageryInfos();
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public List<TileSource> getTileSources() {
|
---|
64 | if (!TMSLayer.PROP_ADD_TO_SLIPPYMAP_CHOOSER.get()) return Collections.<TileSource>emptyList();
|
---|
65 | return imageryInfosToTileSources(getImageryInfos());
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | static List<TileSource> imageryInfosToTileSources(List<ImageryInfo> imageryInfos) {
|
---|
70 | List<TileSource> sources = new ArrayList<>();
|
---|
71 | for (ImageryInfo info : imageryInfos) {
|
---|
72 | try {
|
---|
73 | TileSource source = TMSLayer.getTileSourceStatic(info);
|
---|
74 | if (source != null) {
|
---|
75 | sources.add(source);
|
---|
76 | }
|
---|
77 | } catch (IllegalArgumentException ex) {
|
---|
78 | Logging.trace(ex);
|
---|
79 | Logging.warn(ex.getMessage());
|
---|
80 | if (!Utils.isEmpty(ex.getMessage())) {
|
---|
81 | new Notification(ex.getMessage()).setIcon(JOptionPane.WARNING_MESSAGE).show();
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | return sources;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * TileSource provider - providing default OSM tile source
|
---|
90 | */
|
---|
91 | public static class DefaultOsmTileSourceProvider implements TileSourceProvider {
|
---|
92 |
|
---|
93 | protected static final StringProperty DEFAULT_OSM_TILE_URL = new StringProperty(
|
---|
94 | "default.osm.tile.source.url", "https://{switch:a,b,c}.tile.openstreetmap.org/{zoom}/{x}/{y}.png");
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public List<TileSource> getTileSources() {
|
---|
98 | List<TileSource> result = imageryInfosToTileSources(ImageryLayerInfo.instance.getLayers().stream()
|
---|
99 | .filter(l -> l.getUrl().equals(DEFAULT_OSM_TILE_URL.get())).collect(Collectors.toList()));
|
---|
100 | if (result.isEmpty()) {
|
---|
101 | result.add(new OsmTileSource.Mapnik());
|
---|
102 | }
|
---|
103 | return result;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Returns the default OSM tile source.
|
---|
108 | * @return the default OSM tile source
|
---|
109 | */
|
---|
110 | public static TileSource get() {
|
---|
111 | return new DefaultOsmTileSourceProvider().getTileSources().get(0);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * TileSource provider - providing sources from imagery sources menu
|
---|
117 | */
|
---|
118 | public static class TMSTileSourceProvider extends AbstractImageryInfoBasedTileSourceProvider {
|
---|
119 | @Override
|
---|
120 | public List<ImageryInfo> getImageryInfos() {
|
---|
121 | return ImageryLayerInfo.instance.getLayers();
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * TileSource provider - providing sources from current layers
|
---|
127 | */
|
---|
128 | public static class CurrentLayersTileSourceProvider extends AbstractImageryInfoBasedTileSourceProvider {
|
---|
129 | @Override
|
---|
130 | public List<ImageryInfo> getImageryInfos() {
|
---|
131 | return MainApplication.getLayerManager().getLayers().stream().filter(
|
---|
132 | layer -> layer instanceof ImageryLayer
|
---|
133 | ).map(
|
---|
134 | layer -> ((ImageryLayer) layer).getInfo()
|
---|
135 | ).collect(Collectors.toList());
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected final transient TileLoader cachedLoader;
|
---|
140 | protected final transient OsmTileLoader uncachedLoader;
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Constructs a new {@code JosmMapViewer}.
|
---|
144 | */
|
---|
145 | public JosmMapViewer() {
|
---|
146 | Map<String, String> headers = new HashMap<>();
|
---|
147 | headers.put("User-Agent", Version.getInstance().getFullAgentString());
|
---|
148 |
|
---|
149 | TileLoaderFactory cachedLoaderFactory = AbstractCachedTileSourceLayer.getTileLoaderFactory("TMS", TMSCachedTileLoader.class);
|
---|
150 | if (cachedLoaderFactory != null) {
|
---|
151 | cachedLoader = cachedLoaderFactory.makeTileLoader(this, headers, TimeUnit.HOURS.toSeconds(1));
|
---|
152 | } else {
|
---|
153 | cachedLoader = null;
|
---|
154 | }
|
---|
155 |
|
---|
156 | uncachedLoader = new OsmTileLoader(this);
|
---|
157 | uncachedLoader.headers.putAll(headers);
|
---|
158 | setFileCacheEnabled(true);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Enables the disk tile cache.
|
---|
163 | * @param enabled true to enable, false to disable
|
---|
164 | */
|
---|
165 | public final void setFileCacheEnabled(boolean enabled) {
|
---|
166 | if (enabled && cachedLoader != null) {
|
---|
167 | setTileLoader(cachedLoader);
|
---|
168 | } else {
|
---|
169 | setTileLoader(uncachedLoader);
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Sets the maximum number of tiles that may be held in memory
|
---|
175 | * @param tiles The maximum number of tiles.
|
---|
176 | */
|
---|
177 | public final void setMaxTilesInMemory(int tiles) {
|
---|
178 | ((MemoryTileCache) getTileCache()).setCacheSize(tiles);
|
---|
179 | }
|
---|
180 | }
|
---|