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

Last change on this file since 8526 was 8526, checked in by wiktorn, 9 years ago

Introduce WMS layer based on TMS. (closes: #11255)

HEADS UP: After this patch you need to manually remove JAX-B generated file/class: org/w3/_2001/xmlschema/Adapter1.java to compile the tree again.

  • create AbstractTileSourceLayer based on TMSLayer as a base for TMS, WMS and (future) WMTS layers, (addresses #11459)
  • WMS layer now uses JCS Caching (closes: #7363)
  • introduce new conversion methods in TileSource, that convert both X and Y (lat and lon) in one call. This is necessary for other than PseudoMercator projections
    • introduce TileXY class that represents X and Y indexes of tile in tile matrix/space
    • mark old conversion methods as deprecated
    • refactor JMapViewer and JOSM to new methods
    • change use of Coordinate class to ICoordinate where appropiate
  • extract CachedAttributionBingAerialTileSource to separate file
  • create TemplatedWMSTileSource that provides the WMS Layer with square (according to current projection) tiles (closes: #11572, closes: #7682, addresses: #5454)
  • implemented precaching imagery along GPX track for AbstractTileSourceLayer, so now it work for both - WMS and TMS (closes: #9154)
  • implemented common righ-click menu on map view, as well on layer list (closes #3591)
  • create separate build commands for JMapViewer classes to easily spot, when josm classes are used within JMapViewer
  • remove unnecessary classes of previous WMS implementation - GeorefImage, wms-cache.xsd (and JAXB task from build), WMSCache, WMSRequest, WMSGrabber, HTMLGrabber, WMSException
  • Property svn:eol-style set to native
File size: 5.6 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.io.IOException;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.List;
11import java.util.Map;
12
13import javax.swing.AbstractAction;
14import javax.swing.Action;
15
16import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
17import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
18import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
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.TileLoaderFactory;
26import org.openstreetmap.josm.data.imagery.WMSCachedTileLoader;
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.ProjectionChangeListener;
31import org.openstreetmap.josm.gui.MapView;
32import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
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 AbstractTileSourceLayer {
40 /** default tile size for WMS Layer */
41 public static final IntegerProperty PROP_IMAGE_SIZE = new IntegerProperty("imagery.wms.imageSize", 512);
42 /** should WMS layer autozoom in default mode */
43 public static final BooleanProperty PROP_DEFAULT_AUTOZOOM = new BooleanProperty("imagery.wms.default_autozoom", true);
44
45 /**
46 * Constructs a new {@code WMSLayer}.
47 * @param info ImageryInfo description of the layer
48 */
49 public WMSLayer(ImageryInfo info) {
50 super(info);
51 }
52
53 @Override
54 public void hookUpMapView() {
55 super.hookUpMapView();
56 final ProjectionChangeListener listener = new ProjectionChangeListener() {
57 @Override
58 public void projectionChanged(Projection oldValue, Projection newValue) {
59 if (!oldValue.equals(newValue) && tileSource instanceof TemplatedWMSTileSource) {
60 ((TemplatedWMSTileSource)tileSource).initProjection(newValue);
61 }
62
63 }
64 };
65 Main.addProjectionChangeListener(listener);
66
67 MapView.addLayerChangeListener(new LayerChangeListener() {
68 @Override
69 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
70 // empty
71 }
72
73 @Override
74 public void layerAdded(Layer newLayer) {
75 // empty
76 }
77
78 @Override
79 public void layerRemoved(Layer oldLayer) {
80 if (oldLayer == WMSLayer.this) {
81 Main.removeProjectionChangeListener(listener);
82 }
83 }
84 });
85 }
86
87 @Override
88 public Action[] getMenuEntries() {
89 List<Action> ret = new ArrayList<>();
90 ret.addAll(Arrays.asList(super.getMenuEntries()));
91 ret.add(SeparatorLayerAction.INSTANCE);
92 ret.add(new LayerSaveAction(this));
93 ret.add(new LayerSaveAsAction(this));
94 ret.add(new BookmarkWmsAction());
95 return ret.toArray(new Action[]{});
96 }
97
98
99 @Override
100 protected TileSource getTileSource(ImageryInfo info) throws IllegalArgumentException {
101 if (info.getImageryType() == ImageryType.WMS && info.getUrl() != null) {
102 TemplatedWMSTileSource.checkUrl(info.getUrl());
103 TemplatedWMSTileSource tileSource = new TemplatedWMSTileSource(info);
104 info.setAttribution(tileSource);
105 return tileSource;
106 }
107 return null;
108 }
109
110 /**
111 * This action will add a WMS layer menu entry with the current WMS layer
112 * URL and name extended by the current resolution.
113 * When using the menu entry again, the WMS cache will be used properly.
114 */
115 public class BookmarkWmsAction extends AbstractAction {
116 /**
117 * Constructs a new {@code BookmarkWmsAction}.
118 */
119 public BookmarkWmsAction() {
120 super(tr("Set WMS Bookmark"));
121 }
122
123 @Override
124 public void actionPerformed(ActionEvent ev) {
125 ImageryLayerInfo.addLayer(new ImageryInfo(info));
126 }
127 }
128
129
130 /**
131 * Checks that WMS layer is a grabber-compatible one (HTML or WMS).
132 * @throws IllegalStateException if imagery time is neither HTML nor WMS
133 * @since 8068
134 */
135 public void checkGrabberType() {
136 }
137
138 private static TileLoaderFactory loaderFactory = new CachedTileLoaderFactory("WMS") {
139 @Override
140 protected TileLoader getLoader(TileLoaderListener listener, String cacheName, int connectTimeout,
141 int readTimeout, Map<String, String> headers, String cacheDir) throws IOException {
142 return new WMSCachedTileLoader(listener, cacheName, connectTimeout, readTimeout, headers, cacheDir);
143 }
144
145 };
146
147 @Override
148 protected TileLoaderFactory getTileLoaderFactory() {
149 return loaderFactory;
150 }
151
152 @Override
153 protected Map<String, String> getHeaders(TileSource tileSource) {
154 if (tileSource instanceof TemplatedWMSTileSource) {
155 return ((TemplatedWMSTileSource)tileSource).getHeaders();
156 }
157 return null;
158 }
159}
Note: See TracBrowser for help on using the repository browser.