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.josm.Main;
|
---|
21 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
22 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
23 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
24 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
25 | import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
|
---|
26 | import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
|
---|
27 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
28 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
29 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
30 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
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<TemplatedWMSTileSource> {
|
---|
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 | private static final String CACHE_REGION_NAME = "WMS";
|
---|
47 |
|
---|
48 | private final Set<String> supportedProjections;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Constructs a new {@code WMSLayer}.
|
---|
52 | * @param info ImageryInfo description of the layer
|
---|
53 | */
|
---|
54 | public WMSLayer(ImageryInfo info) {
|
---|
55 | super(info);
|
---|
56 | this.supportedProjections = new TreeSet<>(info.getServerProjections());
|
---|
57 | this.autoZoom = PROP_DEFAULT_AUTOZOOM.get();
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public Action[] getMenuEntries() {
|
---|
62 | List<Action> ret = new ArrayList<>();
|
---|
63 | ret.addAll(Arrays.asList(super.getMenuEntries()));
|
---|
64 | ret.add(SeparatorLayerAction.INSTANCE);
|
---|
65 | ret.add(new LayerSaveAction(this));
|
---|
66 | ret.add(new LayerSaveAsAction(this));
|
---|
67 | ret.add(new BookmarkWmsAction());
|
---|
68 | return ret.toArray(new Action[]{});
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | protected TemplatedWMSTileSource getTileSource(ImageryInfo info) {
|
---|
73 | if (info.getImageryType() == ImageryType.WMS && info.getUrl() != null) {
|
---|
74 | TemplatedWMSTileSource.checkUrl(info.getUrl());
|
---|
75 | TemplatedWMSTileSource tileSource = new TemplatedWMSTileSource(info);
|
---|
76 | info.setAttribution(tileSource);
|
---|
77 | return tileSource;
|
---|
78 | }
|
---|
79 | return null;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * This action will add a WMS layer menu entry with the current WMS layer
|
---|
84 | * URL and name extended by the current resolution.
|
---|
85 | * When using the menu entry again, the WMS cache will be used properly.
|
---|
86 | */
|
---|
87 | public class BookmarkWmsAction extends AbstractAction {
|
---|
88 | /**
|
---|
89 | * Constructs a new {@code BookmarkWmsAction}.
|
---|
90 | */
|
---|
91 | public BookmarkWmsAction() {
|
---|
92 | super(tr("Set WMS Bookmark"));
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public void actionPerformed(ActionEvent ev) {
|
---|
97 | ImageryLayerInfo.addLayer(new ImageryInfo(info));
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | protected Map<String, String> getHeaders(TemplatedWMSTileSource tileSource) {
|
---|
103 | return tileSource.getHeaders();
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public boolean isProjectionSupported(Projection proj) {
|
---|
108 | return supportedProjections == null || supportedProjections.isEmpty() || supportedProjections.contains(proj.toCode()) ||
|
---|
109 | (info.isEpsg4326To3857Supported() && supportedProjections.contains("EPSG:4326")
|
---|
110 | && "EPSG:3857".equals(Main.getProjection().toCode()));
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public String nameSupportedProjections() {
|
---|
115 | StringBuilder ret = new StringBuilder();
|
---|
116 | for (String e: supportedProjections) {
|
---|
117 | ret.append(e).append(", ");
|
---|
118 | }
|
---|
119 | String appendix = "";
|
---|
120 |
|
---|
121 | if (isReprojectionPossible()) {
|
---|
122 | appendix = ". <p>" + tr("JOSM will use EPSG:4326 to query the server, but results may vary "
|
---|
123 | + "depending on the WMS server") + "</p>";
|
---|
124 | }
|
---|
125 | return ret.substring(0, ret.length()-2) + appendix;
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
130 | // do not call super - we need custom warning dialog
|
---|
131 |
|
---|
132 | if (!isProjectionSupported(newValue)) {
|
---|
133 | String message =
|
---|
134 | "<html><body><p>" + tr("The layer {0} does not support the new projection {1}.", getName(), newValue.toCode()) +
|
---|
135 | "<p style='width: 450px; position: absolute; margin: 0px;'>" +
|
---|
136 | tr("Supported projections are: {0}", nameSupportedProjections()) + "</p>" +
|
---|
137 | "<p>" + tr("Change the projection again or remove the layer.");
|
---|
138 |
|
---|
139 | ExtendedDialog warningDialog = new ExtendedDialog(Main.parent, tr("Warning"), new String[]{tr("OK")}).
|
---|
140 | setContent(message).
|
---|
141 | setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
142 |
|
---|
143 | if (isReprojectionPossible()) {
|
---|
144 | warningDialog.toggleEnable("imagery.wms.projectionSupportWarnings." + tileSource.getBaseUrl());
|
---|
145 | }
|
---|
146 | warningDialog.showDialog();
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (!newValue.equals(oldValue)) {
|
---|
150 | tileSource.initProjection(newValue);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | protected Class<? extends TileLoader> getTileLoaderClass() {
|
---|
156 | return WMSCachedTileLoader.class;
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | protected String getCacheName() {
|
---|
161 | return CACHE_REGION_NAME;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * @return cache region for WMS layer
|
---|
166 | */
|
---|
167 | public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
|
---|
168 | return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
|
---|
169 | }
|
---|
170 |
|
---|
171 | private boolean isReprojectionPossible() {
|
---|
172 | return supportedProjections.contains("EPSG:4326") && "EPSG:3857".equals(Main.getProjection().toCode());
|
---|
173 | }
|
---|
174 | }
|
---|