/
layerSync-watcher.ts
276 lines (262 loc) · 13.8 KB
/
layerSync-watcher.ts
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
import { Watcher } from '@igo2/utils';
import { Layer } from '../shared/layers/layer';
import olLayer from 'ol/layer/Layer';
import olSource from 'ol/source/Source';
import { DataSource } from '../../datasource/shared/datasources/datasource';
import { IgoMap } from '../../map/shared/map';
import { LayersLink, LinkedProperties } from '../shared/layers/layer.interface';
import { OgcFilterableDataSource, OgcFilterableDataSourceOptions } from '../../filter/shared/ogc-filter.interface';
import { WMSDataSource } from '../../datasource/shared/datasources/wms-datasource';
import { OgcFilterWriter } from '../../filter/shared/ogc-filter';
import { TimeFilterableDataSource } from '../../filter/shared/time-filter.interface';
import { Subscription } from 'rxjs';
import { first } from 'rxjs/operators';
export class LayerSyncWatcher extends Watcher {
private ogcFilters$$: Subscription;
private timeFilter$$: Subscription;
private ol: olLayer<olSource>;
private layer: Layer;
private dataSource: DataSource;
private map: IgoMap;
private ogcFilterWriter;
constructor(layer: Layer, map: IgoMap) {
super();
this.ol = layer.ol;
this.layer = layer;
this.dataSource = layer.options.source;
this.map = map;
this.ogcFilterWriter = new OgcFilterWriter();
}
protected watch() {
this.ol.on('propertychange', evt => this.transferCommonProperties(evt));
if ((this.dataSource as OgcFilterableDataSource).ogcFilters$) {
this.ogcFilters$$ = (this.dataSource as OgcFilterableDataSource).ogcFilters$
.subscribe(ogcFilters => this.transferOgcFiltersProperties(ogcFilters));
}
if ((this.dataSource as TimeFilterableDataSource).timeFilter$) {
this.timeFilter$$ = (this.dataSource as TimeFilterableDataSource).timeFilter$
.subscribe(timeFilter => this.transferTimeFilterProperties(timeFilter));
}
this.syncChildLayers();
}
protected unwatch() {
this.ol.un('propertychange', evt => this.transferCommonProperties(evt));
if (this.ogcFilters$$) { this.ogcFilters$$.unsubscribe(); }
if (this.timeFilter$$) { this.timeFilter$$.unsubscribe(); }
}
private syncChildLayers() {
// Force the sync the child layers with parent on the first load.
if (!this.map) {
return;
}
this.map.status$
.pipe(first())
.subscribe(() => {
this.map.layers
.filter(layer => layer.options.linkedLayers?.links)
.map(layer => {
layer.options.linkedLayers.links.map(link => {
if (link.properties?.indexOf(LinkedProperties.VISIBLE) !== -1) {
layer.ol.set('visible', !(layer.visible), false);
layer.ol.set('visible', !(layer.visible), false);
layer.visible = layer.visible;
}
if (link.properties?.indexOf(LinkedProperties.OPACITY) !== -1) {
const baseOpacity = layer.ol.get('opacity');
layer.ol.set('opacity', 0, false);
layer.ol.set('opacity', baseOpacity, false);
layer.opacity = layer.opacity;
}
if (link.properties?.indexOf(LinkedProperties.MINRESOLUTION) !== -1) {
const baseMinResolution = layer.ol.get('minResolution');
layer.ol.set('minResolution', 0, false);
layer.ol.set('minResolution', baseMinResolution, false);
layer.minResolution = layer.minResolution;
}
if (link.properties?.indexOf(LinkedProperties.MAXRESOLUTION) !== -1) {
const baseMaxResolution = layer.ol.get('maxResolution');
layer.ol.set('maxResolution', 0, false);
layer.ol.set('maxResolution', baseMaxResolution, false);
layer.minResolution = layer.minResolution;
}
if (link.properties?.indexOf(LinkedProperties.OGCFILTERS) !== -1) {
const ogcFilters$ = (layer.dataSource as OgcFilterableDataSource).ogcFilters$;
ogcFilters$.next(ogcFilters$.value);
}
if (link.properties?.indexOf(LinkedProperties.TIMEFILTER) !== -1) {
const timeFilter$ = (layer.dataSource as TimeFilterableDataSource).timeFilter$;
timeFilter$.next(timeFilter$.value);
}
});
});
});
}
private transferCommonProperties(layerChange) {
const key = layerChange.key;
const layerChangeProperties = layerChange.target.getProperties();
const newValue = layerChangeProperties[key];
if (['visible', 'opacity', 'minResolution', 'maxResolution'].indexOf(key) === -1) {
return;
}
const linkedLayers = layerChangeProperties.linkedLayers as LayersLink;
if (!linkedLayers) {
return;
}
const currentLinkedId = linkedLayers.linkId;
const currentLinks = linkedLayers.links;
const isParentLayer = currentLinks ? true : false;
if (isParentLayer) {
// search for child layers
const silent = true;
currentLinks.map(link => {
if (!link.properties || link.properties.indexOf(key) === -1) {
return;
}
link.linkedIds.map(linkedId => {
const layerToApply = this.map.layers.find(layer => layer.options.linkedLayers?.linkId === linkedId);
if (layerToApply) {
layerToApply.ol.set(key, newValue, silent);
if (key === 'visible') {
layerToApply.visible$.next(newValue);
}
}
});
});
} else {
// search for parent layer
const silent = false;
this.map.layers.map(layer => {
if (layer.options.linkedLayers?.links) {
layer.options.linkedLayers.links.map(l => {
if (l.properties && l.properties.indexOf(key) !== -1 &&
l.bidirectionnal !== false && l.linkedIds.indexOf(currentLinkedId) !== -1) {
layer.ol.set(key, newValue, silent);
if (key === 'visible') {
layer.visible$.next(newValue);
}
}
});
}
});
}
}
private transferOgcFiltersProperties(ogcFilters) {
const linkedLayers = this.ol.getProperties().linkedLayers as LayersLink;
if (!linkedLayers) {
return;
}
const currentLinkedId = linkedLayers.linkId;
const currentLinks = linkedLayers.links;
const isParentLayer = currentLinks ? true : false;
if (isParentLayer) {
// search for child layers
currentLinks.map(link => {
if (!link.properties || link.properties.indexOf(LinkedProperties.OGCFILTERS) === -1) {
return;
}
link.linkedIds.map(linkedId => {
const layerToApply = this.map.layers.find(layer => layer.options.linkedLayers?.linkId === linkedId);
if (layerToApply) {
const layerType = layerToApply.ol.getProperties().sourceOptions.type;
(layerToApply.dataSource as OgcFilterableDataSource).setOgcFilters(ogcFilters, false);
if (layerType === 'wfs') {
if (ogcFilters !== (layerToApply.dataSource as OgcFilterableDataSource).ogcFilters$.value) {
layerToApply.ol.getSource().refresh();
}
}
if (layerType === 'wms') {
let appliedOgcFilter;
if (this.ol.getProperties().sourceOptions.type === 'wfs') {
appliedOgcFilter = this.ogcFilterWriter.handleOgcFiltersAppliedValue(
this.layer.dataSource.options as OgcFilterableDataSourceOptions,
(this.dataSource.options as any).fieldNameGeometry,
undefined,
this.map.viewController.getOlProjection()
);
} else if (this.ol.getProperties().sourceOptions.type === 'wms') {
appliedOgcFilter = (this.dataSource as WMSDataSource).ol.getParams().FILTER;
}
(layerToApply.dataSource as WMSDataSource).ol.updateParams({ FILTER: appliedOgcFilter });
}
}
});
});
} else {
// search for parent layer
this.map.layers.map(layer => {
if (layer.options.linkedLayers?.links) {
layer.options.linkedLayers.links.map(l => {
if (l.properties && l.properties.indexOf(LinkedProperties.OGCFILTERS) !== -1 &&
l.bidirectionnal !== false && l.linkedIds.indexOf(currentLinkedId) !== -1) {
const layerType = layer.ol.getProperties().sourceOptions.type;
if (layerType === 'wfs') {
if (ogcFilters !== (layer.dataSource as OgcFilterableDataSource).ogcFilters$.value) {
(layer.dataSource as OgcFilterableDataSource).setOgcFilters(ogcFilters, true);
layer.ol.getSource().refresh();
}
}
if (layerType === 'wms') {
let appliedOgcFilter;
if (this.ol.getProperties().sourceOptions.type === 'wfs') {
appliedOgcFilter = this.ogcFilterWriter.handleOgcFiltersAppliedValue(
layer.dataSource.options as OgcFilterableDataSourceOptions,
(this.dataSource.options as any).fieldNameGeometry,
undefined,
this.map.viewController.getOlProjection()
);
} else if (this.ol.getProperties().sourceOptions.type === 'wms') {
appliedOgcFilter = (this.dataSource as WMSDataSource).ol.getParams().FILTER;
}
(layer.dataSource as WMSDataSource).ol.updateParams({ FILTER: appliedOgcFilter });
(layer.dataSource as OgcFilterableDataSource).setOgcFilters(ogcFilters, true);
}
}
});
}
});
}
}
private transferTimeFilterProperties(timeFilter) {
const linkedLayers = this.ol.getProperties().linkedLayers as LayersLink;
if (!linkedLayers) {
return;
}
const currentLinkedId = linkedLayers.linkId;
const currentLinks = linkedLayers.links;
const isParentLayer = currentLinks ? true : false;
if (isParentLayer) {
// search for child layers
currentLinks.map(link => {
if (!link.properties || link.properties.indexOf(LinkedProperties.TIMEFILTER) === -1) {
return;
}
link.linkedIds.map(linkedId => {
const childLayer = this.map.layers.find(layer =>
layer.dataSource instanceof WMSDataSource &&
layer.options.linkedLayers?.linkId === linkedId);
if (childLayer) {
(childLayer.dataSource as TimeFilterableDataSource).setTimeFilter(timeFilter, false);
const appliedTimeFilter = this.ol.get('values_').source.getParams().TIME;
(childLayer.dataSource as WMSDataSource).ol.updateParams({ TIME: appliedTimeFilter });
}
});
});
} else {
// search for parent layer
this.map.layers
.filter(layer => layer.dataSource instanceof WMSDataSource)
.map(parentLayer => {
if (parentLayer.options.linkedLayers?.links) {
parentLayer.options.linkedLayers.links.map(l => {
if (l.properties && l.properties.indexOf(LinkedProperties.TIMEFILTER) !== -1 &&
l.bidirectionnal !== false && l.linkedIds.indexOf(currentLinkedId) !== -1) {
const appliedTimeFilter = this.ol.get('values_').source.getParams().TIME;
(parentLayer.dataSource as WMSDataSource).ol.updateParams({ TIME: appliedTimeFilter });
(parentLayer.dataSource as TimeFilterableDataSource).setTimeFilter(timeFilter, true);
}
});
}
});
}
}
}