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.Collection;
|
---|
6 |
|
---|
7 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
8 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
11 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
12 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
13 | import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
|
---|
14 | import org.openstreetmap.josm.data.imagery.WMTSTileSource;
|
---|
15 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
16 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
17 | import org.openstreetmap.josm.gui.layer.imagery.TileSourceDisplaySettings;
|
---|
18 | import org.openstreetmap.josm.tools.Logging;
|
---|
19 | import org.openstreetmap.josm.tools.Utils;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * WMTS layer based on AbstractTileSourceLayer. Overrides few methods to align WMTS to Tile based computations
|
---|
23 | * but most magic is done within WMTSTileSource class.
|
---|
24 | *
|
---|
25 | * Full specification of the protocol available at:
|
---|
26 | * http://www.opengeospatial.org/standards/wmts
|
---|
27 | *
|
---|
28 | * @author Wiktor Niesiobędzki
|
---|
29 | *
|
---|
30 | */
|
---|
31 | public class WMTSLayer extends AbstractCachedTileSourceLayer<WMTSTileSource> implements NativeScaleLayer {
|
---|
32 | private static final String PREFERENCE_PREFIX = "imagery.wmts";
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Registers all setting properties
|
---|
36 | */
|
---|
37 | static {
|
---|
38 | new TileSourceDisplaySettings(PREFERENCE_PREFIX);
|
---|
39 | }
|
---|
40 |
|
---|
41 | private static final String CACHE_REGION_NAME = "WMTS";
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Creates WMTS layer from ImageryInfo
|
---|
45 | * @param info Imagery Info describing the layer
|
---|
46 | */
|
---|
47 | public WMTSLayer(ImageryInfo info) {
|
---|
48 | super(info);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | protected TileSourceDisplaySettings createDisplaySettings() {
|
---|
53 | return new TileSourceDisplaySettings(PREFERENCE_PREFIX);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | protected WMTSTileSource getTileSource() {
|
---|
58 | try {
|
---|
59 | if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) {
|
---|
60 | WMTSTileSource.checkUrl(info.getUrl());
|
---|
61 | WMTSTileSource tileSource = new WMTSTileSource(info);
|
---|
62 | info.setAttribution(tileSource);
|
---|
63 | return tileSource;
|
---|
64 | }
|
---|
65 | return null;
|
---|
66 | } catch (IOException e) {
|
---|
67 | Logging.warn(e);
|
---|
68 | throw new IllegalArgumentException(e);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public int getBestZoom() {
|
---|
74 | if (!MainApplication.isDisplayingMapView())
|
---|
75 | return 0;
|
---|
76 | ScaleList scaleList = getNativeScales();
|
---|
77 | if (scaleList == null) {
|
---|
78 | return getMaxZoomLvl();
|
---|
79 | }
|
---|
80 | double displayScale = MainApplication.getMap().mapView.getScale();
|
---|
81 | if (coordinateConverter.requiresReprojection()) {
|
---|
82 | displayScale *= Main.getProjection().getMetersPerUnit();
|
---|
83 | }
|
---|
84 | Scale snap = scaleList.getSnapScale(displayScale, false);
|
---|
85 | return Utils.clamp(snap != null ? snap.getIndex() : getMaxZoomLvl(),
|
---|
86 | getMinZoomLvl(), getMaxZoomLvl());
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | protected int getMinZoomLvl() {
|
---|
91 | return 0;
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public Collection<String> getNativeProjections() {
|
---|
96 | return tileSource.getSupportedProjections();
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
101 | super.projectionChanged(oldValue, newValue);
|
---|
102 | 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 tileSource.getNativeScales();
|
---|
125 | }
|
---|
126 | }
|
---|