-
Notifications
You must be signed in to change notification settings - Fork 88
/
WMSGroup.js
232 lines (202 loc) · 6.51 KB
/
WMSGroup.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
// The MIT License (MIT)
//
// Copyright (c) 2017-2022 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import ngeoDatasourceOGCGroup from 'ngeo/datasource/OGCGroup';
import gmfDatasourceOGC from 'gmf/datasource/OGC';
import {remove as removeFromArray} from 'ol/array';
/**
* The options required to create a `WMSGroup`.
*
* extends OGCGroupOptions
*
* @typedef {Object} WMSGroupOptions
* @property {angular.auto.IInjectorService} injector Angular main injector.
* @property {string} url (WMSGroupOptions)
* @property {import('ngeo/datasource/DataSource').default[]} dataSources (GroupOptions)
* @property {string} title (GroupOptions)
*/
/**
* @hidden
*/
export default class extends ngeoDatasourceOGCGroup {
/**
* A WMSGroup data source combines multiple `ngeo.datasource.OGC` objects
* that have the 'WMS' type. Its main goal is to create a single
* `ol.layer.Image` object in which the data source visible properties
* determine the WMS LAYERS parameter.
*
* Note: the layer is not added to the map here.
*
* @param {WMSGroupOptions} options Options.
* @param {import('ngeo/map/LayerHelper').LayerHelper} ngeoLayerHelper the ngeo map LayerHelper service.
*/
constructor(options, ngeoLayerHelper) {
super(options);
const injector = options.injector;
// === PRIVATE properties ===
/**
* @type {?import('ol/layer/Image').default<import('ol/source/Image').default>}
* @private
*/
this.layer_ = null;
/**
* @type {import('ngeo/map/LayerHelper').LayerHelper}
* @private
*/
this.ngeoLayerHelper_ = ngeoLayerHelper;
/**
* @type {angular.IScope}
* @private
*/
this.rootScope_ = injector.get('$rootScope');
/**
* The functions to call to unregister the `watch` event on data sources
* that are registered. Key is the id of the data source.
*
* @type {Object<number, Function>}
* @private
*/
this.wmsDataSourceUnregister_ = {};
this.init_();
}
/**
* @private
*/
init_() {
if (!this.dataSources.length) {
throw new Error('Missing dataSources');
}
for (const dataSource of this.dataSources) {
if (dataSource instanceof gmfDatasourceOGC) {
this.registerDataSource_(dataSource);
}
}
}
/**
* @inheritDoc
*/
destroy() {
for (const dataSource of this.dataSources) {
if (dataSource instanceof gmfDatasourceOGC) {
this.unregisterDataSource_(dataSource);
}
}
super.destroy();
}
// =======================================
// === Static property getters/setters ===
// =======================================
/**
* @returns {import('ol/layer/Image').default<import('ol/source/Image').default>} layer
*/
get layer() {
if (!this.layer_) {
throw new Error('Missing layer');
}
return this.layer_;
}
// =======================
// === Utility Methods ===
// =======================
/**
* @param {import('ngeo/datasource/DataSource').default} dataSource Data source to add.
*/
addDataSource(dataSource) {
super.addDataSource(dataSource);
if (!(dataSource instanceof gmfDatasourceOGC)) {
throw new Error('Wrong datasource type');
}
this.registerDataSource_(dataSource);
}
/**
* @param {gmfDatasourceOGC} dataSource OGC data source to register.
* @private
*/
registerDataSource_(dataSource) {
const id = dataSource.id;
this.wmsDataSourceUnregister_[id] = this.rootScope_.$watch(
() => dataSource.visible,
this.handleDataSourceVisibleChange_.bind(this)
);
if (!this.layer_) {
this.layer_ = this.ngeoLayerHelper_.createBasicWMSLayerFromDataSource(dataSource);
} else {
this.layer_.get('querySourceIds').push(id);
this.updateLayer_();
}
}
/**
* @param {boolean|undefined} value Current visible property of the DS
* @param {boolean|undefined} oldValue Old visible property of the DS
* @private
*/
handleDataSourceVisibleChange_(value, oldValue) {
if (value !== undefined && value !== oldValue) {
this.updateLayer_();
}
}
/**
* @private
*/
updateLayer_() {
const layer = this.layer;
/** @type {string[]} */
let layerNames = [];
// (1) Collect layer names from data sources in the group
for (const dataSource of this.dataSources) {
if (dataSource instanceof gmfDatasourceOGC && dataSource.visible) {
layerNames = layerNames.concat(dataSource.getWMSLayerNames());
}
}
// (2) Update layer object
this.ngeoLayerHelper_.updateWMSLayerState(layer, layerNames.join(','));
}
/**
* @param {import('ngeo/datasource/DataSource').default} dataSource Data source to remove.
*/
removeDataSource(dataSource) {
super.removeDataSource(dataSource);
if (dataSource instanceof gmfDatasourceOGC) {
this.unregisterDataSource_(dataSource);
}
}
/**
* @param {import('ngeo/datasource/OGC').default} dataSource OGC data source to unregister.
* @private
*/
unregisterDataSource_(dataSource) {
const id = dataSource.id;
const layer = this.layer;
// Unregister watcher
const unregister = this.wmsDataSourceUnregister_[id];
unregister();
delete this.wmsDataSourceUnregister_[id];
// Remove DS from the group
removeFromArray(this.dataSources, dataSource);
// Remove query source id
const ids = this.ngeoLayerHelper_.getQuerySourceIds(layer);
if (ids) {
removeFromArray(ids, id);
}
if (this.dataSources.length) {
this.updateLayer_();
}
}
}