/
LayerHelper.js
540 lines (480 loc) · 16.5 KB
/
LayerHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/**
* @module ngeo.map.LayerHelper
*/
import googAsserts from 'goog/asserts.js';
import * as olArray from 'ol/array.js';
import olFormatWMTSCapabilities from 'ol/format/WMTSCapabilities.js';
import OlLayerGroup from 'ol/layer/Group.js';
import olLayerImage from 'ol/layer/Image.js';
import olLayerTile from 'ol/layer/Tile.js';
import * as olObj from 'ol/obj.js';
import olSourceImageWMS from 'ol/source/ImageWMS.js';
import olSourceTileWMS from 'ol/source/TileWMS.js';
import olSourceWMTS, {optionsFromCapabilities} from 'ol/source/WMTS.js';
import * as olUri from 'ol/uri.js';
/**
* Provides help functions that helps you to create and manage layers.
* @param {angular.$q} $q Angular promises/deferred service.
* @param {angular.$http} $http Angular http service.
* @param {number} ngeoTilesPreloadingLimit Load tiles up to preload levels. By default preload is Infinity,
* which means load all tiles on the top of the visible level. See also preload value
* in documentation for ol.Layer.Tile.
* @constructor
* @struct
* @ngdoc service
* @ngname ngeoLayerHelper
* @ngInject
*/
const exports = function($q, $http, ngeoTilesPreloadingLimit) {
/**
* @type {angular.$q}
* @private
*/
this.$q_ = $q;
/**
* @type {angular.$http}
* @private
*/
this.$http_ = $http;
/**
* The Tiles Preloading Limit value
* @type {number}
* @private
*/
this.tilesPreloadingLimit_ = ngeoTilesPreloadingLimit;
};
/**
* @const
*/
exports.GROUP_KEY = 'groupName';
/**
* @const
*/
exports.REFRESH_PARAM = 'random';
/**
* Create and return a basic WMS layer with only a source URL and a comma
* separated layers names (see {@link ol.source.ImageWMS}).
*
* @param {string} sourceURL The source URL.
* @param {string} sourceLayersName A comma separated names string.
* @param {string} sourceFormat Image format, for example 'image/png'.
* @param {string=} opt_serverType Type of the server ("mapserver",
* "geoserver", "qgisserver", …).
* @param {string=} opt_time time parameter for layer queryable by time/periode
* @param {Object.<string, string>=} opt_params WMS parameters.
* @param {string=} opt_crossOrigin crossOrigin.
* @param {Object=} opt_customSourceOptions Some initial options.
* @param {Object=} opt_customLayerOptions The layer opacity.
* @return {ol.layer.Image} WMS Layer.
* @export
*/
exports.prototype.createBasicWMSLayer = function(sourceURL,
sourceLayersName, sourceFormat, opt_serverType, opt_time, opt_params, opt_crossOrigin,
opt_customSourceOptions, opt_customLayerOptions) {
const params = {
'FORMAT': sourceFormat,
'LAYERS': sourceLayersName
};
let olServerType;
if (opt_time) {
params['TIME'] = opt_time;
}
if (opt_serverType) {
params['SERVERTYPE'] = opt_serverType;
// OpenLayers expects 'qgis' insteads of 'qgisserver'
olServerType = opt_serverType.replace('qgisserver', 'qgis');
}
const options = Object.assign({}, opt_customSourceOptions, {
url: sourceURL,
params: params,
serverType: olServerType,
crossOrigin: opt_crossOrigin
});
const source = new olSourceImageWMS(options);
if (opt_params) {
source.updateParams(opt_params);
}
const layerOptions = Object.assign({}, opt_customLayerOptions, {source});
return new olLayerImage(layerOptions);
};
/**
* Create and return a basic WMS layer using an OGC data source.
*
* @param {ngeo.datasource.OGC} dataSource OGC data source.
* @param {string=} opt_crossOrigin crossOrigin.
* @return {ol.layer.Image} WMS Layer.
* @export
*/
exports.prototype.createBasicWMSLayerFromDataSource = function(
dataSource, opt_crossOrigin
) {
const url = dataSource.wmsUrl;
googAsserts.assert(url);
const layerNames = dataSource.getOGCLayerNames().join(',');
const serverType = dataSource.ogcServerType;
const imageType = dataSource.ogcImageType;
// (1) Layer creation
const layer = this.createBasicWMSLayer(
url,
layerNames,
imageType,
serverType,
undefined,
undefined,
opt_crossOrigin
);
// (2) Manage visibility
layer.setVisible(dataSource.visible);
// (3) Reference to the data source
layer.set('querySourceIds', [dataSource.id]);
return layer;
};
/**
* Create and return a promise that provides a WMTS layer with source on
* success, no layer else.
* The WMTS layer source will be configured by the capabilities that are
* loaded from the given capabilitiesUrl.
* The style object described in the capabilities for this layer will be added
* as key 'capabilitiesStyles' as param of the new layer.
* @param {string} capabilitiesURL The getCapabilities url.
* @param {string} layerName The name of the layer.
* @param {string=} opt_matrixSet Optional WMTS matrix set.
* @param {Object.<string, string>=} opt_dimensions WMTS dimensions.
* @param {Object=} opt_customOptions Some initial options.
* @return {angular.$q.Promise.<ol.layer.Tile>} A Promise with a layer (with source) on success,
* no layer else.
* @export
*/
exports.prototype.createWMTSLayerFromCapabilitites = function(capabilitiesURL, layerName, opt_matrixSet, opt_dimensions, opt_customOptions) {
const parser = new olFormatWMTSCapabilities();
const layer = new olLayerTile({
preload: this.tilesPreloadingLimit_
});
const $q = this.$q_;
return this.$http_.get(capabilitiesURL, {cache: true}).then((response) => {
let result;
if (response.data) {
result = parser.read(response.data);
}
if (result) {
const options = Object.assign({}, opt_customOptions, optionsFromCapabilities(result, {
matrixSet: opt_matrixSet,
crossOrigin: 'anonymous',
layer: layerName
}));
const source = new olSourceWMTS(/** @type {olx.source.WMTSOptions} */ (options));
if (opt_dimensions && !olObj.isEmpty(opt_dimensions)) {
source.updateDimensions(opt_dimensions);
}
layer.setSource(source);
// Add styles from capabilities as param of the layer
const layers = result['Contents']['Layer'];
const l = olArray.find(layers, (elt, index, array) => elt['Identifier'] == layerName);
layer.set('capabilitiesStyles', l['Style']);
return $q.resolve(layer);
}
return $q.reject(`Failed to get WMTS capabilities from ${capabilitiesURL}`);
});
};
/**
* Create and return a WMTS layer using a formatted capabilities response
* and a capability layer.
*
* @param {!Object} capabilities The complete capabilities object of the service
* @param {!Object} layerCap The layer capability object
* @param {Object.<string, string>=} opt_dimensions WMTS dimensions.
* @return {!ol.layer.Tile} WMTS layer
* @export
*/
exports.prototype.createWMTSLayerFromCapabilititesObj = function(
capabilities, layerCap, opt_dimensions
) {
const options = optionsFromCapabilities(capabilities, {
crossOrigin: 'anonymous',
layer: layerCap['Identifier']
});
googAsserts.assert(options);
const source = new olSourceWMTS(
/** @type {olx.source.WMTSOptions} */ (options));
if (opt_dimensions && !olObj.isEmpty(opt_dimensions)) {
source.updateDimensions(opt_dimensions);
}
return new olLayerTile({
'capabilitiesStyles': layerCap['Style'],
preload: Infinity,
source: source
});
};
/**
* Create and return an ol.layer.Group. You can pass a collection of layers to
* directly add them in the returned group.
* @param {ol.Collection.<ol.layer.Base>=} opt_layers The layer to add to the
* returned Group.
* @return {ol.layer.Group} Layer group.
* @export
*/
exports.prototype.createBasicGroup = function(opt_layers) {
const group = new OlLayerGroup();
if (opt_layers) {
group.setLayers(opt_layers);
}
return group;
};
/**
* Retrieve (or create if it doesn't exist) and return a group of layer from
* the base array of layers of a map. The given name is used as unique
* identifier. If the group is created, it will be automatically added to
* the map.
* @param {ol.Map} map A map.
* @param {string} groupName The name of the group.
* @return {ol.layer.Group} The group corresponding to the given name.
* @export
*/
exports.prototype.getGroupFromMap = function(map, groupName) {
const groups = map.getLayerGroup().getLayers();
let group;
groups.getArray().some((existingGroup) => {
if (existingGroup.get(exports.GROUP_KEY) === groupName) {
group = /** @type {ol.layer.Group} */ (existingGroup);
return true;
} else {
return false;
}
});
if (!group) {
group = this.createBasicGroup();
group.set(exports.GROUP_KEY, groupName);
map.addLayer(group);
}
return group;
};
/**
* Get an array of all layers in a group. The group can contain multiple levels
* of others groups.
* @param {ol.layer.Base} layer The base layer, mostly a group of layers.
* @return {Array.<ol.layer.Layer>} Layers.
* @export
*/
exports.prototype.getFlatLayers = function(layer) {
if (layer instanceof OlLayerGroup) {
const sublayers = layer.getLayers().getArray();
const hasGroupLayer = sublayers.some(sublayer => sublayer instanceof OlLayerGroup);
if (!hasGroupLayer) {
return sublayers.slice();
}
}
return this.getFlatLayers_(layer, [], undefined);
};
/**
* Get an array of all layers in a group. The group can contain multiple levels
* of others groups. When we flatten a group, we get the child layers.
* If opacity is defined on the group, this value is lost.
* Computed opacity is a custom 'back-up' value that contains
* the calculated value of all ancestors and the given layer.
* @param {ol.layer.Base} layer The base layer, mostly a group of layers.
* @param {Array.<ol.layer.Base>} array An array to add layers.
* @param {number|undefined} computedOpacity Opacity inherited from ancestor layer groups.
* @return {Array.<ol.layer.Layer>} Layers.
* @private
*/
exports.prototype.getFlatLayers_ = function(layer, array, computedOpacity) {
const opacity = layer.getOpacity();
if (computedOpacity !== undefined) {
computedOpacity *= opacity;
} else {
computedOpacity = opacity;
}
if (layer instanceof OlLayerGroup) {
const sublayers = layer.getLayers();
sublayers.forEach((l) => {
this.getFlatLayers_(l, array, computedOpacity);
});
} else {
if (array.indexOf(layer) < 0) {
layer.set('inheritedOpacity', computedOpacity, true);
array.push(layer);
}
}
return array;
};
/**
* Get a layer that has a `layerName` property equal to a given layer name from
* an array of layers. If one of the layers in the array is a group, then the
* layers contained in that group are searched as well.
* @param {string} layerName The name of the layer we're looking for.
* @param {Array.<ol.layer.Base>} layers Layers.
* @return {?ol.layer.Base} Layer.
* @export
*/
exports.prototype.getLayerByName = function(layerName, layers) {
let found = null;
layers.some((layer) => {
if (layer instanceof OlLayerGroup) {
const sublayers = layer.getLayers().getArray();
found = this.getLayerByName(layerName, sublayers);
} else if (layer.get('layerNodeName') === layerName) {
found = layer;
}
return !!found;
});
return found;
};
/**
* Get the WMTS legend URL for the given layer.
* @param {ol.layer.Tile} layer Tile layer as returned by the
* ngeo layerHelper service.
* @return {string|undefined} The legend URL or undefined.
* @export
*/
exports.prototype.getWMTSLegendURL = function(layer) {
// FIXME case of multiple styles ? case of multiple legendUrl ?
let url;
const styles = layer.get('capabilitiesStyles');
if (styles !== undefined) {
const legendURL = styles[0]['legendURL'];
if (legendURL !== undefined) {
url = legendURL[0]['href'];
}
}
return url;
};
/**
* Get the WMS legend URL for the given node.
* @param {string|undefined} url The base url of the wms service.
* @param {string} layerName The name of a wms layer.
* @param {number=} opt_scale A scale.
* @param {string=} opt_legendRule rule parameters to add to the returned URL.
* @param {number=} opt_legendWidth the legend width.
* @param {number=} opt_legendHeight the legend height.
* @param {string=} opt_servertype the OpenLayers server type.
* @param {number=} opt_dpi the DPI.
* @param {Array.number=} opt_bbox the bbox.
* @param {string=} opt_srs The projection code.
* @param {Object.<string, string>=} opt_additionalQueryString Additional query string parameters.
* @return {string|undefined} The legend URL or undefined.
* @export
*/
exports.prototype.getWMSLegendURL = function(url,
layerName, opt_scale, opt_legendRule, opt_legendWidth, opt_legendHeight,
opt_servertype, opt_dpi, opt_bbox, opt_srs, opt_additionalQueryString) {
if (!url) {
return undefined;
}
const queryString = {
'FORMAT': 'image/png',
'TRANSPARENT': true,
'SERVICE': 'WMS',
'VERSION': '1.1.1',
'REQUEST': 'GetLegendGraphic',
'LAYER': layerName
};
if (opt_scale !== undefined) {
queryString['SCALE'] = opt_scale;
}
if (opt_legendRule !== undefined) {
queryString['RULE'] = opt_legendRule;
if (opt_legendWidth !== undefined) {
queryString['WIDTH'] = opt_legendWidth;
}
if (opt_legendHeight !== undefined) {
queryString['HEIGHT'] = opt_legendHeight;
}
}
if (opt_servertype == 'qgis') {
if (opt_dpi != undefined) {
queryString['DPI'] = opt_dpi;
}
if (opt_bbox != undefined && opt_srs != undefined && opt_scale != undefined && opt_dpi != undefined && opt_legendRule == undefined) {
queryString['BBOX'] = opt_bbox.join(',');
queryString['SRS'] = opt_srs;
queryString['WIDTH'] = Math.round((opt_bbox[2] - opt_bbox[0]) / opt_scale * 39.37 * opt_dpi);
queryString['HEIGHT'] = Math.round((opt_bbox[3] - opt_bbox[1]) / opt_scale * 39.37 * opt_dpi);
}
}
if (opt_additionalQueryString) {
Object.assign(queryString, opt_additionalQueryString);
}
return olUri.appendParams(url, queryString);
};
/**
* Returns if this layer is visible at the current resolution.
* @param {ol.layer.Base} layer Layer.
* @param {ol.Map} map Map.
* @return {boolean} Is the layer currently visible?
*/
exports.prototype.isLayerVisible = function(layer, map) {
if (!layer.getVisible()) {
return false;
}
const currentResolution = map.getView().getResolution();
return currentResolution > layer.getMinResolution() &&
currentResolution < layer.getMaxResolution();
};
/**
* Force a WMS layer to refresh using a random value.
* @param {ol.layer.Image|ol.layer.Tile} layer Layer to refresh.
*/
exports.prototype.refreshWMSLayer = function(layer) {
const source_ = layer.getSource();
googAsserts.assert(
source_ instanceof olSourceImageWMS ||
source_ instanceof olSourceTileWMS
);
const source = /** @type {ol.source.ImageWMS|ol.source.TileWMS} */ (source_);
const params = source.getParams();
params[exports.REFRESH_PARAM] = Math.random();
source.updateParams(params);
};
/**
* Set ZIndex property to first level children elements
* @param {ol.layer.Group|ol.layer.Base} element The group of layer with first level children layers.
* @param {number} ZIndex The ZIndex for children element.
*/
exports.prototype.setZIndexToFirstLevelChildren = function(element, ZIndex) {
if (!(element instanceof OlLayerGroup)) {
return;
}
const innerGroupLayers = element.getLayers();
innerGroupLayers.forEach(innerLayer => innerLayer.setZIndex(ZIndex));
};
/**
* Update the LAYERS parameter of the source of the given WMS layer.
* @param {ol.layer.Image} layer The WMS layer.
* @param {string} names The names that will be used to set
* the LAYERS parameter.
* @param {string=} opt_time The start
* and optionally the end datetime (for time range selection) selected by user
* in a ISO-8601 string datetime or time interval format
* @export
*/
exports.prototype.updateWMSLayerState = function(layer, names, opt_time) {
// Don't send layer without parameters, hide layer instead;
if (names.length <= 0) {
layer.setVisible(false);
} else {
layer.setVisible(true);
const source = /** @type {ol.source.ImageWMS} */ (layer.getSource());
if (opt_time) {
source.updateParams({'LAYERS': names, 'TIME': opt_time});
} else {
source.updateParams({'LAYERS': names});
}
}
};
/**
* @param {ol.layer.Image} layer The WMS layer.
* @return {Array.<number>|undefined} List of query source ids, a.k.a.
* the data source ids this layer is composed of.
* @export
*/
exports.prototype.getQuerySourceIds = function(layer) {
return /** @type {Array.<number>|undefined} */ (
layer.get('querySourceIds'));
};
/**
* @type {!angular.Module}
*/
exports.module = angular.module('ngeoLayerHelper', [])
.service('ngeoLayerHelper', exports)
.value('ngeoTilesPreloadingLimit', Infinity);
export default exports;