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

Last change on this file since 15716 was 15716, checked in by simon04, 4 years ago

Java 8: use String.join

  • Property svn:eol-style set to native
File size: 8.0 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.data.cache.BufferedImageCacheEntry;
19import org.openstreetmap.josm.data.coor.LatLon;
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.imagery.WMSEndpointTileSource;
27import org.openstreetmap.josm.data.preferences.BooleanProperty;
28import org.openstreetmap.josm.data.preferences.IntegerProperty;
29import org.openstreetmap.josm.data.projection.Projection;
30import org.openstreetmap.josm.data.projection.ProjectionRegistry;
31import org.openstreetmap.josm.data.projection.Projections;
32import org.openstreetmap.josm.gui.MainApplication;
33import org.openstreetmap.josm.gui.layer.imagery.TileSourceDisplaySettings;
34import org.openstreetmap.josm.tools.CheckParameterUtil;
35import org.openstreetmap.josm.tools.Logging;
36
37/**
38 * This is a layer that grabs the current screen from an WMS server. The data
39 * fetched this way is tiled and managed to the disc to reduce server load.
40 *
41 */
42public class WMSLayer extends AbstractCachedTileSourceLayer<AbstractWMSTileSource> {
43 private static final String PREFERENCE_PREFIX = "imagery.wms";
44 /**
45 * Registers all setting properties
46 */
47 static {
48 new TileSourceDisplaySettings(PREFERENCE_PREFIX);
49 }
50
51 /** default tile size for WMS Layer */
52 public static final IntegerProperty PROP_IMAGE_SIZE = new IntegerProperty(PREFERENCE_PREFIX + ".imageSize", 512);
53
54 /** should WMS layer autozoom in default mode */
55 public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
56
57 private static final String CACHE_REGION_NAME = "WMS";
58
59 private List<String> serverProjections;
60
61 /**
62 * Constructs a new {@code WMSLayer}.
63 * @param info ImageryInfo description of the layer
64 */
65 public WMSLayer(ImageryInfo info) {
66 super(info);
67 CheckParameterUtil.ensureThat(
68 info.getImageryType() == ImageryType.WMS || info.getImageryType() == ImageryType.WMS_ENDPOINT, "ImageryType is WMS");
69 CheckParameterUtil.ensureParameterNotNull(info.getUrl(), "info.url");
70 if (info.getImageryType() == ImageryType.WMS) {
71 TemplatedWMSTileSource.checkUrl(info.getUrl());
72
73 }
74 this.serverProjections = new ArrayList<>(info.getServerProjections());
75 }
76
77 @Override
78 protected TileSourceDisplaySettings createDisplaySettings() {
79 return new TileSourceDisplaySettings(PREFERENCE_PREFIX);
80 }
81
82 @Override
83 public Action[] getMenuEntries() {
84 List<Action> ret = new ArrayList<>();
85 ret.addAll(Arrays.asList(super.getMenuEntries()));
86 ret.add(SeparatorLayerAction.INSTANCE);
87 ret.add(new LayerSaveAction(this));
88 ret.add(new LayerSaveAsAction(this));
89 ret.add(new BookmarkWmsAction());
90 return ret.toArray(new Action[0]);
91 }
92
93 @Override
94 protected AbstractWMSTileSource getTileSource() {
95 AbstractWMSTileSource tileSource;
96 if (info.getImageryType() == ImageryType.WMS) {
97 tileSource = new TemplatedWMSTileSource(info, chooseProjection(ProjectionRegistry.getProjection()));
98 } else {
99 /*
100 * Chicken-and-egg problem. We want to create tile source, but supported projections we can get only
101 * from this tile source. So create tilesource first with dummy ProjectionRegistry.getProjection(), and then update
102 * once we update server projections.
103 *
104 * Thus:
105 * * it is not required to provide projections for wms_endpoint imagery types
106 * * we always use current definitions returned by server
107 */
108 WMSEndpointTileSource endpointTileSource = new WMSEndpointTileSource(info, ProjectionRegistry.getProjection());
109 this.serverProjections = endpointTileSource.getServerProjections();
110 endpointTileSource.setTileProjection(chooseProjection(ProjectionRegistry.getProjection()));
111 tileSource = endpointTileSource;
112 }
113 info.setAttribution(tileSource);
114 return tileSource;
115 }
116
117 /**
118 * This action will add a WMS layer menu entry with the current WMS layer
119 * URL and name extended by the current resolution.
120 * When using the menu entry again, the WMS cache will be used properly.
121 */
122 public class BookmarkWmsAction extends AbstractAction {
123 /**
124 * Constructs a new {@code BookmarkWmsAction}.
125 */
126 public BookmarkWmsAction() {
127 super(tr("Set WMS Bookmark"));
128 }
129
130 @Override
131 public void actionPerformed(ActionEvent ev) {
132 ImageryLayerInfo.addLayer(new ImageryInfo(info));
133 }
134 }
135
136 @Override
137 public Collection<String> getNativeProjections() {
138 return serverProjections;
139 }
140
141 @Override
142 public void projectionChanged(Projection oldValue, Projection newValue) {
143 super.projectionChanged(oldValue, newValue);
144 Projection tileProjection = chooseProjection(newValue);
145 if (!Objects.equals(tileSource.getTileProjection(), tileProjection)) {
146 tileSource.setTileProjection(tileProjection);
147 }
148 }
149
150 private Projection chooseProjection(Projection requested) {
151 if (serverProjections.contains(requested.toCode())) {
152 return requested;
153 } else {
154 LatLon center = MainApplication.isDisplayingMapView() ?
155 requested.eastNorth2latlon(MainApplication.getMap().mapView.getCenter()) : null;
156 Projection firstNonNullproj = null;
157 Projection firstProjInBounds = null;
158 for (String code : serverProjections) {
159 Projection proj = Projections.getProjectionByCode(code);
160 if (proj != null) {
161 if (firstNonNullproj == null) {
162 firstNonNullproj = proj;
163 }
164 if (center != null && proj.getWorldBoundsLatLon().contains(center)) {
165 firstProjInBounds = proj;
166 break;
167 }
168 }
169 }
170 if (firstProjInBounds != null) {
171 return selectProjection(firstProjInBounds);
172 } else if (firstNonNullproj != null) {
173 return selectProjection(firstNonNullproj);
174 }
175 Logging.warn(tr("Unable to find supported projection for layer {0}. Using {1}.", getName(), requested.toCode()));
176 return requested;
177 }
178 }
179
180 private Projection selectProjection(Projection proj) {
181 Logging.info(tr("Reprojecting layer {0} from {1} to {2}. For best image quality and performance,"
182 + " switch to one of the supported projections: {3}",
183 getName(), proj.toCode(), ProjectionRegistry.getProjection().toCode(), String.join(", ", getNativeProjections())));
184 return proj;
185 }
186
187 @Override
188 protected Class<? extends TileLoader> getTileLoaderClass() {
189 return WMSCachedTileLoader.class;
190 }
191
192 @Override
193 protected String getCacheName() {
194 return CACHE_REGION_NAME;
195 }
196
197 /**
198 * @return cache region for WMS layer
199 */
200 public static CacheAccess<String, BufferedImageCacheEntry> getCache() {
201 return AbstractCachedTileSourceLayer.getCache(CACHE_REGION_NAME);
202 }
203}
Note: See TracBrowser for help on using the repository browser.