1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.Set;
|
---|
6 |
|
---|
7 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
8 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
9 | import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
12 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
13 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
14 | import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
|
---|
15 | import org.openstreetmap.josm.data.imagery.WMTSTileSource;
|
---|
16 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
17 | import 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 | */
|
---|
29 | public 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 | }
|
---|