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

Last change on this file since 9818 was 9818, checked in by wiktorn, 8 years ago

Snap scale to mercator zoom levels.

See #12350

Patch submitted by: kolesar

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import java.io.IOException;
5import java.util.Set;
6
7import org.apache.commons.jcs.access.CacheAccess;
8import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
9import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
12import org.openstreetmap.josm.data.imagery.ImageryInfo;
13import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
14import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
15import org.openstreetmap.josm.data.imagery.WMTSTileSource;
16import org.openstreetmap.josm.data.preferences.BooleanProperty;
17import org.openstreetmap.josm.data.projection.Projection;
18
19/**
20 * WMTS layer based on AbstractTileSourceLayer. Overrides few methods to align WMTS to Tile based computations
21 * but most magic is done within WMTSTileSource class.
22 *
23 * Full specification of the protocol available at:
24 * http://www.opengeospatial.org/standards/wmts
25 *
26 * @author Wiktor Niesiobędzki
27 *
28 */
29public class WMTSLayer extends AbstractCachedTileSourceLayer implements NativeScaleLayer {
30 /**
31 * default setting of autozoom per layer
32 */
33 public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty("imagery.wmts.default_autozoom", true);
34 private static final String CACHE_REGION_NAME = "WMTS";
35
36 /**
37 * Creates WMTS layer from ImageryInfo
38 * @param info Imagery Info describing the layer
39 */
40 public WMTSLayer(ImageryInfo info) {
41 super(info);
42 autoZoom = PROP_DEFAULT_AUTOZOOM.get();
43 }
44
45 @Override
46 protected AbstractTMSTileSource getTileSource(ImageryInfo info) {
47 try {
48 if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) {
49 WMTSTileSource.checkUrl(info.getUrl());
50 WMTSTileSource tileSource = new WMTSTileSource(info);
51 info.setAttribution(tileSource);
52 return tileSource;
53 }
54 return null;
55 } catch (IOException e) {
56 Main.warn(e);
57 throw new IllegalArgumentException(e);
58 }
59 }
60
61 @Override
62 protected int getBestZoom() {
63 if (!Main.isDisplayingMapView()) return 0;
64 ScaleList scaleList = getNativeScales();
65 for (int i = scaleList.size()-1; i >= 0; i--) {
66 Scale scale = scaleList.get(i);
67 if (scale.scale >= Main.map.mapView.getScale()) {
68 return i;
69 }
70 }
71 return 0;
72 }
73
74 @Override
75 protected int getMaxZoomLvl() {
76 return getNativeScales().size()-1;
77 }
78
79 @Override
80 protected int getMinZoomLvl() {
81 return 0;
82 }
83
84 @Override
85 public boolean isProjectionSupported(Projection proj) {
86 Set<String> supportedProjections = ((WMTSTileSource) tileSource).getSupportedProjections();
87 return supportedProjections.contains(proj.toCode());
88 }
89
90 @Override
91 public String nameSupportedProjections() {
92 StringBuilder ret = new StringBuilder();
93 for (String e: ((WMTSTileSource) tileSource).getSupportedProjections()) {
94 ret.append(e).append(", ");
95 }
96 return ret.length() > 2 ? ret.substring(0, ret.length()-2) : ret.toString();
97 }
98
99 @Override
100 public void projectionChanged(Projection oldValue, Projection newValue) {
101 super.projectionChanged(oldValue, newValue);
102 ((WMTSTileSource) 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 ((WMTSTileSource) tileSource).getNativeScales();
125 }
126}
Note: See TracBrowser for help on using the repository browser.