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