-
Notifications
You must be signed in to change notification settings - Fork 41
/
Layer.js
298 lines (274 loc) · 8.89 KB
/
Layer.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
288
289
290
291
292
293
294
295
296
297
298
import TileLayer from 'ol/layer/Tile';
import TileWmsSource from 'ol/source/TileWMS';
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';
/**
* 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 === 'WMS') {
return this.createWmsLayer(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,
zIndex: lConf.zIndex,
confName: lConf.name,
confAttributions: lConf.attributions
};
},
/**
* Returns an OpenLayers WMS layer instance due to given config.
*
* @param {Object} lConf Layer config object
* @return {ol.layer.Tile} OL WMS layer instance
*/
createWmsLayer (lConf) {
const layer = new TileLayer({
...this.getCommonLayerOptions(lConf),
source: new TileWmsSource({
url: lConf.url,
params: {
'LAYERS': lConf.layers,
'TILED': lConf.tiled
},
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 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
let wfsRequest = lConf.url + '?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
});
var 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;
}
}