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