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

Last change on this file since 11846 was 11789, checked in by bastiK, 7 years ago

WMTS: fix autozoom in WGS84

  • Property svn:eol-style set to native
File size: 4.2 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.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.layer.imagery.TileSourceDisplaySettings;
17
18/**
19 * WMTS layer based on AbstractTileSourceLayer. Overrides few methods to align WMTS to Tile based computations
20 * but most magic is done within WMTSTileSource class.
21 *
22 * Full specification of the protocol available at:
23 * http://www.opengeospatial.org/standards/wmts
24 *
25 * @author Wiktor Niesiobędzki
26 *
27 */
28public class WMTSLayer extends AbstractCachedTileSourceLayer<WMTSTileSource> implements NativeScaleLayer {
29 private static final String PREFERENCE_PREFIX = "imagery.wmts";
30
31 /**
32 * Registers all setting properties
33 */
34 static {
35 new TileSourceDisplaySettings(PREFERENCE_PREFIX);
36 }
37
38 private static final String CACHE_REGION_NAME = "WMTS";
39
40 /**
41 * Creates WMTS layer from ImageryInfo
42 * @param info Imagery Info describing the layer
43 */
44 public WMTSLayer(ImageryInfo info) {
45 super(info);
46 }
47
48 @Override
49 protected TileSourceDisplaySettings createDisplaySettings() {
50 return new TileSourceDisplaySettings(PREFERENCE_PREFIX);
51 }
52
53 @Override
54 protected WMTSTileSource getTileSource() {
55 try {
56 if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) {
57 WMTSTileSource.checkUrl(info.getUrl());
58 WMTSTileSource tileSource = new WMTSTileSource(info);
59 info.setAttribution(tileSource);
60 return tileSource;
61 }
62 return null;
63 } catch (IOException e) {
64 Main.warn(e);
65 throw new IllegalArgumentException(e);
66 }
67 }
68
69 @Override
70 protected int getBestZoom() {
71 if (!Main.isDisplayingMapView())
72 return 0;
73 ScaleList scaleList = getNativeScales();
74 if (scaleList == null) {
75 return getMaxZoomLvl();
76 }
77 double displayScale = Main.map.mapView.getScale() * Main.getProjection().getMetersPerUnit(); // meter per pixel
78 Scale snap = scaleList.getSnapScale(displayScale, false);
79 return Math.max(
80 getMinZoomLvl(),
81 Math.min(
82 snap != null ? snap.getIndex() : getMaxZoomLvl(),
83 getMaxZoomLvl()
84 )
85 );
86 }
87
88 @Override
89 protected int getMinZoomLvl() {
90 return 0;
91 }
92
93 @Override
94 public boolean isProjectionSupported(Projection proj) {
95 Set<String> supportedProjections = tileSource.getSupportedProjections();
96 return supportedProjections.contains(proj.toCode());
97 }
98
99 @Override
100 public String nameSupportedProjections() {
101 StringBuilder ret = new StringBuilder();
102 for (String e: tileSource.getSupportedProjections()) {
103 ret.append(e).append(", ");
104 }
105 return ret.length() > 2 ? ret.substring(0, ret.length()-2) : ret.toString();
106 }
107
108 @Override
109 public void projectionChanged(Projection oldValue, Projection newValue) {
110 super.projectionChanged(oldValue, newValue);
111 tileSource.initProjection(newValue);
112 }
113
114 @Override
115 protected Class<? extends TileLoader> getTileLoaderClass() {
116 return WMSCachedTileLoader.class;
117 }
118
119 @Override
120 protected String getCacheName() {
121 return CACHE_REGION_NAME;
122 }
123
124 /**
125 * @return cache region for WMTS layer
126 */
127 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
128 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
129 }
130
131 @Override
132 public ScaleList getNativeScales() {
133 return tileSource.getNativeScales();
134 }
135}
Note: See TracBrowser for help on using the repository browser.