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

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

see #12350 - fix coverity 1352420, 1352421, 1352422, 1352423, 1352424 (potential NPEs)

  • Property svn:eol-style set to native
File size: 3.9 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.preferences.BooleanProperty;
16import org.openstreetmap.josm.data.projection.Projection;
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 /**
30 * default setting of autozoom per layer
31 */
32 public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty("imagery.wmts.default_autozoom", true);
33 private static final String CACHE_REGION_NAME = "WMTS";
34
35 /**
36 * Creates WMTS layer from ImageryInfo
37 * @param info Imagery Info describing the layer
38 */
39 public WMTSLayer(ImageryInfo info) {
40 super(info);
41 autoZoom = PROP_DEFAULT_AUTOZOOM.get();
42 }
43
44 @Override
45 protected WMTSTileSource getTileSource(ImageryInfo info) {
46 try {
47 if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) {
48 WMTSTileSource.checkUrl(info.getUrl());
49 WMTSTileSource tileSource = new WMTSTileSource(info);
50 info.setAttribution(tileSource);
51 return tileSource;
52 }
53 return null;
54 } catch (IOException e) {
55 Main.warn(e);
56 throw new IllegalArgumentException(e);
57 }
58 }
59
60 @Override
61 protected int getBestZoom() {
62 if (!Main.isDisplayingMapView())
63 return 0;
64 Scale snap = getNativeScales().getSnapScale(Main.map.mapView.getScale(), false);
65 return Math.max(
66 getMinZoomLvl(),
67 Math.min(
68 snap != null ? snap.getIndex() : getMaxZoomLvl(),
69 getMaxZoomLvl()
70 )
71 );
72 }
73
74 @Override
75 protected int getMinZoomLvl() {
76 return 0;
77 }
78
79 @Override
80 public boolean isProjectionSupported(Projection proj) {
81 Set<String> supportedProjections = tileSource.getSupportedProjections();
82 return supportedProjections.contains(proj.toCode());
83 }
84
85 @Override
86 public String nameSupportedProjections() {
87 StringBuilder ret = new StringBuilder();
88 for (String e: tileSource.getSupportedProjections()) {
89 ret.append(e).append(", ");
90 }
91 return ret.length() > 2 ? ret.substring(0, ret.length()-2) : ret.toString();
92 }
93
94 @Override
95 public void projectionChanged(Projection oldValue, Projection newValue) {
96 super.projectionChanged(oldValue, newValue);
97 tileSource.initProjection(newValue);
98 }
99
100 @Override
101 protected Class<? extends TileLoader> getTileLoaderClass() {
102 return WMSCachedTileLoader.class;
103 }
104
105 @Override
106 protected String getCacheName() {
107 return CACHE_REGION_NAME;
108 }
109
110 /**
111 * @return cache region for WMTS layer
112 */
113 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
114 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
115 }
116
117 @Override
118 public ScaleList getNativeScales() {
119 return tileSource.getNativeScales();
120 }
121}
Note: See TracBrowser for help on using the repository browser.