-
Notifications
You must be signed in to change notification settings - Fork 22
/
LayerUtil.ts
295 lines (270 loc) · 8.68 KB
/
LayerUtil.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import Logger from '@terrestris/base-util/dist/Logger';
import StringUtil from '@terrestris/base-util/dist/StringUtil/StringUtil';
import OpenLayersParser from 'geostyler-openlayers-parser';
import { isNil } from 'lodash';
import _uniq from 'lodash/uniq';
import { Extent as OlExtent } from 'ol/extent';
import OlFormatGeoJSON from 'ol/format/GeoJSON';
import OlLayerImage from 'ol/layer/Image';
import OlLayer from 'ol/layer/Layer';
import OlLayerTile from 'ol/layer/Tile';
import OlSourceImageWMS from 'ol/source/ImageWMS';
import OlSourceOSM from 'ol/source/OSM';
import OlSourceStadiaMaps from 'ol/source/StadiaMaps';
import OlSourceTileWMS from 'ol/source/TileWMS';
import OlSourceVector from 'ol/source/Vector';
import OlSourceWMTS from 'ol/source/WMTS';
import CapabilitiesUtil from '../CapabilitiesUtil/CapabilitiesUtil';
import { WmsLayer, WmtsLayer } from '../typeUtils/typeUtils';
import { InkmapGeoJsonLayer, InkmapLayer } from './InkmapTypes';
/**
* Helper class for layer interaction.
*
* @class LayerUtil
*/
class LayerUtil {
/**
* Returns the configured URL of the given layer.
*
* @param { WmsLayer | WmtsLayer } layer The layer
* to get the URL from.
* @returns {string} The layer URL.
*/
static getLayerUrl = (
layer: WmsLayer | WmtsLayer
): string => {
const layerSource = layer.getSource();
if (!layerSource) {
return '';
} else if (layerSource instanceof OlSourceTileWMS) {
return layerSource.getUrls()?.[0] ?? '';
} else if (layerSource instanceof OlSourceImageWMS) {
return layerSource.getUrl() ?? '';
} else {
return layerSource.getUrls()?.[0] ?? '';
}
};
/**
* Returns the extent of the given layer as defined in the
* appropriate Capabilities document.
*
* @param {WmsLayer} layer
* @param {RequestInit} fetchOpts Optional fetch options to make use of
* while requesting the Capabilities.
* @returns {Promise<[number, number, number, number]>} The extent of the layer.
*/
static async getExtentForLayer(
layer: WmsLayer,
fetchOpts: RequestInit = {}
): Promise<OlExtent> {
const capabilities = await CapabilitiesUtil.getWmsCapabilitiesByLayer(layer, fetchOpts);
let layerNodes = capabilities?.Capability?.Layer?.Layer;
if (!layerNodes) {
throw new Error('Unexpected format of the Capabilities.');
}
if (!Array.isArray(layerNodes)) {
layerNodes = [layerNodes];
}
const layerName = layer.getSource()?.getParams().LAYERS;
const version = layer.getSource()?.getParams().VERSION || '1.3.0';
const layers = layerNodes.filter((l: any) => {
return l.Name === layerName;
});
if (!layers || layers.length === 0) {
throw new Error('Could not find the desired layer in the Capabilities.');
}
let extent;
if (version === '1.3.0') {
const {
eastBoundLongitude,
northBoundLatitude,
southBoundLatitude,
westBoundLongitude
} = layers[0].EX_GeographicBoundingBox;
extent = [
westBoundLongitude,
southBoundLatitude,
eastBoundLongitude,
northBoundLatitude,
];
} else if (version === '1.1.0' || version === '1.1.1') {
const {
minx,
miny,
maxx,
maxy
} = layers[0].LatLonBoundingBox;
extent = [minx, miny, maxx, maxy];
}
if (!extent || extent?.length !== 4) {
throw new Error('No extent set in the Capabilities.');
}
return extent;
}
/**
* Converts a given OpenLayers layer to an inkmap layer spec.
*
*/
static async mapOlLayerToInkmap(
olLayer: OlLayer
): Promise<InkmapLayer> {
const source = olLayer.getSource();
if (!olLayer.getVisible()) {
// do not include invisible layers
return Promise.reject();
}
const opacity = olLayer.getOpacity();
const legendUrl = olLayer.get('legendUrl');
const layerName = olLayer.get('name');
let time;
if (source instanceof OlSourceTileWMS || source instanceof OlSourceImageWMS) {
time = source.getParams().TIME;
}
// todo: introduce config object which hold possible additional configurations
const attributionString = LayerUtil.getLayerAttributionsText(olLayer, ' ,', true);
if (source instanceof OlSourceTileWMS) {
return {
type: 'WMS',
url: source.getUrls()?.[0] ?? '',
opacity,
attribution: attributionString,
layer: source.getParams()?.LAYERS,
tiled: true,
legendUrl,
layerName,
customParams: {
...(time && { time })
}
};
} else if (source instanceof OlSourceImageWMS) {
return {
type: 'WMS',
url: source.getUrl() ?? '',
opacity,
attribution: attributionString,
layer: source.getParams()?.LAYERS,
tiled: false,
legendUrl,
layerName,
customParams: {
...(time && { time })
}
};
} else if (source instanceof OlSourceWMTS) {
const olTileGrid = source.getTileGrid();
const resolutions = olTileGrid?.getResolutions();
const matrixIds = resolutions?.map((res: number, idx: number) => idx);
const tileGrid = {
resolutions: olTileGrid?.getResolutions(),
extent: olTileGrid?.getExtent(),
matrixIds: matrixIds
};
return {
type: 'WMTS',
requestEncoding: source.getRequestEncoding(),
url: source.getUrls()?.[0] ?? '',
layer: source.getLayer(),
projection: source.getProjection()?.getCode(),
matrixSet: source.getMatrixSet(),
tileGrid,
format: source.getFormat(),
opacity,
attribution: attributionString,
legendUrl,
layerName
};
} else if (source instanceof OlSourceOSM) {
return {
type: 'XYZ',
url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png',
opacity,
attribution: '© OpenStreetMap (www.openstreetmap.org)',
tiled: true,
legendUrl,
layerName
};
} else if (source instanceof OlSourceStadiaMaps) {
const urls = source.getUrls();
if (isNil(urls)) {
return Promise.reject();
}
return {
type: 'XYZ',
url: urls[0],
opacity,
attribution: attributionString,
tiled: true,
legendUrl,
layerName
};
} else if (source instanceof OlSourceVector) {
const geojson = new OlFormatGeoJSON().writeFeaturesObject(source.getFeatures());
const parser = new OpenLayersParser();
const geojsonLayerConfig: InkmapGeoJsonLayer = {
type: 'GeoJSON',
geojson,
attribution: attributionString,
style: undefined,
legendUrl,
layerName
};
let olStyle = null;
// @ts-ignore
if (olLayer.getStyle) {
// @ts-ignore
olStyle = olLayer.getStyle();
}
// todo: support style function / different styles per feature
// const styles = source.getFeatures()?.map(f => f.getStyle());
if (olStyle) {
const gsStyle = await parser.readStyle(olStyle);
if (gsStyle.errors) {
Logger.error('Geostyler errors: ', gsStyle.errors);
}
if (gsStyle.warnings) {
Logger.warn('Geostyler warnings: ', gsStyle.warnings);
}
if (gsStyle.unsupportedProperties) {
Logger.warn('Detected unsupported style properties: ', gsStyle.unsupportedProperties);
}
if (gsStyle.output) {
geojsonLayerConfig.style = gsStyle.output;
}
}
return geojsonLayerConfig;
}
return Promise.reject();
}
/**
* Returns all attributions as text joined by a separator.
*
* @param {OlLayer} layer The layer to get the attributions from.
* @param {string} separator The separator separating multiple attributions.
* @param {boolean} removeDuplicates Whether to remove duplicated attribution
* strings or not.
* @returns {string} The attributions.
*/
static getLayerAttributionsText = (
layer: OlLayer,
separator: string = ', ',
removeDuplicates: boolean = false
): string => {
if (isNil(layer)) {
return '';
}
const attributionsFn = layer.getSource()?.getAttributions();
// @ts-ignore
let attributions = attributionsFn ? attributionsFn(undefined) : null;
let attributionString;
if (Array.isArray(attributions)) {
if (removeDuplicates) {
attributions = _uniq(attributions);
}
attributionString = attributions.map(StringUtil.stripHTMLTags).join(separator);
} else {
attributionString = attributions ? StringUtil.stripHTMLTags(attributions) : '';
}
return attributionString;
};
}
export default LayerUtil;