source: josm/trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java@ 8584

Last change on this file since 8584 was 8584, checked in by wiktorn, 9 years ago
  • added axis definition to ESPG projections definition, where it's not default (i.e. North/East)
  • added switchXY boolean in Projection interface
  • WMTS and WMS (1.3.0) uses now switchXY when doing requests against servers
  • better error reporting when there are problems with parsing the WMTS TileSource

Addresses: #10623

Now http://webgis.linz.at/WMTS/1.0.0/getCapabilities.xml should work.

Wallonie services still being debugged.

File size: 4.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import java.io.IOException;
5import java.util.Map;
6
7import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
8import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
9import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
10import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.coor.LatLon;
13import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
14import org.openstreetmap.josm.data.imagery.ImageryInfo;
15import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
16import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
17import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
18import org.openstreetmap.josm.data.imagery.WMTSTileSource;
19import org.openstreetmap.josm.data.preferences.BooleanProperty;
20import org.openstreetmap.josm.data.projection.Projection;
21import 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 */
33public 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}
Note: See TracBrowser for help on using the repository browser.