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

Last change on this file since 11167 was 11167, checked in by simon04, 8 years ago

AbstractTileSourceLayer#getTileSource: drop method parameter, use field instead

  • Property svn:eol-style set to native
File size: 6.6 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.Set;
11import java.util.TreeSet;
12
13import javax.swing.AbstractAction;
14import javax.swing.Action;
15import javax.swing.JOptionPane;
16
17import org.apache.commons.jcs.access.CacheAccess;
18import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
21import org.openstreetmap.josm.data.imagery.AbstractWMSTileSource;
22import org.openstreetmap.josm.data.imagery.ImageryInfo;
23import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
24import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
25import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
26import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
27import org.openstreetmap.josm.data.preferences.BooleanProperty;
28import org.openstreetmap.josm.data.preferences.IntegerProperty;
29import org.openstreetmap.josm.data.projection.Projection;
30import org.openstreetmap.josm.gui.ExtendedDialog;
31import org.openstreetmap.josm.gui.layer.imagery.TileSourceDisplaySettings;
32
33/**
34 * This is a layer that grabs the current screen from an WMS server. The data
35 * fetched this way is tiled and managed to the disc to reduce server load.
36 *
37 */
38public class WMSLayer extends AbstractCachedTileSourceLayer<AbstractWMSTileSource> {
39 private static final String PREFERENCE_PREFIX = "imagery.wms";
40 /**
41 * Registers all setting properties
42 */
43 static {
44 new TileSourceDisplaySettings(PREFERENCE_PREFIX);
45 }
46
47 /** default tile size for WMS Layer */
48 public static final IntegerProperty PROP_IMAGE_SIZE = new IntegerProperty(PREFERENCE_PREFIX + ".imageSize", 512);
49
50 /** should WMS layer autozoom in default mode */
51 public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
52
53 private static final String CACHE_REGION_NAME = "WMS";
54
55 private final Set<String> supportedProjections;
56
57 /**
58 * Constructs a new {@code WMSLayer}.
59 * @param info ImageryInfo description of the layer
60 */
61 public WMSLayer(ImageryInfo info) {
62 super(info);
63 this.supportedProjections = new TreeSet<>(info.getServerProjections());
64 }
65
66 @Override
67 protected TileSourceDisplaySettings createDisplaySettings() {
68 return new TileSourceDisplaySettings(PREFERENCE_PREFIX);
69 }
70
71 @Override
72 public Action[] getMenuEntries() {
73 List<Action> ret = new ArrayList<>();
74 ret.addAll(Arrays.asList(super.getMenuEntries()));
75 ret.add(SeparatorLayerAction.INSTANCE);
76 ret.add(new LayerSaveAction(this));
77 ret.add(new LayerSaveAsAction(this));
78 ret.add(new BookmarkWmsAction());
79 return ret.toArray(new Action[ret.size()]);
80 }
81
82 @Override
83 protected AbstractWMSTileSource getTileSource() {
84 if (info.getImageryType() == ImageryType.WMS && info.getUrl() != null) {
85 TemplatedWMSTileSource.checkUrl(info.getUrl());
86 AbstractWMSTileSource tileSource = new TemplatedWMSTileSource(info);
87 info.setAttribution(tileSource);
88 return tileSource;
89 }
90 return null;
91 }
92
93 /**
94 * This action will add a WMS layer menu entry with the current WMS layer
95 * URL and name extended by the current resolution.
96 * When using the menu entry again, the WMS cache will be used properly.
97 */
98 public class BookmarkWmsAction extends AbstractAction {
99 /**
100 * Constructs a new {@code BookmarkWmsAction}.
101 */
102 public BookmarkWmsAction() {
103 super(tr("Set WMS Bookmark"));
104 }
105
106 @Override
107 public void actionPerformed(ActionEvent ev) {
108 ImageryLayerInfo.addLayer(new ImageryInfo(info));
109 }
110 }
111
112 @Override
113 public boolean isProjectionSupported(Projection proj) {
114 return supportedProjections == null || supportedProjections.isEmpty() || supportedProjections.contains(proj.toCode()) ||
115 (info.isEpsg4326To3857Supported() && supportedProjections.contains("EPSG:4326")
116 && "EPSG:3857".equals(Main.getProjection().toCode()));
117 }
118
119 @Override
120 public String nameSupportedProjections() {
121 StringBuilder ret = new StringBuilder();
122 for (String e: supportedProjections) {
123 ret.append(e).append(", ");
124 }
125 String appendix = "";
126
127 if (isReprojectionPossible()) {
128 appendix = ". <p>" + tr("JOSM will use EPSG:4326 to query the server, but results may vary "
129 + "depending on the WMS server") + "</p>";
130 }
131 return ret.substring(0, ret.length()-2) + appendix;
132 }
133
134 @Override
135 public void projectionChanged(Projection oldValue, Projection newValue) {
136 // do not call super - we need custom warning dialog
137
138 if (!isProjectionSupported(newValue)) {
139 String message =
140 "<html><body><p>" + tr("The layer {0} does not support the new projection {1}.", getName(), newValue.toCode()) +
141 "<p style='width: 450px; position: absolute; margin: 0px;'>" +
142 tr("Supported projections are: {0}", nameSupportedProjections()) + "</p>" +
143 "<p>" + tr("Change the projection again or remove the layer.");
144
145 ExtendedDialog warningDialog = new ExtendedDialog(Main.parent, tr("Warning"), new String[]{tr("OK")}).
146 setContent(message).
147 setIcon(JOptionPane.WARNING_MESSAGE);
148
149 if (isReprojectionPossible()) {
150 warningDialog.toggleEnable("imagery.wms.projectionSupportWarnings." + tileSource.getBaseUrl());
151 }
152 warningDialog.showDialog();
153 }
154
155 if (!newValue.equals(oldValue)) {
156 tileSource.initProjection(newValue);
157 }
158 }
159
160 @Override
161 protected Class<? extends TileLoader> getTileLoaderClass() {
162 return WMSCachedTileLoader.class;
163 }
164
165 @Override
166 protected String getCacheName() {
167 return CACHE_REGION_NAME;
168 }
169
170 /**
171 * @return cache region for WMS layer
172 */
173 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
174 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
175 }
176
177 private boolean isReprojectionPossible() {
178 return supportedProjections.contains("EPSG:4326") && "EPSG:3857".equals(Main.getProjection().toCode());
179 }
180}
Note: See TracBrowser for help on using the repository browser.