source: josm/trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java@ 12669

Last change on this file since 12669 was 12630, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import java.io.IOException;
5import java.util.Collection;
6
7import org.apache.commons.jcs.access.CacheAccess;
8import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
11import org.openstreetmap.josm.data.imagery.ImageryInfo;
12import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
13import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
14import org.openstreetmap.josm.data.imagery.WMTSTileSource;
15import org.openstreetmap.josm.data.projection.Projection;
16import org.openstreetmap.josm.gui.MainApplication;
17import org.openstreetmap.josm.gui.layer.imagery.TileSourceDisplaySettings;
18import org.openstreetmap.josm.tools.Logging;
19import org.openstreetmap.josm.tools.Utils;
20
21/**
22 * WMTS layer based on AbstractTileSourceLayer. Overrides few methods to align WMTS to Tile based computations
23 * but most magic is done within WMTSTileSource class.
24 *
25 * Full specification of the protocol available at:
26 * http://www.opengeospatial.org/standards/wmts
27 *
28 * @author Wiktor Niesiobędzki
29 *
30 */
31public class WMTSLayer extends AbstractCachedTileSourceLayer<WMTSTileSource> implements NativeScaleLayer {
32 private static final String PREFERENCE_PREFIX = "imagery.wmts";
33
34 /**
35 * Registers all setting properties
36 */
37 static {
38 new TileSourceDisplaySettings(PREFERENCE_PREFIX);
39 }
40
41 private static final String CACHE_REGION_NAME = "WMTS";
42
43 /**
44 * Creates WMTS layer from ImageryInfo
45 * @param info Imagery Info describing the layer
46 */
47 public WMTSLayer(ImageryInfo info) {
48 super(info);
49 }
50
51 @Override
52 protected TileSourceDisplaySettings createDisplaySettings() {
53 return new TileSourceDisplaySettings(PREFERENCE_PREFIX);
54 }
55
56 @Override
57 protected WMTSTileSource getTileSource() {
58 try {
59 if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) {
60 WMTSTileSource.checkUrl(info.getUrl());
61 WMTSTileSource tileSource = new WMTSTileSource(info);
62 info.setAttribution(tileSource);
63 return tileSource;
64 }
65 return null;
66 } catch (IOException e) {
67 Logging.warn(e);
68 throw new IllegalArgumentException(e);
69 }
70 }
71
72 @Override
73 public int getBestZoom() {
74 if (!MainApplication.isDisplayingMapView())
75 return 0;
76 ScaleList scaleList = getNativeScales();
77 if (scaleList == null) {
78 return getMaxZoomLvl();
79 }
80 double displayScale = MainApplication.getMap().mapView.getScale();
81 if (coordinateConverter.requiresReprojection()) {
82 displayScale *= Main.getProjection().getMetersPerUnit();
83 }
84 Scale snap = scaleList.getSnapScale(displayScale, false);
85 return Utils.clamp(snap != null ? snap.getIndex() : getMaxZoomLvl(),
86 getMinZoomLvl(), getMaxZoomLvl());
87 }
88
89 @Override
90 protected int getMinZoomLvl() {
91 return 0;
92 }
93
94 @Override
95 public Collection<String> getNativeProjections() {
96 return tileSource.getSupportedProjections();
97 }
98
99 @Override
100 public void projectionChanged(Projection oldValue, Projection newValue) {
101 super.projectionChanged(oldValue, newValue);
102 tileSource.initProjection(newValue);
103 }
104
105 @Override
106 protected Class<? extends TileLoader> getTileLoaderClass() {
107 return WMSCachedTileLoader.class;
108 }
109
110 @Override
111 protected String getCacheName() {
112 return CACHE_REGION_NAME;
113 }
114
115 /**
116 * @return cache region for WMTS layer
117 */
118 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
119 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
120 }
121
122 @Override
123 public ScaleList getNativeScales() {
124 return tileSource.getNativeScales();
125 }
126}
Note: See TracBrowser for help on using the repository browser.