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.layer.imagery.TileSourceDisplaySettings;
|
---|
17 | import org.openstreetmap.josm.tools.Utils;
|
---|
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<WMTSTileSource> implements NativeScaleLayer {
|
---|
30 | private static final String PREFERENCE_PREFIX = "imagery.wmts";
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Registers all setting properties
|
---|
34 | */
|
---|
35 | static {
|
---|
36 | new TileSourceDisplaySettings(PREFERENCE_PREFIX);
|
---|
37 | }
|
---|
38 |
|
---|
39 | private static final String CACHE_REGION_NAME = "WMTS";
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Creates WMTS layer from ImageryInfo
|
---|
43 | * @param info Imagery Info describing the layer
|
---|
44 | */
|
---|
45 | public WMTSLayer(ImageryInfo info) {
|
---|
46 | super(info);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | protected TileSourceDisplaySettings createDisplaySettings() {
|
---|
51 | return new TileSourceDisplaySettings(PREFERENCE_PREFIX);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | protected WMTSTileSource getTileSource() {
|
---|
56 | try {
|
---|
57 | if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) {
|
---|
58 | WMTSTileSource.checkUrl(info.getUrl());
|
---|
59 | WMTSTileSource tileSource = new WMTSTileSource(info);
|
---|
60 | info.setAttribution(tileSource);
|
---|
61 | return tileSource;
|
---|
62 | }
|
---|
63 | return null;
|
---|
64 | } catch (IOException e) {
|
---|
65 | Main.warn(e);
|
---|
66 | throw new IllegalArgumentException(e);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public int getBestZoom() {
|
---|
72 | if (!Main.isDisplayingMapView())
|
---|
73 | return 0;
|
---|
74 | ScaleList scaleList = getNativeScales();
|
---|
75 | if (scaleList == null) {
|
---|
76 | return getMaxZoomLvl();
|
---|
77 | }
|
---|
78 | double displayScale = Main.map.mapView.getScale();
|
---|
79 | if (coordinateConverter.requiresReprojection()) {
|
---|
80 | displayScale *= Main.getProjection().getMetersPerUnit();
|
---|
81 | }
|
---|
82 | Scale snap = scaleList.getSnapScale(displayScale, false);
|
---|
83 | return Utils.clamp(snap != null ? snap.getIndex() : getMaxZoomLvl(),
|
---|
84 | getMinZoomLvl(), getMaxZoomLvl());
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | protected int getMinZoomLvl() {
|
---|
89 | return 0;
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public Collection<String> getNativeProjections() {
|
---|
94 | return tileSource.getSupportedProjections();
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
99 | super.projectionChanged(oldValue, newValue);
|
---|
100 | tileSource.initProjection(newValue);
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | protected Class<? extends TileLoader> getTileLoaderClass() {
|
---|
105 | return WMSCachedTileLoader.class;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | protected String getCacheName() {
|
---|
110 | return CACHE_REGION_NAME;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * @return cache region for WMTS layer
|
---|
115 | */
|
---|
116 | public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
|
---|
117 | return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public ScaleList getNativeScales() {
|
---|
122 | return tileSource.getNativeScales();
|
---|
123 | }
|
---|
124 | }
|
---|