Skip to content

Commit b12dc50

Browse files
sissbrueckerhdamr
andauthored
feat: add support for wms image sources (#2607)
* feat: add support for image wms sources * add basic IT * run formatter Co-authored-by: Hadi Amiri <84700249+hdamr@users.noreply.github.com>
1 parent 37106b5 commit b12dc50

11 files changed

Lines changed: 412 additions & 2 deletions

File tree

‎vaadin-map-flow-parent/vaadin-map-flow-integration-tests/src/main/java/com/vaadin/flow/component/map/SourcesPage.java‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.vaadin.flow.component.html.Div;
44
import com.vaadin.flow.component.html.NativeButton;
5+
import com.vaadin.flow.component.map.configuration.layer.ImageLayer;
56
import com.vaadin.flow.component.map.configuration.layer.TileLayer;
7+
import com.vaadin.flow.component.map.configuration.source.ImageWMSSource;
68
import com.vaadin.flow.component.map.configuration.source.TileWMSSource;
79
import com.vaadin.flow.component.map.configuration.source.XYZSource;
810
import com.vaadin.flow.router.Route;
@@ -47,6 +49,24 @@ public SourcesPage() {
4749
});
4850
setupXYZSource.setId("setup-xyz-source");
4951

50-
add(new Div(setupTileWMSSource, setupXYZSource));
52+
NativeButton setupImageWMSSource = new NativeButton(
53+
"Setup ImageWMS source", e -> {
54+
String url = "https://example.com/wms";
55+
String serverType = "geoserver";
56+
HashMap<String, Object> params = new HashMap<>();
57+
params.put("LAYERS", "layer1");
58+
ImageWMSSource source = new ImageWMSSource(
59+
new ImageWMSSource.Options().setUrl(url)
60+
.setServerType(serverType).setParams(params)
61+
.setCrossOrigin("custom-cross-origin")
62+
.setRatio(2));
63+
ImageLayer layer = new ImageLayer();
64+
layer.setId("background-layer");
65+
layer.setSource(source);
66+
map.setBackgroundLayer(layer);
67+
});
68+
setupImageWMSSource.setId("setup-image-wms-source");
69+
70+
add(new Div(setupTileWMSSource, setupXYZSource, setupImageWMSSource));
5171
}
5272
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.vaadin.flow.component.map.demo;
2+
3+
import com.vaadin.flow.component.html.Div;
4+
import com.vaadin.flow.component.map.Map;
5+
import com.vaadin.flow.component.map.configuration.Coordinate;
6+
import com.vaadin.flow.component.map.configuration.layer.ImageLayer;
7+
import com.vaadin.flow.component.map.configuration.layer.TileLayer;
8+
import com.vaadin.flow.component.map.configuration.source.ImageWMSSource;
9+
import com.vaadin.flow.component.map.configuration.source.TileWMSSource;
10+
import com.vaadin.flow.router.Route;
11+
12+
import java.util.HashMap;
13+
14+
/**
15+
* Demo that displays an image WMS layer on top of the default OSM background
16+
* layer
17+
* <p>
18+
* Adapted from https://openlayers.org/en/latest/examples/wms-tiled.html
19+
*/
20+
@Route("vaadin-map/demo/image-wms")
21+
public class ImageWMSDemo extends Div {
22+
public ImageWMSDemo() {
23+
Map map = new Map();
24+
map.setWidthFull();
25+
map.setHeight("400px");
26+
add(map);
27+
28+
// Add image layer showing an overlay of the individual states of the US
29+
HashMap<String, Object> params = new HashMap<>();
30+
params.put("LAYERS", "topp:states");
31+
ImageWMSSource imageWMSSource = new ImageWMSSource(
32+
new ImageWMSSource.Options()
33+
.setUrl("https://ahocevar.com/geoserver/wms")
34+
.setParams(params).setServerType("geoserver")
35+
.setRatio(1));
36+
37+
ImageLayer imageLayer = new ImageLayer();
38+
imageLayer.setSource(imageWMSSource);
39+
40+
map.addLayer(imageLayer);
41+
42+
// Move viewport to US
43+
map.getView().setCenter(new Coordinate(-10997148, 4569099));
44+
map.getView().setZoom(4);
45+
}
46+
}

‎vaadin-map-flow-parent/vaadin-map-flow-integration-tests/src/test/java/com/vaadin/flow/components/map/SourcesIT.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ public class SourcesIT extends AbstractComponentIT {
1717
private MapElement map;
1818
private TestBenchElement setupTileWMSSource;
1919
private TestBenchElement setupXYZSource;
20+
private TestBenchElement setupImageWMSSource;
2021

2122
@Before
2223
public void init() {
2324
open();
2425
map = $(MapElement.class).waitForFirst();
2526
setupTileWMSSource = $("button").id("setup-tile-wms-source");
2627
setupXYZSource = $("button").id("setup-xyz-source");
28+
setupImageWMSSource = $("button").id("setup-image-wms-source");
2729
}
2830

2931
@Test
@@ -66,4 +68,31 @@ public void initializeXYZSource() {
6668
Assert.assertEquals("ol/source/XYZ", sourceType);
6769
Assert.assertEquals("https://example.com/wms", url);
6870
}
71+
72+
@Test
73+
public void initializeImageWMSSource() {
74+
setupImageWMSSource.click();
75+
76+
String backgroundLayerEx = map.getLayerExpression("background-layer");
77+
String sourceEx = backgroundLayerEx + ".getSource()";
78+
79+
String sourceType = (String) map
80+
.evaluateOLExpression(map.getOLTypeNameExpression(sourceEx));
81+
String url = (String) map.evaluateOLExpression(sourceEx + ".url_");
82+
String layersParam = (String) map
83+
.evaluateOLExpression(sourceEx + ".params_.LAYERS");
84+
String serverType = (String) map
85+
.evaluateOLExpression(sourceEx + ".serverType_");
86+
String crossOrigin = (String) map
87+
.evaluateOLExpression(sourceEx + ".crossOrigin_");
88+
float ratio = ((Number) map.evaluateOLExpression(sourceEx + ".ratio_"))
89+
.floatValue();
90+
91+
Assert.assertEquals("ol/source/ImageWMS", sourceType);
92+
Assert.assertEquals("https://example.com/wms", url);
93+
Assert.assertEquals("layer1", layersParam);
94+
Assert.assertEquals("geoserver", serverType);
95+
Assert.assertEquals("custom-cross-origin", crossOrigin);
96+
Assert.assertEquals(2, ratio, 0.1);
97+
}
6998
}

‎vaadin-map-flow-parent/vaadin-map-flow/src/main/java/com/vaadin/flow/component/map/configuration/Constants.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
public class Constants {
2020
// Layers
21+
public static final String OL_LAYER_IMAGE = "ol/layer/Image";
2122
public static final String OL_LAYER_TILE = "ol/layer/Tile";
2223
public static final String OL_LAYER_VECTOR = "ol/layer/Vector";
2324
// Sources
2425
public static final String OL_SOURCE_XYZ = "ol/source/XYZ";
2526
public static final String OL_SOURCE_OSM = "ol/source/OSM";
2627
public static final String OL_SOURCE_VECTOR = "ol/source/Vector";
2728
public static final String OL_SOURCE_TILE_WMS = "ol/source/TileWMS";
29+
public static final String OL_SOURCE_IMAGE_WMS = "ol/source/ImageWMS";
2830
// Geometry
2931
public static final String OL_GEOMETRY_POINT = "ol/geom/Point";
3032
// Style
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.vaadin.flow.component.map.configuration.layer;
2+
3+
import com.vaadin.flow.component.map.configuration.Constants;
4+
import com.vaadin.flow.component.map.configuration.source.ImageSource;
5+
6+
/**
7+
* Layer for server-rendered images that are available in arbitrary extents and
8+
* resolutions.
9+
*/
10+
public class ImageLayer extends Layer {
11+
12+
private ImageSource source;
13+
14+
@Override
15+
public String getType() {
16+
return Constants.OL_LAYER_IMAGE;
17+
}
18+
19+
/**
20+
* @return source for this layer, null by default
21+
*/
22+
public ImageSource getSource() {
23+
return source;
24+
}
25+
26+
/**
27+
* Sets the source for this layer. The source must be a subclass of
28+
* {@link ImageSource}, which means that it provides single, untiled images.
29+
*
30+
* @param source
31+
* the new source for the layer, not null
32+
*/
33+
public void setSource(ImageSource source) {
34+
this.source = source;
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.vaadin.flow.component.map.configuration.source;
2+
3+
/**
4+
* Abstract base class for all sources providing a single image
5+
*/
6+
public abstract class ImageSource extends Source {
7+
public ImageSource(BaseOptions<?> options) {
8+
super(options);
9+
}
10+
11+
public static abstract class BaseOptions<T extends BaseOptions<T>>
12+
extends Source.BaseOptions<T> {
13+
14+
}
15+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package com.vaadin.flow.component.map.configuration.source;
2+
3+
import com.vaadin.flow.component.map.configuration.Constants;
4+
5+
import java.util.Map;
6+
import java.util.Objects;
7+
8+
/**
9+
* Source for WMS servers providing single, untiled images
10+
*/
11+
public class ImageWMSSource extends ImageSource {
12+
13+
private String url;
14+
private final Map<String, Object> params;
15+
private final String serverType;
16+
private final String crossOrigin;
17+
private final float ratio;
18+
19+
public ImageWMSSource(Options options) {
20+
super(options);
21+
Objects.requireNonNull(options.params,
22+
"WMS request parameters must not be null");
23+
Objects.requireNonNull(options.params.get("LAYERS"),
24+
"WMS request parameter LAYERS must not be null");
25+
this.url = options.url;
26+
this.params = options.params;
27+
this.serverType = options.serverType;
28+
this.crossOrigin = options.crossOrigin;
29+
this.ratio = options.ratio;
30+
}
31+
32+
@Override
33+
public String getType() {
34+
return Constants.OL_SOURCE_IMAGE_WMS;
35+
}
36+
37+
/**
38+
* The WMS service URL
39+
*
40+
* @return the current URL
41+
*/
42+
public String getUrl() {
43+
return url;
44+
}
45+
46+
/**
47+
* Sets the WMS service URL
48+
*
49+
* @param url
50+
* the new URL
51+
*/
52+
public void setUrl(String url) {
53+
this.url = url;
54+
}
55+
56+
/**
57+
* The WMS request parameters for requesting images from the WMS server. At
58+
* least the {@code LAYERS} parameter is required. By default,
59+
* {@code VERSION} is {@code 1.3.0}, and {@code STYLES} is {@code ""}.
60+
* {@code WIDTH}, {@code HEIGHT}, {@code BBOX}, and {@code CRS} /
61+
* {@code SRS} will be set dynamically.
62+
* <p>
63+
* For individual parameters please refer to the documentation of the WMS
64+
* server as well as the <a href="https://www.ogc.org/standards/wms">WMS
65+
* specification</a>.
66+
* <p>
67+
* This value can not be changed after constructing an instance, it can only
68+
* be set initially by passing an options object to the constructor.
69+
*
70+
* @return the WMS parameters
71+
*/
72+
public Map<String, Object> getParams() {
73+
return params;
74+
}
75+
76+
/**
77+
* The type of WMS server.
78+
* <p>
79+
* This value can not be changed after constructing an instance, it can only
80+
* be set initially by passing an options object to the constructor.
81+
*
82+
* @return the type of WMS server
83+
*/
84+
public String getServerType() {
85+
return serverType;
86+
}
87+
88+
/**
89+
* The {@code crossOrigin} attribute for loaded images.
90+
* <p>
91+
* This value can not be changed after constructing an instance, it can only
92+
* be set initially by passing an options object to the constructor.
93+
*
94+
* @return the crossOrigin attribute used for loaded images
95+
*/
96+
public String getCrossOrigin() {
97+
return crossOrigin;
98+
}
99+
100+
/**
101+
* The ratio for the size of requested images compared to the map's
102+
* viewport. Ratio {@code 1} means image requests are the size of the
103+
* viewport, a ratio of {@code 2} means twice the size of the viewport, and
104+
* so on. Default is {code 1.5}.
105+
* <p>
106+
* This value can not be changed after constructing an instance, it can only
107+
* be set initially by passing an options object to the constructor.
108+
*
109+
* @return the ratio
110+
*/
111+
public float getRatio() {
112+
return ratio;
113+
}
114+
115+
public static class Options extends ImageSource.BaseOptions<Options> {
116+
private String url;
117+
private Map<String, Object> params;
118+
private String serverType;
119+
private String crossOrigin;
120+
private float ratio = 1.5f;
121+
122+
/**
123+
* @see ImageWMSSource#getUrl()
124+
*/
125+
public Options setUrl(String url) {
126+
this.url = url;
127+
return getThis();
128+
}
129+
130+
/**
131+
* @see ImageWMSSource#getParams()
132+
*/
133+
public Options setParams(Map<String, Object> params) {
134+
this.params = params;
135+
return getThis();
136+
}
137+
138+
/**
139+
* @see ImageWMSSource#getServerType()
140+
*/
141+
public Options setServerType(String serverType) {
142+
this.serverType = serverType;
143+
return getThis();
144+
}
145+
146+
/**
147+
* @see ImageWMSSource#getCrossOrigin()
148+
*/
149+
public Options setCrossOrigin(String crossOrigin) {
150+
this.crossOrigin = crossOrigin;
151+
return getThis();
152+
}
153+
154+
/**
155+
* @see ImageWMSSource#getRatio()
156+
*/
157+
public Options setRatio(float ratio) {
158+
this.ratio = ratio;
159+
return getThis();
160+
}
161+
}
162+
}

‎vaadin-map-flow-parent/vaadin-map-flow/src/main/resources/META-INF/resources/frontend/vaadin-map/synchronization/index.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
import Feature from "ol/Feature";
77
import Point from "ol/geom/Point";
88
import View from "ol/View";
9-
import { synchronizeTileLayer, synchronizeVectorLayer } from "./layers.js";
109
import {
10+
synchronizeImageLayer,
11+
synchronizeTileLayer,
12+
synchronizeVectorLayer,
13+
} from "./layers.js";
14+
import {
15+
synchronizeImageWMSSource,
1116
synchronizeOSMSource,
1217
synchronizeTileWMSSource,
1318
synchronizeVectorSource,
@@ -83,9 +88,11 @@ const synchronizerLookup = {
8388
"ol/Map": synchronizeMap,
8489
"ol/View": synchronizeView,
8590
// Layers
91+
"ol/layer/Image": synchronizeImageLayer,
8692
"ol/layer/Tile": synchronizeTileLayer,
8793
"ol/layer/Vector": synchronizeVectorLayer,
8894
// Sources
95+
"ol/source/ImageWMS": synchronizeImageWMSSource,
8996
"ol/source/OSM": synchronizeOSMSource,
9097
"ol/source/TileWMS": synchronizeTileWMSSource,
9198
"ol/source/Vector": synchronizeVectorSource,

0 commit comments

Comments
 (0)