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.TileXY;
|
---|
9 | import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
---|
10 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
11 | import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
16 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
17 | import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
|
---|
18 | import org.openstreetmap.josm.data.imagery.WMTSTileSource;
|
---|
19 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
20 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
21 | import org.openstreetmap.josm.gui.MapView;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * WMTS layer based on AbstractTileSourceLayer. Overrides few methods to align WMTS to Tile based computations
|
---|
25 | * but most magic is done within WMTSTileSource class.
|
---|
26 | *
|
---|
27 | * Full specification of the protocol available at:
|
---|
28 | * http://www.opengeospatial.org/standards/wmts
|
---|
29 | *
|
---|
30 | * @author Wiktor Niesiobędzki
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public class WMTSLayer extends AbstractCachedTileSourceLayer {
|
---|
34 | /**
|
---|
35 | * default setting of autozoom per layer
|
---|
36 | */
|
---|
37 | public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty("imagery.wmts.default_autozoom", true);
|
---|
38 | private static final String CACHE_REGION_NAME = "WMTS";
|
---|
39 |
|
---|
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 | autoZoom = PROP_DEFAULT_AUTOZOOM.get();
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected AbstractTMSTileSource getTileSource(ImageryInfo info) {
|
---|
52 | try {
|
---|
53 | if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) {
|
---|
54 | WMTSTileSource.checkUrl(info.getUrl());
|
---|
55 | WMTSTileSource tileSource = new WMTSTileSource(info);
|
---|
56 | info.setAttribution(tileSource);
|
---|
57 | return tileSource;
|
---|
58 | }
|
---|
59 | return null;
|
---|
60 | } catch (IOException e) {
|
---|
61 | Main.warn(e);
|
---|
62 | throw new IllegalArgumentException(e);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * @param zoom level of the tile
|
---|
68 | * @return how many pixels of the screen occupies one pixel of the tile
|
---|
69 | */
|
---|
70 | private double getTileToScreenRatio(int zoom) {
|
---|
71 | MapView mv = Main.map.mapView;
|
---|
72 | LatLon topLeft = mv.getLatLon(0, 0);
|
---|
73 | LatLon botLeft = mv.getLatLon(0, tileSource.getTileSize());
|
---|
74 |
|
---|
75 | TileXY topLeftTile = tileSource.latLonToTileXY(topLeft.toCoordinate(), zoom);
|
---|
76 |
|
---|
77 | ICoordinate north = tileSource.tileXYToLatLon(topLeftTile.getXIndex(), topLeftTile.getYIndex(), zoom);
|
---|
78 | ICoordinate south = tileSource.tileXYToLatLon(topLeftTile.getXIndex(), topLeftTile.getYIndex() + 1, zoom);
|
---|
79 |
|
---|
80 | return Math.abs((north.getLat() - south.getLat()) / (topLeft.lat() - botLeft.lat()));
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | protected int getBestZoom() {
|
---|
85 | if (!Main.isDisplayingMapView()) return 1;
|
---|
86 |
|
---|
87 | for (int i = getMinZoomLvl() + 1; i <= getMaxZoomLvl(); i++) {
|
---|
88 | double ret = getTileToScreenRatio(i);
|
---|
89 | if (ret < 1) {
|
---|
90 | return i - 1;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return getMaxZoomLvl();
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public boolean isProjectionSupported(Projection proj) {
|
---|
98 | Set<String> supportedProjections = ((WMTSTileSource) tileSource).getSupportedProjections();
|
---|
99 | return supportedProjections.contains(proj.toCode());
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public String nameSupportedProjections() {
|
---|
104 | StringBuilder ret = new StringBuilder();
|
---|
105 | for (String e: ((WMTSTileSource) tileSource).getSupportedProjections()) {
|
---|
106 | ret.append(e).append(", ");
|
---|
107 | }
|
---|
108 | return ret.length() > 2 ? ret.substring(0, ret.length()-2) : ret.toString();
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
113 | super.projectionChanged(oldValue, newValue);
|
---|
114 | ((WMTSTileSource) tileSource).initProjection(newValue);
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | protected Class<? extends TileLoader> getTileLoaderClass() {
|
---|
119 | return WMSCachedTileLoader.class;
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | protected String getCacheName() {
|
---|
124 | return CACHE_REGION_NAME;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * @return cache region for WMTS layer
|
---|
129 | */
|
---|
130 | public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
|
---|
131 | return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
|
---|
132 | }
|
---|
133 | }
|
---|