@masterportal/masterportalapi 2.1.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ import WMTS, {optionsFromCapabilities} from "ol/source/WMTS";
2
+ import WMTSTileGrid from "ol/tilegrid/WMTS";
3
+ import TileLayer from "ol/layer/Tile";
4
+ import {DEVICE_PIXEL_RATIO} from "ol/has";
5
+ import {getWidth} from "ol/extent";
6
+ import WMTSCapabilities from "ol/format/WMTSCapabilities";
7
+ import {get as getProjection} from "ol/proj";
8
+
9
+ /**
10
+ * Shows error message in console for various WMTS errors.
11
+ * @param {String} errorMessage error message
12
+ * @param {String} layerName layerName
13
+ * @returns {void}
14
+ */
15
+ export function showErrorMessage (errorMessage, layerName) {
16
+ console.error("content: Layer " + layerName + ": " + errorMessage);
17
+ }
18
+
19
+ /**
20
+ * Generates resolutions and matrixIds arrays for the WMTS LayerSource.
21
+ * @param {Array} resolutions The resolutions array for the LayerSource.
22
+ * @param {Array} matrixIds The matrixIds array for the LayerSource.
23
+ * @param {Number} length The length of the given arrays.
24
+ * @param {Number} size The tileSize depending on the extent.
25
+ * @returns {void}
26
+ */
27
+ export function generateArrays (resolutions, matrixIds, length, size) {
28
+ for (let i = 0; i < length; ++i) {
29
+ resolutions[i] = size / Math.pow(2, i);
30
+ matrixIds[i] = i;
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Gets the WMTS-GetCapabilities document and parse it
36
+ * @param {String} url url for getting capabilities
37
+ * @throws {Error} on unexpected return
38
+ * @returns {promise} promise resolves to parsed WMTS-GetCapabilities object
39
+ */
40
+ export function getWMTSCapabilities (url) {
41
+ return fetch(url)
42
+ .then(response => {
43
+ if (response && response.status === 200) {
44
+ return response.text();
45
+ }
46
+ console.error(response);
47
+ throw new Error(`Failing WMTS request to ${url}. Status: ${response?.status}`);
48
+ })
49
+ .then((text) => new WMTSCapabilities().read(text));
50
+ }
51
+
52
+ /**
53
+ * Creates the LayerSource from definitions in the service.json for this WMTSLayer.
54
+ * @param {Object} attrs attributes of the layer
55
+ * @param {WMTS} tileLayer layer object
56
+ * @returns {void}
57
+ */
58
+ export function createLayerSourceByDefinitions (attrs, tileLayer) {
59
+ const projection = getProjection(attrs.coordinateSystem),
60
+ extent = projection.getExtent(),
61
+ style = attrs.style,
62
+ format = attrs.format,
63
+ wrapX = attrs.wrapX ? attrs.wrapX : false,
64
+ urls = attrs.urls,
65
+ size = extent ? getWidth(extent) / parseInt(attrs.tileSize, 10) : null,
66
+ resLength = parseInt(attrs.resLength, 10),
67
+ resolutions = new Array(resLength),
68
+ matrixIds = new Array(resLength),
69
+ source = new WMTS({
70
+ projection: projection,
71
+ attributions: attrs.olAttribution,
72
+ tileGrid: new WMTSTileGrid({
73
+ origin: attrs.origin,
74
+ resolutions: resolutions,
75
+ matrixIds: matrixIds,
76
+ tileSize: attrs.tileSize
77
+ }),
78
+ tilePixelRatio: DEVICE_PIXEL_RATIO,
79
+ urls: urls,
80
+ matrixSet: attrs.tileMatrixSet,
81
+ matrixSizes: attrs.matrixSizes,
82
+ layer: attrs.layers,
83
+ format: format,
84
+ style: style,
85
+ version: attrs.version,
86
+ transparent: attrs.transparent.toString(),
87
+ wrapX: wrapX,
88
+ requestEncoding: attrs.requestEncoding,
89
+ scales: attrs.scales
90
+ });
91
+
92
+ if (size) {
93
+ generateArrays(resolutions, matrixIds, resLength, size);
94
+ }
95
+ else {
96
+ showErrorMessage(attrs.name, `${projection.getCode()} has been given as projection to wmts.js for layer with id ${attrs.id}, but only "EPSG:4326" and "EPSG:3857" are supported. Please use the "capabilitiesUrl" and "optionsFromCapabilities" configuration parameters on this layer.`);
97
+ }
98
+
99
+ source.matrixSizes = attrs.matrixSizes;
100
+ source.scales = attrs.scales;
101
+ tileLayer.setSource(source);
102
+ tileLayer.getSource().refresh();
103
+ }
104
+
105
+ /**
106
+ * Creates the LayerSource for this WMTSLayer from the WMTS capabilities.
107
+ * @param {Object} attrs attributes of the layer
108
+ * @param {WMTS} tileLayer layer object
109
+ * @returns {void}
110
+ */
111
+ export function createLayerSourceByCapabilities (attrs, tileLayer) {
112
+ const layerIdentifier = attrs.layers,
113
+ url = attrs.capabilitiesUrl,
114
+ matrixSet = attrs.tileMatrixSet,
115
+ capabilitiesOptions = {
116
+ layer: layerIdentifier
117
+ };
118
+
119
+ // use the matrixSet (if defined) for optionsFromCapabilities
120
+ // else look for a tilematrixset in epsg:3857
121
+ if (matrixSet && matrixSet.length > 0) {
122
+ capabilitiesOptions.matrixSet = matrixSet;
123
+ }
124
+ else {
125
+ capabilitiesOptions.projection = "EPSG:3857";
126
+ }
127
+
128
+ getWMTSCapabilities(url)
129
+ .then((result) => {
130
+ const options = optionsFromCapabilities(result, capabilitiesOptions),
131
+ tileMatrixSet = result.Contents.TileMatrixSet.filter(set => set.Identifier === options.matrixSet)[0],
132
+ matrixSizes = [],
133
+ scales = [];
134
+
135
+ // Add the parameters "ScaleDenominator" and "MatrixHeight" / "MatrixWidth" to the source to be able to print WMTS layers
136
+ tileMatrixSet.TileMatrix.forEach(({MatrixHeight, MatrixWidth, ScaleDenominator}) => {
137
+ matrixSizes.push([MatrixWidth, MatrixHeight]);
138
+ scales.push(ScaleDenominator);
139
+ });
140
+
141
+ if (options !== null) {
142
+ const source = new WMTS(options);
143
+
144
+ source.matrixSizes = matrixSizes;
145
+ source.scales = scales;
146
+ tileLayer.set("options", options);
147
+ tileLayer.setSource(source);
148
+ tileLayer.getSource().refresh();
149
+ }
150
+ else {
151
+ const errorMessage = "Cannot get options from WMTS-Capabilities";
152
+
153
+ showErrorMessage(errorMessage, attrs.name || attrs.id);
154
+ throw new Error(errorMessage);
155
+ }
156
+ })
157
+ .catch((error) => {
158
+ if (error === "Fetch error") {
159
+ // error message has already been printed earlier
160
+ return;
161
+ }
162
+ showErrorMessage(error, attrs.name || attrs.id);
163
+ });
164
+ }
165
+
166
+ /**
167
+ * Creates the WMTSLayer.
168
+ * @param {Object} attrs attributes of the layer
169
+ * @returns {void}
170
+ */
171
+ export function createLayer (attrs) {
172
+ const tileLayer = new TileLayer({
173
+ id: attrs.id,
174
+ source: new WMTS({}),
175
+ name: attrs.name,
176
+ minResolution: attrs.minScale,
177
+ maxResolution: attrs.maxScale,
178
+ supported: ["2D", "3D"],
179
+ showSettings: true,
180
+ extent: null,
181
+ typ: attrs.typ,
182
+ legendURL: attrs.legendURL,
183
+ infoFormat: attrs.infoFormat
184
+ });
185
+
186
+ if (attrs.optionsFromCapabilities === undefined) {
187
+ createLayerSourceByDefinitions(attrs, tileLayer);
188
+ }
189
+ else {
190
+ createLayerSourceByCapabilities(attrs, tileLayer);
191
+ }
192
+
193
+ return tileLayer;
194
+ }
@@ -4,9 +4,11 @@ import setBackgroundImage from "../../lib/setBackgroundImage";
4
4
  import getInitialLayers from "../../lib/getInitialLayers";
5
5
  import defaults from "../../defaults";
6
6
  import * as wms from "../../layer/wms";
7
+ import * as wmts from "../../layer/wmts";
7
8
  import * as geojson from "../../layer/geojson";
8
9
  import * as wfs from "../../layer/wfs";
9
10
  import * as vectorBase from "../../layer/vectorBase";
11
+ import * as vectortile from "../../layer/vectorTile";
10
12
  import * as oaf from "../../layer/oaf";
11
13
  import {createMapView} from "../../maps/mapView";
12
14
  import {initializeLayerList, getLayerWhere} from "../../rawLayerList";
@@ -21,9 +23,11 @@ let mapIdCounter = 0;
21
23
  */
22
24
  const layerBuilderMap = {
23
25
  wms,
26
+ wmts,
24
27
  wfs,
25
28
  geojson,
26
29
  vectorBase,
30
+ vectortile,
27
31
  oaf
28
32
  },
29
33
  originalAddLayer = PluggableMap.prototype.addLayer;
@@ -4,6 +4,8 @@
4
4
  import olLayerGroup from "ol/layer/Group.js";
5
5
  import {getUid} from "olcs/util.js";
6
6
  import TileWMS from "ol/source/TileWMS.js";
7
+ import ImageWMS from "ol/source/ImageWMS.js";
8
+ import WMTS from "ol/source/WMTS.js";
7
9
  import olcsAbstractSynchronizer from "olcs/AbstractSynchronizer.js";
8
10
  import olcsCore from "olcs/core.js";
9
11
  import {Tile, Image as ImageLayer} from "ol/layer.js";
@@ -40,7 +42,6 @@ class WMSRasterSynchronizer extends olcsAbstractSynchronizer {
40
42
  * @type {!Cesium.ImageryLayerCollection}
41
43
  * @private
42
44
  */
43
- /* eslint-disable no-undef */
44
45
  this.ourLayers = new Cesium.ImageryLayerCollection();
45
46
  }
46
47
 
@@ -87,17 +88,17 @@ class WMSRasterSynchronizer extends olcsAbstractSynchronizer {
87
88
  * May be overriden by child classes to implement custom behavior.
88
89
  * The default implementation handles tiled imageries in EPSG:4326 or
89
90
  * EPSG:3859.
90
- * @param {!ol.layer.Base} olLayer -
91
- * @param {?ol.proj.Projection} viewProj Projection of the view.
92
- * @return {?Array.<!Cesium.ImageryLayer>} array or null if not possible
91
+ * @param {module:ol/layer/Base~BaseLayer } olLayer The raster layer.
92
+ * @param {module:ol/proj} viewProj Projection of the view.
93
+ * @return {?Array.<!Cesium.ImageryLayer>} Array or null if not possible.
93
94
  * (or supported)
94
95
  * @protected
95
96
  */
96
97
  convertLayerToCesiumImageries (olLayer, viewProj) {
97
- let layerOptions = {
98
- "show": false
99
- },
100
- source = {},
98
+ const layerOptions = {
99
+ "show": false
100
+ };
101
+ let source = {},
101
102
  cesiumLayer = {},
102
103
  provider = null;
103
104
 
@@ -108,61 +109,98 @@ class WMSRasterSynchronizer extends olcsAbstractSynchronizer {
108
109
  source = olLayer.getSource();
109
110
 
110
111
  if (source instanceof TileWMS) {
111
- const params = source.getParams(),
112
- options = {
113
- "url": source.getUrls()[0],
114
- "parameters": params,
115
- "layers": params.LAYERS,
116
- "show": false
117
- },
118
- tileGrid = source.getTileGrid();
119
-
120
- if (tileGrid) {
121
- const ext = olLayer.getExtent();
122
-
123
- if (ext && viewProj) {
124
- options.rectangle = olcsCore.extentToRectangle(ext, viewProj);
125
- const minMax = this.getMinMaxLevelFromTileGrid(tileGrid, ext, viewProj);
126
-
127
- options.tileWidth = tileGrid.getTileSize(0)[0];
128
- options.tileHeight = tileGrid.getTileSize(0)[1];
129
- options.minimumLevel = minMax[0];
130
- options.maximumLevel = minMax[1];
131
- }
132
- }
133
- /* eslint-disable no-undef */
134
- provider = new Cesium.WebMapServiceImageryProvider(options);
112
+ provider = this.createProviderForTileWMS(source, viewProj, olLayer);
113
+ }
114
+ else if (source instanceof ImageWMS) {
115
+ return [this.createImageryLayerForImageWMS(olLayer, viewProj)];
135
116
  }
136
117
  else if (source instanceof StaticImageSource) {
137
- const extent = source.getImageExtent(),
138
- options = {
139
- "url": source.getUrl(),
140
- "show": false
141
- },
142
- bottomLeftCorner = proj4("EPSG:25832", "EPSG:4326", getBottomLeft(extent)),
143
- topRightCorner = proj4("EPSG:25832", "EPSG:4326", getTopRight(extent));
144
-
145
- /* eslint-disable no-undef */
146
- options.rectangle = Cesium.Rectangle.fromDegrees(bottomLeftCorner[0], bottomLeftCorner[1], topRightCorner[0], topRightCorner[1]);
147
- /* eslint-disable no-undef */
148
- provider = new Cesium.SingleTileImageryProvider(options);
149
-
118
+ provider = this.createProviderForStaticImageSource(source);
119
+ }
120
+ else if (source instanceof WMTS) {
121
+ provider = new Cesium.WebMapTileServiceImageryProvider({
122
+ url: source.getUrls()[0],
123
+ format: source.getFormat(),
124
+ layer: source.getLayer(),
125
+ style: source.getStyle(),
126
+ tileMatrixSetID: source.getMatrixSet(),
127
+ tileMatrixLabels: source.getTileGrid().getMatrixIds(),
128
+ credit: source.getAttributions()
129
+ });
150
130
  }
151
131
  else {
152
132
  console.warn("Sources other than TileImage are currently not supported.");
153
133
  return null;
154
134
  }
155
135
 
156
- // the provider is always non-null if we got this far
157
- layerOptions = {
158
- "show": false
159
- };
160
- /* eslint-disable no-undef */
161
136
  cesiumLayer = new Cesium.ImageryLayer(provider, layerOptions);
162
137
 
163
138
  return cesiumLayer ? [cesiumLayer] : null;
164
139
  }
165
140
 
141
+ /**
142
+ * Creates an Cesium.WebMapServiceImageryProvider for RasterLayer of the type TileWMS.
143
+ * @param {module:ol/source/TileWMS} source The raster layer source.
144
+ * @param {module:ol/proj} viewProj Projection of the view.
145
+ * @param {module:ol/layer/Base~BaseLayer } olLayer The raster layer.
146
+ * @returns {WebMapServiceImageryProvider} The imagery provider.
147
+ */
148
+ createProviderForTileWMS (source, viewProj, olLayer) {
149
+ const params = source.getParams(),
150
+ options = {
151
+ "url": source.getUrls()[0],
152
+ "parameters": params,
153
+ "layers": params.LAYERS,
154
+ "show": false
155
+ },
156
+ tileGrid = source.getTileGrid();
157
+
158
+ if (tileGrid) {
159
+ const ext = olLayer.getExtent();
160
+
161
+ if (ext && viewProj) {
162
+ options.rectangle = olcsCore.extentToRectangle(ext, viewProj);
163
+ const minMax = this.getMinMaxLevelFromTileGrid(tileGrid, ext, viewProj);
164
+
165
+ options.tileWidth = tileGrid.getTileSize(0)[0];
166
+ options.tileHeight = tileGrid.getTileSize(0)[1];
167
+ options.minimumLevel = minMax[0];
168
+ options.maximumLevel = minMax[1];
169
+ }
170
+ }
171
+
172
+ return new Cesium.WebMapServiceImageryProvider(options);
173
+ }
174
+
175
+ /**
176
+ * Creates an Cesium.ImageryLayer for RasterLayer of the type ImageWMS.
177
+ * @param {module:ol/layer/Base~BaseLayer } olLayer The raster layer.
178
+ * @param {module:ol/proj} viewProj Projection of the view.
179
+ * @returns {Cesium.ImageryLayer} The imagery layer.
180
+ */
181
+ createImageryLayerForImageWMS (olLayer, viewProj) {
182
+ return olcsCore.tileLayerToImageryLayer(this.map, olLayer, viewProj);
183
+ }
184
+
185
+ /**
186
+ * Creates an Cesium.SingleTileImageryProvider for RasterLayer of the type StaticImageSource.
187
+ * @param {module:ol/source/ImageStatic} source The raster layer source.
188
+ * @returns {SingleTileImageryProvider} The imagery provider.
189
+ */
190
+ createProviderForStaticImageSource (source) {
191
+ const extent = source.getImageExtent(),
192
+ options = {
193
+ "url": source.getUrl(),
194
+ "show": false
195
+ },
196
+ bottomLeftCorner = proj4(source.getProjection().getCode(), "EPSG:4326", getBottomLeft(extent)),
197
+ topRightCorner = proj4(source.getProjection().getCode(), "EPSG:4326", getTopRight(extent));
198
+
199
+ options.rectangle = Cesium.Rectangle.fromDegrees(bottomLeftCorner[0], bottomLeftCorner[1], topRightCorner[0], topRightCorner[1]);
200
+
201
+ return new Cesium.SingleTileImageryProvider(options);
202
+ }
203
+
166
204
  /**
167
205
  *
168
206
  * @param {ol.Extent} extent -
@@ -179,7 +217,6 @@ class WMSRasterSynchronizer extends olcsAbstractSynchronizer {
179
217
  getTopLeft(wgs84Extent)
180
218
  ];
181
219
 
182
- /* eslint-disable no-undef */
183
220
  return olCoords.map(coord => Cesium.Cartographic.fromDegrees(coord[0], coord[1]));
184
221
  }
185
222
  /**
@@ -201,7 +238,6 @@ class WMSRasterSynchronizer extends olcsAbstractSynchronizer {
201
238
  distanceLocalX = Math.abs(tileCoordsLocal[0][1] - tileCoordsLocal[1][1]),
202
239
  distanceLocalY = Math.abs(tileCoordsLocal[0][2] - tileCoordsLocal[3][2]),
203
240
  extentCoords = this.getExtentPoints(extent, projection),
204
- /* eslint-disable no-undef */
205
241
  tilingScheme = new Cesium.GeographicTilingScheme({});
206
242
  let minLevel = 0,
207
243
  maxLevel = 20;