1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Set;
|
---|
12 | import java.util.TreeSet;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.Action;
|
---|
16 |
|
---|
17 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
18 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
19 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
20 | import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
23 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
24 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
25 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
26 | import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
|
---|
27 | import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
|
---|
28 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
29 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
30 | import 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 | */
|
---|
37 | public 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 | }
|
---|