1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.stream.Collectors;
|
---|
7 | import java.util.stream.IntStream;
|
---|
8 |
|
---|
9 | import org.apache.commons.jcs3.access.CacheAccess;
|
---|
10 | import org.openstreetmap.gui.jmapviewer.JMapViewer;
|
---|
11 | import org.openstreetmap.gui.jmapviewer.OsmMercator;
|
---|
12 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
13 | import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
|
---|
14 | import org.openstreetmap.gui.jmapviewer.tilesources.ScanexTileSource;
|
---|
15 | import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
|
---|
16 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
17 | import org.openstreetmap.josm.data.imagery.CachedAttributionBingAerialTileSource;
|
---|
18 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
19 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
20 | import org.openstreetmap.josm.data.imagery.JosmTemplatedTMSTileSource;
|
---|
21 | import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
|
---|
22 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
23 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
24 | import org.openstreetmap.josm.tools.Logging;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Class that displays a slippy map layer.
|
---|
28 | *
|
---|
29 | * @author Frederik Ramm
|
---|
30 | * @author LuVar <lubomir.varga@freemap.sk>
|
---|
31 | * @author Dave Hansen <dave@sr71.net>
|
---|
32 | * @author Upliner <upliner@gmail.com>
|
---|
33 | * @since 3715
|
---|
34 | */
|
---|
35 | public class TMSLayer extends AbstractCachedTileSourceLayer<TMSTileSource> implements NativeScaleLayer {
|
---|
36 | private static final String CACHE_REGION_NAME = "TMS";
|
---|
37 |
|
---|
38 | private static final String PREFERENCE_PREFIX = "imagery.tms";
|
---|
39 |
|
---|
40 | /** minimum zoom level for TMS layer */
|
---|
41 | public static final IntegerProperty PROP_MIN_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".min_zoom_lvl",
|
---|
42 | AbstractTileSourceLayer.PROP_MIN_ZOOM_LVL.get());
|
---|
43 | /** maximum zoom level for TMS layer */
|
---|
44 | public static final IntegerProperty PROP_MAX_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".max_zoom_lvl",
|
---|
45 | AbstractTileSourceLayer.PROP_MAX_ZOOM_LVL.get());
|
---|
46 | /** shall TMS layers be added to download dialog */
|
---|
47 | public static final BooleanProperty PROP_ADD_TO_SLIPPYMAP_CHOOSER = new BooleanProperty(PREFERENCE_PREFIX + ".add_to_slippymap_chooser",
|
---|
48 | true);
|
---|
49 | /** override minimum/maximum zoom level with those supported by JMapViewer, as these might be used in slippymap chooser */
|
---|
50 | public static final int MAX_ZOOM = JMapViewer.MAX_ZOOM;
|
---|
51 | public static final int MIN_ZOOM = JMapViewer.MIN_ZOOM;
|
---|
52 |
|
---|
53 | private static final ScaleList nativeScaleList = initNativeScaleList();
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Create a layer based on ImageryInfo
|
---|
57 | * @param info description of the layer
|
---|
58 | */
|
---|
59 | public TMSLayer(ImageryInfo info) {
|
---|
60 | super(info);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Creates and returns a new TileSource instance depending on the {@link ImageryType}
|
---|
65 | * of the {@link ImageryInfo} object specified in the constructor.
|
---|
66 | *
|
---|
67 | * If no appropriate TileSource is found, null is returned.
|
---|
68 | * Currently supported ImageryType are {@link ImageryType#TMS},
|
---|
69 | * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
|
---|
70 | *
|
---|
71 | *
|
---|
72 | * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
|
---|
73 | * @throws IllegalArgumentException if url from imagery info is null or invalid
|
---|
74 | */
|
---|
75 | @Override
|
---|
76 | protected TMSTileSource getTileSource() {
|
---|
77 | return getTileSourceStatic(info, () -> {
|
---|
78 | Logging.debug("Attribution loaded, running loadAllErrorTiles");
|
---|
79 | this.loadAllErrorTiles(false);
|
---|
80 | });
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public Collection<String> getNativeProjections() {
|
---|
85 | return Collections.singletonList("EPSG:3857");
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Creates and returns a new TileSource instance depending on the {@link ImageryType}
|
---|
90 | * of the passed ImageryInfo object.
|
---|
91 | *
|
---|
92 | * If no appropriate TileSource is found, null is returned.
|
---|
93 | * Currently supported ImageryType are {@link ImageryType#TMS},
|
---|
94 | * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
|
---|
95 | *
|
---|
96 | * @param info imagery info
|
---|
97 | * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
|
---|
98 | * @throws IllegalArgumentException if url from imagery info is null or invalid
|
---|
99 | */
|
---|
100 | public static AbstractTMSTileSource getTileSourceStatic(ImageryInfo info) {
|
---|
101 | return getTileSourceStatic(info, null);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Creates and returns a new TileSource instance depending on the {@link ImageryType}
|
---|
106 | * of the passed ImageryInfo object.
|
---|
107 | *
|
---|
108 | * If no appropriate TileSource is found, null is returned.
|
---|
109 | * Currently supported ImageryType are {@link ImageryType#TMS},
|
---|
110 | * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
|
---|
111 | *
|
---|
112 | * @param info imagery info
|
---|
113 | * @param attributionLoadedTask task to be run once attribution is loaded, might be null, if nothing special shall happen
|
---|
114 | * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
|
---|
115 | * @throws IllegalArgumentException if url from imagery info is null or invalid
|
---|
116 | */
|
---|
117 | public static TMSTileSource getTileSourceStatic(ImageryInfo info, Runnable attributionLoadedTask) {
|
---|
118 | if (info.getImageryType() == ImageryType.TMS) {
|
---|
119 | JosmTemplatedTMSTileSource.checkUrl(info.getUrl());
|
---|
120 | TMSTileSource t = new JosmTemplatedTMSTileSource(info);
|
---|
121 | info.setAttribution(t);
|
---|
122 | return t;
|
---|
123 | } else if (info.getImageryType() == ImageryType.BING) {
|
---|
124 | return new CachedAttributionBingAerialTileSource(info, attributionLoadedTask);
|
---|
125 | } else if (info.getImageryType() == ImageryType.SCANEX) {
|
---|
126 | return new ScanexTileSource(info);
|
---|
127 | }
|
---|
128 | return null;
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | protected Class<? extends TileLoader> getTileLoaderClass() {
|
---|
133 | return TMSCachedTileLoader.class;
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | protected String getCacheName() {
|
---|
138 | return CACHE_REGION_NAME;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Returns cache for TMS region.
|
---|
143 | * @return cache for TMS region
|
---|
144 | */
|
---|
145 | public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
|
---|
146 | return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public ScaleList getNativeScales() {
|
---|
151 | return nativeScaleList;
|
---|
152 | }
|
---|
153 |
|
---|
154 | private static ScaleList initNativeScaleList() {
|
---|
155 | Collection<Double> scales = IntStream.rangeClosed(AbstractTileSourceLayer.MIN_ZOOM, AbstractTileSourceLayer.MAX_ZOOM)
|
---|
156 | .mapToDouble(zoom -> OsmMercator.EARTH_RADIUS * Math.PI * 2 / Math.pow(2, zoom) / OsmMercator.DEFAUL_TILE_SIZE)
|
---|
157 | .boxed()
|
---|
158 | .collect(Collectors.toList());
|
---|
159 | return new ScaleList(scales);
|
---|
160 | }
|
---|
161 | }
|
---|