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 | import javax.swing.JOptionPane;
|
---|
17 |
|
---|
18 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
19 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
20 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
21 | import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
24 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
25 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
26 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
27 | import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
|
---|
28 | import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
|
---|
29 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
30 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
31 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
32 | import 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 | */
|
---|
39 | public 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 | }
|
---|