-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathLayer.js
More file actions
410 lines (378 loc) · 12.7 KB
/
Layer.js
File metadata and controls
410 lines (378 loc) · 12.7 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
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
import { Image as ImageLayer, Tile as TileLayer } from 'ol/layer';
import ImageWMS from 'ol/source/ImageWMS';
import TileWmsSource from 'ol/source/TileWMS';
import WMTS, { optionsFromCapabilities } from 'ol/source/WMTS.js';
import WMTSCapabilities from 'ol/format/WMTSCapabilities.js';
import OsmSource from 'ol/source/OSM';
import VectorTileLayer from 'ol/layer/VectorTile'
import VectorTileSource from 'ol/source/VectorTile'
import MvtFormat from 'ol/format/MVT'
import GeoJsonFormat from 'ol/format/GeoJSON'
import TopoJsonFormat from 'ol/format/TopoJSON'
import KmlFormat from 'ol/format/KML'
import GML2Format from 'ol/format/GML2'
import GML3Format from 'ol/format/GML3'
import GML32Format from 'ol/format/GML32'
import VectorLayer from 'ol/layer/Vector'
import VectorSource from 'ol/source/Vector'
import XyzSource from 'ol/source/XYZ'
import { bbox as bboxStrategy } from 'ol/loadingstrategy';
import { OlStyleFactory } from './OlStyle'
import { applyTransform } from 'ol/extent';
import { getTransform } from 'ol/proj';
import axios from 'axios';
import ObjectUtil from '../util/Object';
/**
* Factory, which creates OpenLayers layer instances according to a given config
* object.
*/
export const LayerFactory = {
/**
* Maps the format literal of the config to the corresponding OL module.
* @type {Object}
*/
formatMapping: {
MVT: MvtFormat,
GeoJSON: GeoJsonFormat,
TopoJSON: TopoJsonFormat,
KML: KmlFormat,
GML2: GML2Format,
GML3: GML3Format,
GML32: GML32Format
},
/**
* Maps the format literal of the config to the corresponding WFS outputFormat.
* @type {Object}
*/
wfsFormatMapping: {
GeoJSON: 'application/json',
GML2: 'text/xml; subtype=gml/2.1.2',
GML3: 'text/xml; subtype=gml/3.1.1',
GML32: 'text/xml; subtype=gml/3.2'
},
/**
* Returns an OpenLayers layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @param {ol/Map} olMap Optional OL map we work on
* @return {ol/layer/Base} OL layer instance
*/
getInstance (lConf, olMap) {
// create correct layer type
if (lConf.type === 'TILEWMS') {
return this.createTileWmsLayer(lConf);
} else if (lConf.type === 'IMAGEWMS') {
return this.createImageWmsLayer(lConf);
} else if (lConf.type === 'WMTS') {
return this.createTileWmtsLayer(lConf);
} else if (lConf.type === 'WFS') {
return this.createWfsLayer(lConf, olMap);
} else if (lConf.type === 'XYZ') {
return this.createXyzLayer(lConf);
} else if (lConf.type === 'OSM') {
return this.createOsmLayer(lConf);
} else if (lConf.type === 'VECTOR') {
return this.createVectorLayer(lConf);
} else if (lConf.type === 'VECTORTILE') {
return this.createVectorTileLayer(lConf);
} else {
return null;
}
},
/**
* Returns an OL layer options initialization object, containing
* attributes common to all layer types.
* @param {Object} lConf Layer config object
* @return {Object} OL layer options
*/
getCommonLayerOptions (lConf) {
return {
lid: lConf.lid,
isBaseLayer: lConf.isBaseLayer,
previewImage: lConf.previewImage,
displayInLayerList: lConf.displayInLayerList,
supportsPermalink: lConf.supportsPermalink,
extent: lConf.extent,
visible: lConf.visible,
opacity: lConf.opacity,
opacityControl: lConf.opacityControl,
zIndex: lConf.zIndex,
confName: lConf.name,
confAttributions: lConf.attributions,
legend: lConf.legend,
legendUrl: lConf.legendUrl,
legendOptions: lConf.legendOptions
};
},
/**
* Returns an OpenLayers WMS layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @return {ol.layer.Image} OL WMS layer instance
*/
createImageWmsLayer (lConf) {
// apply additional HTTP params
const params = { LAYERS: lConf.layers };
ObjectUtil.mergeDeep(params, lConf.params);
const layer = new ImageLayer({
...this.getCommonLayerOptions(lConf),
source: new ImageWMS({
url: lConf.url,
params,
serverType: lConf.serverType,
ratio: lConf.ratio,
interpolate: lConf.interpolate,
projection: lConf.projection,
crossOrigin: lConf.crossOrigin,
hoverable: lConf.hoverable,
hoverAttribute: lConf.hoverAttribute,
hoverOverlay: lConf.hoverOverlay
})
});
return layer;
},
/**
* Returns an OpenLayers Tiled WMS layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @return {ol.layer.Tile} OL Tiled WMS layer instance
*/
createTileWmsLayer (lConf) {
// apply additional HTTP params
const params = { LAYERS: lConf.layers };
ObjectUtil.mergeDeep(params, lConf.params);
const layer = new TileLayer({
...this.getCommonLayerOptions(lConf),
source: new TileWmsSource({
url: lConf.url,
params,
serverType: lConf.serverType,
tileGrid: lConf.tileGrid,
projection: lConf.projection,
crossOrigin: lConf.crossOrigin,
hoverable: lConf.hoverable,
hoverAttribute: lConf.hoverAttribute,
hoverOverlay: lConf.hoverOverlay
})
});
return layer;
},
/**
* Returns an OpenLayers Tiled WMTS layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @return {ol.layer.Tile} OL Tiled WMTS layer instance
*/
createTileWmtsLayer (lConf) {
// apply additional HTTP params
const WMTSlayer = new TileLayer({
...this.getCommonLayerOptions(lConf),
source: new TileWmsSource({ // fake source, will be replaced
url: ''
})
})
if (lConf.optionsFromCapabilities) {
const parser = new WMTSCapabilities();
fetch(lConf.optionsFromCapabilities.url).then(function (response) {
return response.text();
}).then(function (text) {
const capabilities = parser.read(text);
const options = optionsFromCapabilities(capabilities, {
layer: lConf.optionsFromCapabilities.layer,
matrixSet: lConf.optionsFromCapabilities.matrixSet,
projection: lConf.optionsFromCapabilities.projection,
requestEncoding: lConf.optionsFromCapabilities.requestEncoding,
style: lConf.optionsFromCapabilities.style,
format: lConf.optionsFromCapabilities.format,
crossOrigin: lConf.optionsFromCapabilities.crossOrigin
})
const Source = new WMTS({
...options,
attributions: lConf.attributions,
hoverable: lConf.hoverable,
hoverAttribute: lConf.hoverAttribute,
hoverOverlay: lConf.hoverOverlay
})
WMTSlayer.setSource(Source)
});
} else {
WMTSlayer.setSource(new WMTS({
url: lConf.url,
tileGrid: lConf.tileGrid,
cacheSize: lConf.cacheSize,
interpolate: lConf.interpolate,
reprojectionErrorThreshold: lConf.reprojectionErrorThreshold,
projection: lConf.projection,
crossOrigin: lConf.crossOrigin,
layer: lConf.layer,
style: lConf.style,
tilePixelRatio: lConf.tilePixelRatio,
format: lConf.format,
version: lConf.version,
matrixSet: lConf.matrixSet,
urls: lConf.urls,
wrapX: lConf.wrapX,
transition: lConf.transition,
zDirection: lConf.zDirection,
hoverable: lConf.hoverable,
hoverAttribute: lConf.hoverAttribute,
hoverOverlay: lConf.hoverOverlay
}))
}
return WMTSlayer;
},
/**
* Returns an OpenLayers WFS layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @param {ol/Map} olMap The OpenLayers map we work on
* @return {ol.layer.Tile} OL WFS layer instance
*/
createWfsLayer: function (lConf, olMap) {
const mapSrs = olMap.getView().getProjection().getCode();
// set a default projection if not set in config
if (!lConf.projection) {
lConf.projection = mapSrs;
}
// set a default WFS version if not set in config
if (!lConf.version) {
lConf.version = '1.1.0';
}
if (!lConf.format) {
lConf.format = 'GML3';
}
// detect the WFS output format
const outputFormat = this.wfsFormatMapping[lConf.format];
// overwrite format options, so they fit to the SRS (map vs. data)
if (mapSrs !== lConf.projection) {
lConf.formatConfig.dataProjection = lConf.projection;
lConf.formatConfig.featureProjection = mapSrs;
}
const vectorSource = new VectorSource({
format: new this.formatMapping[lConf.format](lConf.formatConfig),
loader: (extent) => {
// assemble WFS GetFeature request
const pre = lConf.url.includes('?') ? '&' : '?';
let wfsRequest = lConf.url + pre + 'service=WFS&' +
'version=' + lConf.version + '&request=GetFeature&' +
'typename=' + lConf.typeName + '&' +
'outputFormat=' + outputFormat + '&srsname=' + lConf.projection;
// add WFS version dependent feature limitation
if (Number.isInteger(parseInt(lConf.maxFeatures))) {
if (lConf.version.startsWith('1.')) {
wfsRequest += '&maxFeatures=' + lConf.maxFeatures;
} else {
wfsRequest += '&count=' + lConf.maxFeatures;
}
}
// add bbox filter
if (lConf.loadOnlyVisible !== false) {
if (mapSrs !== lConf.projection) {
extent = applyTransform(extent, getTransform(mapSrs, lConf.projection));
}
wfsRequest += '&bbox=' + extent.join(',') + ',' + lConf.projection + '';
}
// load data from WFS, parse and add to vector source
const request = {
method: 'GET',
url: wfsRequest
};
axios(request)
.then(response => {
const feats = vectorSource.getFormat().readFeatures(response.data);
vectorSource.addFeatures(feats);
})
.catch(() => {
vectorSource.removeLoadedExtent(extent);
});
},
strategy: lConf.loadOnlyVisible !== false ? bboxStrategy : undefined
});
const vector = new VectorLayer({
...this.getCommonLayerOptions(lConf),
source: vectorSource,
style: OlStyleFactory.getInstance(lConf.style),
columnMapping: lConf.columnMapping,
hoverable: lConf.hoverable,
hoverAttribute: lConf.hoverAttribute,
hoverOverlay: lConf.hoverOverlay
});
return vector;
},
/**
* Returns an XYZ based tile layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @return {ol.layer.Tile} OL XYZ layer instance
*/
createXyzLayer (lConf) {
const xyzLayer = new TileLayer({
...this.getCommonLayerOptions(lConf),
source: new XyzSource({
url: lConf.url,
tileGrid: lConf.tileGrid,
projection: lConf.projection,
crossOrigin: lConf.crossOrigin
})
});
return xyzLayer;
},
/**
* Returns an OpenLayers OSM layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @return {ol.layer.Tile} OL OSM layer instance
*/
createOsmLayer (lConf) {
const layer = new TileLayer({
...this.getCommonLayerOptions(lConf),
source: new OsmSource({
crossOrigin: lConf.crossOrigin
})
});
return layer;
},
/**
* Returns an OpenLayers vector layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @return {ol.layer.Vector} OL vector layer instance
*/
createVectorLayer (lConf) {
const vectorLayer = new VectorLayer({
...this.getCommonLayerOptions(lConf),
source: new VectorSource({
url: lConf.url,
format: new this.formatMapping[lConf.format](lConf.formatConfig)
}),
style: OlStyleFactory.getInstance(lConf.style),
columnMapping: lConf.columnMapping,
hoverable: lConf.hoverable,
hoverAttribute: lConf.hoverAttribute,
hoverOverlay: lConf.hoverOverlay
});
return vectorLayer;
},
/**
* Returns an OpenLayers vector tile layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @return {ol.layer.VectorTile} OL vector tile layer instance
*/
createVectorTileLayer (lConf) {
const vtLayer = new VectorTileLayer({
...this.getCommonLayerOptions(lConf),
source: new VectorTileSource({
url: lConf.url,
format: new this.formatMapping[lConf.format](),
tileGrid: lConf.tileGrid,
projection: lConf.projection
}),
style: OlStyleFactory.getInstance(lConf.style),
hoverable: lConf.hoverable,
hoverAttribute: lConf.hoverAttribute,
hoverOverlay: lConf.hoverOverlay
});
return vtLayer;
}
}