source: josm/trunk/src/org/openstreetmap/josm/gui/layer/TMSLayer.java@ 17318

Last change on this file since 17318 was 16553, checked in by Don-vip, 4 years ago

see #19334 - javadoc fixes + protected constructors for abstract classes

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import java.util.Collection;
5import java.util.Collections;
6import java.util.stream.Collectors;
7import java.util.stream.IntStream;
8
9import org.apache.commons.jcs3.access.CacheAccess;
10import org.openstreetmap.gui.jmapviewer.JMapViewer;
11import org.openstreetmap.gui.jmapviewer.OsmMercator;
12import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
13import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
14import org.openstreetmap.gui.jmapviewer.tilesources.ScanexTileSource;
15import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
16import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
17import org.openstreetmap.josm.data.imagery.CachedAttributionBingAerialTileSource;
18import org.openstreetmap.josm.data.imagery.ImageryInfo;
19import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
20import org.openstreetmap.josm.data.imagery.JosmTemplatedTMSTileSource;
21import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
22import org.openstreetmap.josm.data.preferences.BooleanProperty;
23import org.openstreetmap.josm.data.preferences.IntegerProperty;
24import 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 */
35public 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}
Note: See TracBrowser for help on using the repository browser.