1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 |
|
---|
6 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
7 | import org.openstreetmap.gui.jmapviewer.TileXY;
|
---|
8 | import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
---|
9 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
10 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
14 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
15 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
16 | import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
|
---|
17 | import org.openstreetmap.josm.data.imagery.WMTSTileSource;
|
---|
18 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
19 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
20 | import org.openstreetmap.josm.gui.MapView;
|
---|
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 {
|
---|
33 | /**
|
---|
34 | * default setting of autozoom per layer
|
---|
35 | */
|
---|
36 | public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty("imagery.wmts.default_autozoom", true);
|
---|
37 | private static final String CACHE_REGION_NAME = "WMTS";
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Creates WMTS layer from ImageryInfo
|
---|
42 | * @param info Imagery Info describing the layer
|
---|
43 | */
|
---|
44 | public WMTSLayer(ImageryInfo info) {
|
---|
45 | super(info);
|
---|
46 | autoZoom = PROP_DEFAULT_AUTOZOOM.get();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | protected TileSource getTileSource(ImageryInfo info) {
|
---|
51 | try {
|
---|
52 | if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) {
|
---|
53 | WMTSTileSource.checkUrl(info.getUrl());
|
---|
54 | WMTSTileSource tileSource = new WMTSTileSource(info);
|
---|
55 | info.setAttribution(tileSource);
|
---|
56 | return tileSource;
|
---|
57 | }
|
---|
58 | return null;
|
---|
59 | } catch (IOException e) {
|
---|
60 | Main.warn(e);
|
---|
61 | throw new IllegalArgumentException(e);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * @param zoom level of the tile
|
---|
67 | * @return how many pixels of the screen occupies one pixel of the tile
|
---|
68 | */
|
---|
69 | private double getTileToScreenRatio(int zoom) {
|
---|
70 | MapView mv = Main.map.mapView;
|
---|
71 | LatLon topLeft = mv.getLatLon(0, 0);
|
---|
72 | LatLon botLeft = mv.getLatLon(0, tileSource.getTileSize());
|
---|
73 |
|
---|
74 | TileXY topLeftTile = tileSource.latLonToTileXY(topLeft.toCoordinate(), zoom);
|
---|
75 |
|
---|
76 | ICoordinate north = tileSource.tileXYToLatLon(topLeftTile.getXIndex(), topLeftTile.getYIndex(), zoom);
|
---|
77 | ICoordinate south = tileSource.tileXYToLatLon(topLeftTile.getXIndex(), topLeftTile.getYIndex() + 1, zoom);
|
---|
78 |
|
---|
79 | return Math.abs((north.getLat() - south.getLat()) / (topLeft.lat() - botLeft.lat()));
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | protected int getBestZoom() {
|
---|
84 | if (!Main.isDisplayingMapView()) return 1;
|
---|
85 |
|
---|
86 | for (int i = getMinZoomLvl() + 1; i <= getMaxZoomLvl(); i++) {
|
---|
87 | double ret = getTileToScreenRatio(i);
|
---|
88 | if (ret < 1) {
|
---|
89 | return i - 1;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return getMaxZoomLvl();
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public boolean isProjectionSupported(Projection proj) {
|
---|
97 | return ((WMTSTileSource) tileSource).getSupportedProjections().contains(proj.toCode());
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public String nameSupportedProjections() {
|
---|
102 | StringBuilder ret = new StringBuilder();
|
---|
103 | for (String e: ((WMTSTileSource) tileSource).getSupportedProjections()) {
|
---|
104 | ret.append(e).append(", ");
|
---|
105 | }
|
---|
106 | return ret.substring(0, ret.length()-2);
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
111 | super.projectionChanged(oldValue, newValue);
|
---|
112 | ((WMTSTileSource) tileSource).initProjection(newValue);
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | protected Class<? extends TileLoader> getTileLoaderClass() {
|
---|
117 | return WMSCachedTileLoader.class;
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | protected String getCacheName() {
|
---|
122 | return CACHE_REGION_NAME;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * @return cache region for WMTS layer
|
---|
127 | */
|
---|
128 | public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
|
---|
129 | return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
|
---|
130 | }
|
---|
131 | }
|
---|