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

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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