-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathWFSLayer.js
More file actions
213 lines (203 loc) · 8.78 KB
/
WFSLayer.js
File metadata and controls
213 lines (203 loc) · 8.78 KB
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
/**
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import Layers from '../../../../utils/cesium/Layers';
import * as Cesium from 'cesium';
import isEqual from 'lodash/isEqual';
import { getFeature, getFeatureLayer } from '../../../../api/WFS';
import { needsReload, needsCredentials, getConfig } from '../../../../utils/WFSLayerUtils';
import { optionsToVendorParams } from '../../../../utils/VendorParamsUtils';
import {
getStyle,
layerToGeoStylerStyle
} from '../../../../utils/VectorStyleUtils';
import { applyDefaultStyleToVectorLayer } from '../../../../utils/StyleUtils';
import GeoJSONStyledFeatures from '../../../../utils/cesium/GeoJSONStyledFeatures';
import { ServerTypes } from '../../../../utils/LayersUtils';
import TiledBillboardCollection from '../../../../utils/cesium/TiledBillboardCollection';
const requestFeatures = (options, params, config) => {
return getFeature(options.url, options.name, {
// ...(!params?.CQL_FILTER && { bbox: [minx, miny, maxx, maxy, projection].join(',') }),
outputFormat: 'application/json',
srsname: 'EPSG:4326',
...params
}, config);
};
const createLoader = (options) => {
if (options.serverType === ServerTypes.NO_VENDOR) {
if (needsCredentials(options)) {
return () => null;
}
if (options?.strategy === 'bbox' || options.strategy === 'tile') {
return (extent) => getFeatureLayer(options, { filters: [{
spatialField: {
operation: 'BBOX',
geometry: {
projection: 'EPSG:4326',
extent: [extent] // use array because bbox is buggy
}
}
}], proj: 'EPSG:4326' }, getConfig(options));
}
return () => getFeatureLayer(options, { filters: [], proj: 'EPSG:4326' }, getConfig(options));
}
const params = optionsToVendorParams(options);
const config = getConfig(options);
return () => requestFeatures(options, params, config);
};
const createLayer = (options, map) => {
if (!options.visibility) {
return {
detached: true,
styledFeatures: undefined,
remove: () => {}
};
}
let styledFeatures = new GeoJSONStyledFeatures({
features: [],
id: options?.id,
map: map,
opacity: options.opacity,
queryable: options.queryable === undefined || options.queryable,
styleRules: options?.style?.body?.rules || []
});
let loader;
let loadingBbox;
let bboxTimeout;
let tiledPrimitive;
const add = () => {
loader = createLoader(options);
if (options?.strategy === 'bbox' && options?.serverType === ServerTypes.NO_VENDOR) {
loadingBbox = () => {
if (bboxTimeout) {
clearTimeout(bboxTimeout);
bboxTimeout = undefined;
}
bboxTimeout = setTimeout(() => {
const viewRectangle = map.camera.computeViewRectangle();
const cameraPitch = Math.abs(Cesium.Math.toDegrees(map.camera.pitch));
if (viewRectangle && cameraPitch > 60) {
loader([
Cesium.Math.toDegrees(viewRectangle.west),
Cesium.Math.toDegrees(viewRectangle.south),
Cesium.Math.toDegrees(viewRectangle.east),
Cesium.Math.toDegrees(viewRectangle.north)
])
.then(({ data: collection }) => {
styledFeatures.setFeatures(collection.features);
layerToGeoStylerStyle(options)
.then((style) => {
getStyle(applyDefaultStyleToVectorLayer({
...options,
features: collection.features,
style
}), 'cesium')
.then((styleFunc) => {
styledFeatures.setStyleFunction(styleFunc);
});
});
});
}
}, 300);
};
map.camera.moveEnd.addEventListener(loadingBbox);
} else if (options?.strategy === 'tile' && options?.serverType === ServerTypes.NO_VENDOR) {
// Note that 'TiledBillboardCollection' can only be used for point geometric features for now, So WFS with other than point geometry should not be used for now on strategy === 'tile'
tiledPrimitive = new TiledBillboardCollection({
map,
features: [],
id: options?.id,
opacity: options.opacity,
// the maximum and minimum levels refers to the request done by TiledBillboardCollection
// these values are different from maximum and minimum resolutions to avoid visibility issue when the camera is tilted
// we should review resolutions behavior to match similar zoom level when camera is tilted
minimumLevel: options.minimumLevel || 17,
maximumLevel: options.maximumLevel || 17,
msId: options.id,
debugTiles: false,
queryable: options.queryable === undefined || options.queryable,
style: options.style,
tileWidth: options?.tileSize || 512,
loadTile: (tile) => loader([
Cesium.Math.toDegrees(tile.rectangle.west),
Cesium.Math.toDegrees(tile.rectangle.south),
Cesium.Math.toDegrees(tile.rectangle.east),
Cesium.Math.toDegrees(tile.rectangle.north)
]).then(({ data: collection }) => collection)
});
tiledPrimitive.load();
} else {
loader()
.then(({ data: collection }) => {
styledFeatures.setFeatures(collection.features);
layerToGeoStylerStyle(options)
.then((style) => {
getStyle(applyDefaultStyleToVectorLayer({
...options,
features: collection.features,
style
}), 'cesium')
.then((styleFunc) => {
styledFeatures.setStyleFunction(styleFunc);
});
});
});
}
};
return {
detached: true,
styledFeatures,
add,
remove: () => {
if (styledFeatures) {
styledFeatures.destroy();
styledFeatures = undefined;
}
if (tiledPrimitive) {
tiledPrimitive.destroy();
tiledPrimitive = undefined;
}
if (loadingBbox) {
map.camera.moveEnd.removeEventListener(loadingBbox);
}
}
};
};
Layers.registerType('wfs', {
create: createLayer,
update: (layer, newOptions, oldOptions, map) => {
if (
needsReload(oldOptions, newOptions) ||
oldOptions.forceProxy !== newOptions.forceProxy ||
!isEqual(oldOptions.security, newOptions.security)
) {
return createLayer(newOptions, map);
}
if (layer?.styledFeatures && !isEqual(newOptions.style, oldOptions.style)) {
// update style rules here
if (!isEqual(newOptions?.style?.body?.rules, oldOptions?.style?.body?.rules)) {
let styleRules = newOptions?.style?.body?.rules || [];
layer.styledFeatures._setStyleRules(styleRules);
}
layerToGeoStylerStyle(newOptions)
.then((style) => {
getStyle(applyDefaultStyleToVectorLayer({
...newOptions,
features: layer?.styledFeatures?._originalFeatures,
style
}), 'cesium')
.then((styleFunc) => {
layer.styledFeatures.setStyleFunction(styleFunc);
});
});
}
if (layer?.styledFeatures && newOptions.opacity !== oldOptions.opacity) {
layer.styledFeatures.setOpacity(newOptions.opacity);
}
return null;
}
});