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

Last change on this file since 11535 was 11167, checked in by simon04, 8 years ago

AbstractTileSourceLayer#getTileSource: drop method parameter, use field instead

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collection;
8
9import org.apache.commons.jcs.access.CacheAccess;
10import org.openstreetmap.gui.jmapviewer.OsmMercator;
11import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
12import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
13import org.openstreetmap.gui.jmapviewer.tilesources.ScanexTileSource;
14import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
15import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource;
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
18import org.openstreetmap.josm.data.imagery.CachedAttributionBingAerialTileSource;
19import org.openstreetmap.josm.data.imagery.ImageryInfo;
20import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
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.data.projection.Projection;
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
50 private static final ScaleList nativeScaleList = initNativeScaleList();
51
52 /**
53 * Create a layer based on ImageryInfo
54 * @param info description of the layer
55 */
56 public TMSLayer(ImageryInfo info) {
57 super(info);
58 }
59
60 /**
61 * Creates and returns a new TileSource instance depending on the {@link ImageryType}
62 * of the {@link ImageryInfo} object specified in the constructor.
63 *
64 * If no appropriate TileSource is found, null is returned.
65 * Currently supported ImageryType are {@link ImageryType#TMS},
66 * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
67 *
68 *
69 * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
70 * @throws IllegalArgumentException if url from imagery info is null or invalid
71 */
72 @Override
73 protected TMSTileSource getTileSource() {
74 return getTileSourceStatic(info, () -> {
75 Main.debug("Attribution loaded, running loadAllErrorTiles");
76 this.loadAllErrorTiles(false);
77 });
78 }
79
80 @Override
81 public final boolean isProjectionSupported(Projection proj) {
82 return "EPSG:3857".equals(proj.toCode()) || "EPSG:4326".equals(proj.toCode());
83 }
84
85 @Override
86 public final String nameSupportedProjections() {
87 return tr("EPSG:4326 and Mercator projection are supported");
88 }
89
90 /**
91 * Creates and returns a new TileSource instance depending on the {@link ImageryType}
92 * of the passed ImageryInfo object.
93 *
94 * If no appropriate TileSource is found, null is returned.
95 * Currently supported ImageryType are {@link ImageryType#TMS},
96 * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
97 *
98 * @param info imagery info
99 * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
100 * @throws IllegalArgumentException if url from imagery info is null or invalid
101 */
102 public static AbstractTMSTileSource getTileSourceStatic(ImageryInfo info) {
103 return getTileSourceStatic(info, null);
104 }
105
106 /**
107 * Creates and returns a new TileSource instance depending on the {@link ImageryType}
108 * of the passed ImageryInfo object.
109 *
110 * If no appropriate TileSource is found, null is returned.
111 * Currently supported ImageryType are {@link ImageryType#TMS},
112 * {@link ImageryType#BING}, {@link ImageryType#SCANEX}.
113 *
114 * @param info imagery info
115 * @param attributionLoadedTask task to be run once attribution is loaded, might be null, if nothing special shall happen
116 * @return a new TileSource instance or null if no TileSource for the ImageryInfo/ImageryType could be found.
117 * @throws IllegalArgumentException if url from imagery info is null or invalid
118 */
119 public static TMSTileSource getTileSourceStatic(ImageryInfo info, Runnable attributionLoadedTask) {
120 if (info.getImageryType() == ImageryType.TMS) {
121 TemplatedTMSTileSource.checkUrl(info.getUrl());
122 TMSTileSource t = new TemplatedTMSTileSource(info);
123 info.setAttribution(t);
124 return t;
125 } else if (info.getImageryType() == ImageryType.BING) {
126 return new CachedAttributionBingAerialTileSource(info, attributionLoadedTask);
127 } else if (info.getImageryType() == ImageryType.SCANEX) {
128 return new ScanexTileSource(info);
129 }
130 return null;
131 }
132
133 @Override
134 protected Class<? extends TileLoader> getTileLoaderClass() {
135 return TMSCachedTileLoader.class;
136 }
137
138 @Override
139 protected String getCacheName() {
140 return CACHE_REGION_NAME;
141 }
142
143 /**
144 * @return cache for TMS region
145 */
146 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
147 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
148 }
149
150 @Override
151 public ScaleList getNativeScales() {
152 return nativeScaleList;
153 }
154
155 private static ScaleList initNativeScaleList() {
156 Collection<Double> scales = new ArrayList<>(AbstractTileSourceLayer.MAX_ZOOM);
157 for (int zoom = AbstractTileSourceLayer.MIN_ZOOM; zoom <= AbstractTileSourceLayer.MAX_ZOOM; zoom++) {
158 double scale = OsmMercator.EARTH_RADIUS * Math.PI * 2 / Math.pow(2, zoom) / OsmMercator.DEFAUL_TILE_SIZE;
159 scales.add(scale);
160 }
161 return new ScaleList(scales);
162 }
163 }
Note: See TracBrowser for help on using the repository browser.