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

Last change on this file since 10997 was 10568, checked in by Don-vip, 8 years ago

fix #13169 - Extract imagery layer settings to new class (patch by michael2402, modified) - gsoc-core

  • 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.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(ImageryInfo info) {
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 Scale snap = scaleList.getSnapScale(Main.map.mapView.getScale(), false);
78 return Math.max(
79 getMinZoomLvl(),
80 Math.min(
81 snap != null ? snap.getIndex() : getMaxZoomLvl(),
82 getMaxZoomLvl()
83 )
84 );
85 }
86
87 @Override
88 protected int getMinZoomLvl() {
89 return 0;
90 }
91
92 @Override
93 public boolean isProjectionSupported(Projection proj) {
94 Set<String> supportedProjections = tileSource.getSupportedProjections();
95 return supportedProjections.contains(proj.toCode());
96 }
97
98 @Override
99 public String nameSupportedProjections() {
100 StringBuilder ret = new StringBuilder();
101 for (String e: tileSource.getSupportedProjections()) {
102 ret.append(e).append(", ");
103 }
104 return ret.length() > 2 ? ret.substring(0, ret.length()-2) : ret.toString();
105 }
106
107 @Override
108 public void projectionChanged(Projection oldValue, Projection newValue) {
109 super.projectionChanged(oldValue, newValue);
110 tileSource.initProjection(newValue);
111 }
112
113 @Override
114 protected Class<? extends TileLoader> getTileLoaderClass() {
115 return WMSCachedTileLoader.class;
116 }
117
118 @Override
119 protected String getCacheName() {
120 return CACHE_REGION_NAME;
121 }
122
123 /**
124 * @return cache region for WMTS layer
125 */
126 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
127 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
128 }
129
130 @Override
131 public ScaleList getNativeScales() {
132 return tileSource.getNativeScales();
133 }
134}
Note: See TracBrowser for help on using the repository browser.