-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathWMSLayer.js
287 lines (264 loc) · 11.9 KB
/
WMSLayer.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
/*
* Copyright 2017, 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/openlayers/Layers';
import isNil from 'lodash/isNil';
import isEqual from 'lodash/isEqual';
import union from 'lodash/union';
import isArray from 'lodash/isArray';
import assign from 'object-assign';
import axios from '../../../../libs/ajax';
import CoordinatesUtils from '../../../../utils/CoordinatesUtils';
import { getProjection } from '../../../../utils/ProjectionUtils';
import { getConfigProp } from '../../../../utils/ConfigUtils';
import {optionsToVendorParams} from '../../../../utils/VendorParamsUtils';
import {addAuthenticationToSLD, addAuthenticationParameter, getAuthenticationHeaders} from '../../../../utils/SecurityUtils';
import ImageLayer from 'ol/layer/Image';
import ImageWMS from 'ol/source/ImageWMS';
import {get} from 'ol/proj';
import TileLayer from 'ol/layer/Tile';
import TileWMS from 'ol/source/TileWMS';
import VectorTileSource from 'ol/source/VectorTile';
import VectorTileLayer from 'ol/layer/VectorTile';
import { isVectorFormat } from '../../../../utils/VectorTileUtils';
import { isValidResponse } from '../../../../utils/WMSUtils';
import { OL_VECTOR_FORMATS, applyStyle } from '../../../../utils/openlayers/VectorTileUtils';
import { proxySource, getWMSURLs, wmsToOpenlayersOptions, toOLAttributions, generateTileGrid } from '../../../../utils/openlayers/WMSUtils';
const loadFunction = (options, headers) => function(image, src) {
// fixes #3916, see https://gis.stackexchange.com/questions/175057/openlayers-3-wms-styling-using-sld-body-and-post-request
let img = image.getImage();
let newSrc = proxySource(options.forceProxy, src);
if (typeof window.btoa === 'function' && src.length >= (options.maxLengthUrl || getConfigProp('miscSettings')?.maxURLLength || Infinity)) {
// GET ALL THE PARAMETERS OUT OF THE SOURCE URL**
let [url, ...dataEntries] = src.split("&");
url = proxySource(options.forceProxy, url);
// SET THE PROPER HEADERS AND FINALLY SEND THE PARAMETERS
axios.post(url, "&" + dataEntries.join("&"), {
headers: {
"Content-type": "application/x-www-form-urlencoded;charset=utf-8",
...headers
},
responseType: 'arraybuffer'
}).then(response => {
if (response.status === 200) {
const uInt8Array = new Uint8Array(response.data);
let i = uInt8Array.length;
const binaryString = new Array(i);
while (i--) {
binaryString[i] = String.fromCharCode(uInt8Array[i]);
}
const dataImg = binaryString.join('');
const type = response.headers['content-type'];
if (type.indexOf('image') === 0) {
img.src = 'data:' + type + ';base64,' + window.btoa(dataImg);
}
}
}).catch(e => {
console.error(e);
});
} else {
if (headers) {
axios.get(newSrc, {
headers,
responseType: 'blob'
}).then(response => {
if (isValidResponse(response)) {
image.getImage().src = URL.createObjectURL(response.data);
} else {
// #10701 this is needed to trigger the imageloaderror event
// in ol otherwise this event is not triggered if you assign
// the xml content of the exception to the src attribute
image.getImage().src = null;
console.error("error: " + response.data);
}
}).catch(e => {
console.error(e);
});
} else {
img.src = newSrc;
}
}
};
const createLayer = (options, map, mapId) => {
// the useForElevation in wms types will be deprecated
// as support for existing configuration
// we can use this fallback
if (options.useForElevation) {
return Layers.createLayer('elevation', {
...options,
provider: 'wms'
}, map, mapId);
}
const urls = getWMSURLs(isArray(options.url) ? options.url : [options.url]);
const queryParameters = wmsToOpenlayersOptions(options) || {};
urls.forEach(url => addAuthenticationParameter(url, queryParameters, options.securityToken));
const headers = getAuthenticationHeaders(urls[0], options.securityToken);
const vectorFormat = isVectorFormat(options.format);
if (options.singleTile && !vectorFormat) {
return new ImageLayer({
msId: options.id,
opacity: options.opacity !== undefined ? options.opacity : 1,
visible: options.visibility !== false,
zIndex: options.zIndex,
minResolution: options.minResolution,
maxResolution: options.maxResolution,
source: new ImageWMS({
url: urls[0],
crossOrigin: options.crossOrigin,
attributions: toOLAttributions(options.credits),
params: queryParameters,
ratio: options.ratio || 1,
imageLoadFunction: loadFunction(options, headers)
})
});
}
const sourceOptions = {
attributions: toOLAttributions(options.credits),
urls: urls,
crossOrigin: options.crossOrigin,
params: queryParameters,
tileGrid: generateTileGrid(options, map),
tileLoadFunction: loadFunction(options, headers)
};
const wmsSource = new TileWMS({ ...sourceOptions });
const layerConfig = {
msId: options.id,
opacity: options.opacity !== undefined ? options.opacity : 1,
visible: options.visibility !== false,
zIndex: options.zIndex,
minResolution: options.minResolution,
maxResolution: options.maxResolution
};
let layer;
if (vectorFormat) {
layer = new VectorTileLayer({
...layerConfig,
source: new VectorTileSource({
...sourceOptions,
format: new OL_VECTOR_FORMATS[options.format]({
layerName: '_layer_'
}),
tileUrlFunction: (tileCoord, pixelRatio, projection) => wmsSource.tileUrlFunction(tileCoord, pixelRatio, projection)
})
});
} else {
layer = new TileLayer({
...layerConfig,
source: wmsSource
});
}
layer.set('map', map);
if (vectorFormat) {
layer.set('wmsSource', wmsSource);
if (options.vectorStyle) {
applyStyle(options.vectorStyle, layer, map);
}
}
return layer;
};
const mustCreateNewLayer = (oldOptions, newOptions) => {
return (oldOptions.singleTile !== newOptions.singleTile
|| oldOptions.securityToken !== newOptions.securityToken
|| oldOptions.ratio !== newOptions.ratio
// no way to remove attribution when credits are removed, so have re-create the layer is needed. Seems to be solved in OL v5.3.0, due to the ol commit 9b8232f65b391d5d381d7a99a7cd070fc36696e9 (https://github.com/openlayers/openlayers/pull/7329)
|| oldOptions.credits !== newOptions.credits && !newOptions.credits
|| isVectorFormat(oldOptions.format) !== isVectorFormat(newOptions.format)
|| isVectorFormat(oldOptions.format) && isVectorFormat(newOptions.format) && oldOptions.format !== newOptions.format
|| oldOptions.localizedLayerStyles !== newOptions.localizedLayerStyles
|| oldOptions.tileSize !== newOptions.tileSize
|| oldOptions.forceProxy !== newOptions.forceProxy
|| oldOptions.tileGridStrategy !== newOptions.tileGridStrategy
|| !isEqual(oldOptions.tileGrids, newOptions.tileGrids)
);
};
Layers.registerType('wms', {
create: createLayer,
update: (layer, newOptions, oldOptions, map) => {
const newIsVector = isVectorFormat(newOptions.format);
if (mustCreateNewLayer(oldOptions, newOptions)) {
// TODO: do we need to clean anything before re-creating stuff from scratch?
return createLayer(newOptions, map);
}
let needsRefresh = false;
if (newIsVector && newOptions.vectorStyle && !isEqual(newOptions.vectorStyle, oldOptions.vectorStyle || {})) {
applyStyle(newOptions.vectorStyle, layer, map);
needsRefresh = true;
}
const wmsSource = layer.get('wmsSource') || layer.getSource();
const vectorSource = newIsVector ? layer.getSource() : null;
if (oldOptions.srs !== newOptions.srs) {
const normalizedSrs = CoordinatesUtils.normalizeSRS(newOptions.srs, newOptions.allowedSRS);
const extent = get(normalizedSrs).getExtent() || getProjection(normalizedSrs).extent;
if (newOptions.singleTile && !newIsVector) {
layer.setExtent(extent);
} else {
const tileGrid = generateTileGrid(newOptions, map);
wmsSource.tileGrid = tileGrid;
if (vectorSource) {
vectorSource.tileGrid = tileGrid;
}
}
needsRefresh = true;
}
if (oldOptions.credits !== newOptions.credits && newOptions.credits) {
wmsSource.setAttributions(toOLAttributions(newOptions.credits));
needsRefresh = true;
}
let changed = false;
let oldParams;
let newParams;
if (oldOptions && wmsSource && wmsSource.updateParams) {
if (oldOptions.params && newOptions.params) {
changed = union(
Object.keys(oldOptions.params),
Object.keys(newOptions.params)
).reduce((found, param) => {
if (newOptions.params[param] !== oldOptions.params[param]) {
return true;
}
return found;
}, false);
} else if ((!oldOptions.params && newOptions.params) || (oldOptions.params && !newOptions.params)) {
changed = true;
}
oldParams = wmsToOpenlayersOptions(oldOptions);
newParams = wmsToOpenlayersOptions(newOptions);
changed = changed || ["LAYERS", "STYLES", "FORMAT", "TRANSPARENT", "TILED", "VERSION", "_v_", "CQL_FILTER", "SLD", "VIEWPARAMS"].reduce((found, param) => {
if (oldParams[param] !== newParams[param]) {
return true;
}
return found;
}, false);
needsRefresh = needsRefresh || changed;
}
if (oldOptions.minResolution !== newOptions.minResolution) {
layer.setMinResolution(newOptions.minResolution === undefined ? 0 : newOptions.minResolution);
}
if (oldOptions.maxResolution !== newOptions.maxResolution) {
layer.setMaxResolution(newOptions.maxResolution === undefined ? Infinity : newOptions.maxResolution);
}
if (needsRefresh) {
// forces tile cache drop
// this prevents old cached tiles at lower zoom levels to be
// rendered during new params load
wmsSource?.tileCache?.pruneExceptNewestZ?.();
if (vectorSource) {
vectorSource.clear();
vectorSource.refresh();
}
if (changed) {
const params = assign(newParams, addAuthenticationToSLD(optionsToVendorParams(newOptions) || {}, newOptions));
wmsSource.updateParams(assign(params, Object.keys(oldParams || {}).reduce((previous, key) => {
return !isNil(params[key]) ? previous : assign(previous, {
[key]: undefined
});
}, {})));
}
}
return null;
}
});