source: josm/trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java@ 8602

Last change on this file since 8602 was 8602, checked in by wiktorn, 9 years ago

Properly handle file based tile sources.

  • move getThreadFactory(String name) to Utils class, so it's easily usable across JOSM
  • rollback changes in [8485]
  • detect that we are working with filesystem TileSource in AbstractTileSourceLayer and if so, do not use JCS as caching mechanism, as tiles are already local
  • change return value of getTileSourceInfo in AbstractTileSourceLayer to AbstractTMSTileSource, as this is anyway - lowest we can work with
  • add test data for testing file base tile sources

closes: #11548

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.List;
10import java.util.Map;
11import java.util.Set;
12import java.util.TreeSet;
13
14import javax.swing.AbstractAction;
15import javax.swing.Action;
16
17import org.apache.commons.jcs.access.CacheAccess;
18import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
19import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
20import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
23import org.openstreetmap.josm.data.imagery.ImageryInfo;
24import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
25import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
26import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
27import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
28import org.openstreetmap.josm.data.preferences.BooleanProperty;
29import org.openstreetmap.josm.data.preferences.IntegerProperty;
30import org.openstreetmap.josm.data.projection.Projection;
31
32/**
33 * This is a layer that grabs the current screen from an WMS server. The data
34 * fetched this way is tiled and managed to the disc to reduce server load.
35 *
36 */
37public class WMSLayer extends AbstractCachedTileSourceLayer {
38 private static final String PREFERENCE_PREFIX = "imagery.wms.";
39
40 /** default tile size for WMS Layer */
41 public static final IntegerProperty PROP_IMAGE_SIZE = new IntegerProperty(PREFERENCE_PREFIX + "imageSize", 512);
42
43 /** should WMS layer autozoom in default mode */
44 public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + "default_autozoom", true);
45
46 /** limit of concurrent connections to WMS tile source (per source) */
47 public static final IntegerProperty THREAD_LIMIT = new IntegerProperty(PREFERENCE_PREFIX + "simultaneousConnections", 3);
48
49 private static final String CACHE_REGION_NAME = "WMS";
50
51 private Set<String> supportedProjections;
52
53 /**
54 * Constructs a new {@code WMSLayer}.
55 * @param info ImageryInfo description of the layer
56 */
57 public WMSLayer(ImageryInfo info) {
58 super(info);
59 this.supportedProjections = new TreeSet<>(info.getServerProjections());
60 this.autoZoom = PROP_DEFAULT_AUTOZOOM.get();
61
62 }
63
64 @Override
65 public Action[] getMenuEntries() {
66 List<Action> ret = new ArrayList<>();
67 ret.addAll(Arrays.asList(super.getMenuEntries()));
68 ret.add(SeparatorLayerAction.INSTANCE);
69 ret.add(new LayerSaveAction(this));
70 ret.add(new LayerSaveAsAction(this));
71 ret.add(new BookmarkWmsAction());
72 return ret.toArray(new Action[]{});
73 }
74
75 @Override
76 protected AbstractTMSTileSource getTileSource(ImageryInfo info) {
77 if (info.getImageryType() == ImageryType.WMS && info.getUrl() != null) {
78 TemplatedWMSTileSource.checkUrl(info.getUrl());
79 TemplatedWMSTileSource tileSource = new TemplatedWMSTileSource(info);
80 info.setAttribution(tileSource);
81 return tileSource;
82 }
83 return null;
84 }
85
86 /**
87 * This action will add a WMS layer menu entry with the current WMS layer
88 * URL and name extended by the current resolution.
89 * When using the menu entry again, the WMS cache will be used properly.
90 */
91 public class BookmarkWmsAction extends AbstractAction {
92 /**
93 * Constructs a new {@code BookmarkWmsAction}.
94 */
95 public BookmarkWmsAction() {
96 super(tr("Set WMS Bookmark"));
97 }
98
99 @Override
100 public void actionPerformed(ActionEvent ev) {
101 ImageryLayerInfo.addLayer(new ImageryInfo(info));
102 }
103 }
104
105 @Override
106 protected Map<String, String> getHeaders(TileSource tileSource) {
107 if (tileSource instanceof TemplatedWMSTileSource) {
108 return ((TemplatedWMSTileSource) tileSource).getHeaders();
109 }
110 return null;
111 }
112
113 @Override
114 public boolean isProjectionSupported(Projection proj) {
115 return supportedProjections == null || supportedProjections.isEmpty() || supportedProjections.contains(proj.toCode());
116 }
117
118 @Override
119 public String nameSupportedProjections() {
120 StringBuffer ret = new StringBuffer();
121 for (String e: supportedProjections) {
122 ret.append(e).append(", ");
123 }
124 String appendix = "";
125
126 if (supportedProjections.contains("EPSG:4326") && "EPSG:3857".equals(Main.getProjection().toCode())) {
127 appendix = ". " + tr("JOSM will use EPSG:4326 to query the server, but results may vary "
128 + "depending on the WMS server");
129 }
130 return ret.substring(0, ret.length()-2) + appendix;
131 }
132
133 @Override
134 public void projectionChanged(Projection oldValue, Projection newValue) {
135 super.projectionChanged(oldValue, newValue);
136
137 if (!newValue.equals(oldValue) && tileSource instanceof TemplatedWMSTileSource) {
138 ((TemplatedWMSTileSource) tileSource).initProjection(newValue);
139 }
140 }
141
142 /**
143 * Checks that WMS layer is a grabber-compatible one (HTML or WMS).
144 * @throws IllegalStateException if imagery time is neither HTML nor WMS
145 * @since 8068
146 * @deprecated not implemented anymore
147 */
148 @Deprecated
149 public void checkGrabberType() {
150 // not implemented
151 }
152
153 @Override
154 protected Class<? extends TileLoader> getTileLoaderClass() {
155 return WMSCachedTileLoader.class;
156 }
157
158 @Override
159 protected String getCacheName() {
160 return CACHE_REGION_NAME;
161 }
162
163 /**
164 * @return cache region for WMS layer
165 */
166 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
167 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
168 }
169}
Note: See TracBrowser for help on using the repository browser.