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

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

Add appendix to supported layers message

  • Property svn:eol-style set to native
File size: 5.5 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.io.IOException;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.List;
11import java.util.Map;
12
13import javax.swing.AbstractAction;
14import javax.swing.Action;
15
16import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
17import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
18import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
21import org.openstreetmap.josm.data.imagery.ImageryInfo;
22import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
23import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
24import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
25import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
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;
30
31/**
32 * This is a layer that grabs the current screen from an WMS server. The data
33 * fetched this way is tiled and managed to the disc to reduce server load.
34 *
35 */
36public class WMSLayer extends AbstractTileSourceLayer {
37 /** default tile size for WMS Layer */
38 public static final IntegerProperty PROP_IMAGE_SIZE = new IntegerProperty("imagery.wms.imageSize", 512);
39 /** should WMS layer autozoom in default mode */
40 public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty("imagery.wms.default_autozoom", true);
41 private List<String> supportedProjections;
42
43 /**
44 * Constructs a new {@code WMSLayer}.
45 * @param info ImageryInfo description of the layer
46 */
47
48
49 public WMSLayer(ImageryInfo info) {
50 super(info);
51 this.supportedProjections = info.getServerProjections();
52 }
53
54 @Override
55 public Action[] getMenuEntries() {
56 List<Action> ret = new ArrayList<>();
57 ret.addAll(Arrays.asList(super.getMenuEntries()));
58 ret.add(SeparatorLayerAction.INSTANCE);
59 ret.add(new LayerSaveAction(this));
60 ret.add(new LayerSaveAsAction(this));
61 ret.add(new BookmarkWmsAction());
62 return ret.toArray(new Action[]{});
63 }
64
65 @Override
66 protected TileSource getTileSource(ImageryInfo info) throws IllegalArgumentException {
67 if (info.getImageryType() == ImageryType.WMS && info.getUrl() != null) {
68 TemplatedWMSTileSource.checkUrl(info.getUrl());
69 TemplatedWMSTileSource tileSource = new TemplatedWMSTileSource(info);
70 info.setAttribution(tileSource);
71 return tileSource;
72 }
73 return null;
74 }
75
76 /**
77 * This action will add a WMS layer menu entry with the current WMS layer
78 * URL and name extended by the current resolution.
79 * When using the menu entry again, the WMS cache will be used properly.
80 */
81 public class BookmarkWmsAction extends AbstractAction {
82 /**
83 * Constructs a new {@code BookmarkWmsAction}.
84 */
85 public BookmarkWmsAction() {
86 super(tr("Set WMS Bookmark"));
87 }
88
89 @Override
90 public void actionPerformed(ActionEvent ev) {
91 ImageryLayerInfo.addLayer(new ImageryInfo(info));
92 }
93 }
94
95 /**
96 * Checks that WMS layer is a grabber-compatible one (HTML or WMS).
97 * @throws IllegalStateException if imagery time is neither HTML nor WMS
98 * @since 8068
99 */
100 public void checkGrabberType() {
101 }
102
103 private static TileLoaderFactory loaderFactory = new CachedTileLoaderFactory("WMS") {
104 @Override
105 protected TileLoader getLoader(TileLoaderListener listener, String cacheName, int connectTimeout,
106 int readTimeout, Map<String, String> headers, String cacheDir) throws IOException {
107 return new WMSCachedTileLoader(listener, cacheName, connectTimeout, readTimeout, headers, cacheDir);
108 }
109
110 };
111
112 @Override
113 protected TileLoaderFactory getTileLoaderFactory() {
114 return loaderFactory;
115 }
116
117 @Override
118 protected Map<String, String> getHeaders(TileSource tileSource) {
119 if (tileSource instanceof TemplatedWMSTileSource) {
120 return ((TemplatedWMSTileSource) tileSource).getHeaders();
121 }
122 return null;
123 }
124
125 @Override
126 public boolean isProjectionSupported(Projection proj) {
127 return supportedProjections == null || supportedProjections.isEmpty() || supportedProjections.contains(proj.toCode());
128 }
129
130 @Override
131 public String nameSupportedProjections() {
132 StringBuffer ret = new StringBuffer();
133 for (String e: supportedProjections) {
134 ret.append(e).append(", ");
135 }
136 String appendix = "";
137
138 if (supportedProjections.contains("EPSG:4326") && "EPSG:3857".equals(Main.getProjection().toCode())) {
139 appendix = ". " + tr("JOSM will use EPSG:4326 to query the server, but results may vary "
140 + "depending on the WMS server");
141 }
142 return ret.substring(0, ret.length()-2) + appendix;
143 }
144
145 @Override
146 public void projectionChanged(Projection oldValue, Projection newValue) {
147 super.projectionChanged(oldValue, newValue);
148
149 if (!newValue.equals(oldValue) && tileSource instanceof TemplatedWMSTileSource) {
150 ((TemplatedWMSTileSource) tileSource).initProjection(newValue);
151 }
152
153 }
154
155}
Note: See TracBrowser for help on using the repository browser.