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

Last change on this file since 9277 was 9135, checked in by Don-vip, 8 years ago

checkstyle

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