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

Last change on this file since 12340 was 11956, checked in by bastiK, 7 years ago

see #7427 - repaint after clear tile cache action

  • Property svn:eol-style set to native
File size: 5.9 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.Collection;
10import java.util.List;
11import java.util.Objects;
12
13import javax.swing.AbstractAction;
14import javax.swing.Action;
15
16import org.apache.commons.jcs.access.CacheAccess;
17import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
20import org.openstreetmap.josm.data.imagery.AbstractWMSTileSource;
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.WMSCachedTileLoader;
26import org.openstreetmap.josm.data.preferences.BooleanProperty;
27import org.openstreetmap.josm.data.preferences.IntegerProperty;
28import org.openstreetmap.josm.data.projection.Projection;
29import org.openstreetmap.josm.data.projection.Projections;
30import org.openstreetmap.josm.gui.layer.imagery.TileSourceDisplaySettings;
31import org.openstreetmap.josm.tools.CheckParameterUtil;
32import org.openstreetmap.josm.tools.Utils;
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<AbstractWMSTileSource> {
40 private static final String PREFERENCE_PREFIX = "imagery.wms";
41 /**
42 * Registers all setting properties
43 */
44 static {
45 new TileSourceDisplaySettings(PREFERENCE_PREFIX);
46 }
47
48 /** default tile size for WMS Layer */
49 public static final IntegerProperty PROP_IMAGE_SIZE = new IntegerProperty(PREFERENCE_PREFIX + ".imageSize", 512);
50
51 /** should WMS layer autozoom in default mode */
52 public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
53
54 private static final String CACHE_REGION_NAME = "WMS";
55
56 private final List<String> serverProjections;
57
58 /**
59 * Constructs a new {@code WMSLayer}.
60 * @param info ImageryInfo description of the layer
61 */
62 public WMSLayer(ImageryInfo info) {
63 super(info);
64 CheckParameterUtil.ensureThat(info.getImageryType() == ImageryType.WMS, "ImageryType is WMS");
65 CheckParameterUtil.ensureParameterNotNull(info.getUrl(), "info.url");
66 TemplatedWMSTileSource.checkUrl(info.getUrl());
67 this.serverProjections = new ArrayList<>(info.getServerProjections());
68 }
69
70 @Override
71 protected TileSourceDisplaySettings createDisplaySettings() {
72 return new TileSourceDisplaySettings(PREFERENCE_PREFIX);
73 }
74
75 @Override
76 public Action[] getMenuEntries() {
77 List<Action> ret = new ArrayList<>();
78 ret.addAll(Arrays.asList(super.getMenuEntries()));
79 ret.add(SeparatorLayerAction.INSTANCE);
80 ret.add(new LayerSaveAction(this));
81 ret.add(new LayerSaveAsAction(this));
82 ret.add(new BookmarkWmsAction());
83 return ret.toArray(new Action[ret.size()]);
84 }
85
86 @Override
87 protected AbstractWMSTileSource getTileSource() {
88 AbstractWMSTileSource tileSource = new TemplatedWMSTileSource(
89 info, chooseProjection(Main.getProjection()));
90 info.setAttribution(tileSource);
91 return tileSource;
92 }
93
94 /**
95 * This action will add a WMS layer menu entry with the current WMS layer
96 * URL and name extended by the current resolution.
97 * When using the menu entry again, the WMS cache will be used properly.
98 */
99 public class BookmarkWmsAction extends AbstractAction {
100 /**
101 * Constructs a new {@code BookmarkWmsAction}.
102 */
103 public BookmarkWmsAction() {
104 super(tr("Set WMS Bookmark"));
105 }
106
107 @Override
108 public void actionPerformed(ActionEvent ev) {
109 ImageryLayerInfo.addLayer(new ImageryInfo(info));
110 }
111 }
112
113 @Override
114 public Collection<String> getNativeProjections() {
115 return serverProjections;
116 }
117
118 @Override
119 public void projectionChanged(Projection oldValue, Projection newValue) {
120 super.projectionChanged(oldValue, newValue);
121 Projection tileProjection = chooseProjection(newValue);
122 if (!Objects.equals(tileSource.getTileProjection(), tileProjection)) {
123 tileSource.setTileProjection(tileProjection);
124 }
125 }
126
127 private Projection chooseProjection(Projection requested) {
128 if (serverProjections.contains(requested.toCode())) {
129 return requested;
130 } else {
131 for (String code : serverProjections) {
132 Projection proj = Projections.getProjectionByCode(code);
133 if (proj != null) {
134 Main.info(tr("Reprojecting layer {0} from {1} to {2}. For best image quality and performance,"
135 + " switch to one of the supported projections: {3}",
136 getName(), proj.toCode(), Main.getProjection().toCode(), Utils.join(", ", getNativeProjections())));
137 return proj;
138 }
139 }
140 Main.warn(tr("Unable to find supported projection for layer {0}. Using {1}.", getName(), requested.toCode()));
141 return requested;
142 }
143 }
144
145 @Override
146 protected Class<? extends TileLoader> getTileLoaderClass() {
147 return WMSCachedTileLoader.class;
148 }
149
150 @Override
151 protected String getCacheName() {
152 return CACHE_REGION_NAME;
153 }
154
155 /**
156 * @return cache region for WMS layer
157 */
158 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
159 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
160 }
161}
Note: See TracBrowser for help on using the repository browser.