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.Map;
|
---|
6 |
|
---|
7 | import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
---|
8 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
9 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
|
---|
10 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
|
---|
14 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
15 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
16 | import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
|
---|
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 AbstractTileSourceLayer {
|
---|
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 |
|
---|
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 | }
|
---|
47 |
|
---|
48 | private static TileLoaderFactory loaderFactory = new CachedTileLoaderFactory("WMTS") {
|
---|
49 | @Override
|
---|
50 | protected TileLoader getLoader(TileLoaderListener listener, String cacheName, int connectTimeout,
|
---|
51 | int readTimeout, Map<String, String> headers, String cacheDir) throws IOException {
|
---|
52 | return new WMSCachedTileLoader(listener, cacheName, connectTimeout, readTimeout, headers, cacheDir);
|
---|
53 | }
|
---|
54 |
|
---|
55 | };
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | protected TileLoaderFactory getTileLoaderFactory() {
|
---|
59 | return loaderFactory;
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | protected TileSource getTileSource(ImageryInfo info) {
|
---|
64 | try {
|
---|
65 | if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) {
|
---|
66 | WMTSTileSource.checkUrl(info.getUrl());
|
---|
67 | WMTSTileSource tileSource = new WMTSTileSource(info);
|
---|
68 | info.setAttribution(tileSource);
|
---|
69 | return tileSource;
|
---|
70 | }
|
---|
71 | return null;
|
---|
72 | } catch (IOException e) {
|
---|
73 | Main.warn(e);
|
---|
74 | throw new IllegalArgumentException(e);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * @param zoom level of the tile
|
---|
80 | * @return how many pixels of the screen occupies one pixel of the tile
|
---|
81 | */
|
---|
82 | private double getTileToScreenRatio(int zoom) {
|
---|
83 | ICoordinate north = tileSource.tileXYToLatLon(0, 0, zoom);
|
---|
84 | ICoordinate south = tileSource.tileXYToLatLon(0, 1, zoom);
|
---|
85 |
|
---|
86 | MapView mv = Main.map.mapView;
|
---|
87 | LatLon topLeft = mv.getLatLon(0, 0);
|
---|
88 | LatLon botLeft = mv.getLatLon(0, tileSource.getTileSize());
|
---|
89 |
|
---|
90 | return Math.abs((north.getLat() - south.getLat()) / (topLeft.lat() - botLeft.lat()));
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | protected int getBestZoom() {
|
---|
95 | if (!Main.isDisplayingMapView()) return 1;
|
---|
96 |
|
---|
97 | for (int i = getMinZoomLvl(); i <= getMaxZoomLvl(); i++) {
|
---|
98 | double ret = getTileToScreenRatio(i);
|
---|
99 | if (ret < 1) {
|
---|
100 | return i;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | return getMaxZoomLvl();
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public boolean isProjectionSupported(Projection proj) {
|
---|
108 | return ((WMTSTileSource) tileSource).getSupportedProjections().contains(proj.toCode());
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public String nameSupportedProjections() {
|
---|
113 | StringBuilder ret = new StringBuilder();
|
---|
114 | for (String e: ((WMTSTileSource) tileSource).getSupportedProjections()) {
|
---|
115 | ret.append(e).append(", ");
|
---|
116 | }
|
---|
117 | return ret.substring(0, ret.length()-2);
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
122 | super.projectionChanged(oldValue, newValue);
|
---|
123 | ((WMTSTileSource) tileSource).initProjection(newValue);
|
---|
124 | }
|
---|
125 | }
|
---|