-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathWMTSCapabilitiesParser.class.js
237 lines (220 loc) · 8.86 KB
/
WMTSCapabilitiesParser.class.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
import WMTSCapabilities from 'ol/format/WMTSCapabilities'
import proj4 from 'proj4'
import { LayerAttribution } from '@/api/layers/AbstractLayer.class'
import ExternalWMTSLayer from '@/api/layers/ExternalWMTSLayer.class'
import allCoordinateSystems, { WGS84 } from '@/utils/coordinates/coordinateSystems'
import log from '@/utils/logging'
function parseCrs(crs) {
let epsgNumber = crs?.split(':').pop()
if (/84/.test(epsgNumber)) {
epsgNumber = '4326'
}
return allCoordinateSystems.find((system) => system.epsg === `EPSG:${epsgNumber}`)
}
function findLayer(layerId, startFrom) {
let layer = null
const layers = startFrom
for (let i = 0; i < layers.length && !layer; i++) {
if (layers[i].Identifier === layerId) {
layer = layers[i]
} else if (!layers[i].Identifier && layers[i].Title === layerId) {
layer = layers[i]
}
}
return layer
}
/** Wrapper around the OpenLayer WMSCapabilities to add more functionalities */
export default class WMTSCapabilitiesParser {
constructor(content, originUrl) {
const parser = new WMTSCapabilities()
const capabilities = parser.read(content)
if (!capabilities.version) {
throw new Error(`Failed to parse WMTS Capabilities: invalid content`)
}
Object.assign(this, capabilities)
this.originUrl = originUrl
}
/**
* Find recursively in the capabilities the matching layer ID node
*
* @param {string} layerId Layer ID to search for
* @returns {WMTSCapabilities.Contents.Layer} Capability layer node
*/
findLayer(layerId) {
return findLayer(layerId, this.Contents.Layer)
}
/**
* Get ExternalWMTSLayer object from the capabilities for the given layer ID
*
* @param {string} layerId WMTS Capabilities layer ID to retrieve
* @param {CoordinateSystem} projection Projection currently used by the application
* @param {number} opacity
* @param {boolean} visible
* @param {boolean} ignoreError Don't throw exception in case of error, but return a default
* value or null
* @returns {ExternalWMTSLayer | null} ExternalWMTSLayer object
*/
getExternalLayerObject(layerId, projection, opacity = 1, visible = true, ignoreError = true) {
const layer = this.findLayer(layerId)
if (!layer) {
const msg = `No WMTS layer ${layerId} found in Capabilities ${this.originUrl}`
log.error(msg)
if (!ignoreError) {
throw new Error(msg)
}
return null
}
return this._getExternalLayerObject(layer, projection, opacity, visible, ignoreError)
}
/**
* Get all ExternalWMTSLayer objects from capabilities
*
* @param {CoordinateSystem} projection Projection currently used by the application
* @param {number} opacity
* @param {boolean} visible
* @param {boolean} ignoreError Don't throw exception in case of error, but return a default
* value or null
* @returns {[ExternalWMTSLayer]} List of ExternalWMTSLayer objects
*/
getAllExternalLayerObjects(projection, opacity = 1, visible = true, ignoreError = true) {
return this.Contents.Layer.map((layer) =>
this._getExternalLayerObject(layer, projection, opacity, visible, ignoreError)
).filter((layer) => !!layer)
}
_getExternalLayerObject(layer, projection, opacity, visible, ignoreError) {
const attributes = this._getLayerAttributes(layer, projection, ignoreError)
if (!attributes) {
return null
}
return new ExternalWMTSLayer(
attributes.title,
opacity,
visible,
attributes.url,
attributes.layerId,
attributes.attributions,
attributes.abstract,
attributes.extent,
false
)
}
_getLayerAttributes(layer, projection, ignoreError = true) {
let layerId = layer.Identifier
if (!layerId) {
// fallback to Title
layerId = layer.Title
}
if (!layerId) {
const msg = `No layer identifier found in Capabilities ${this.originUrl}`
log.error(msg, layer)
if (ignoreError) {
return {}
}
throw new Error(msg)
}
return {
layerId: layerId,
title: layer.Title || layerId,
url: this.originUrl,
version: this.version,
abstract: layer.Abstract,
attributions: this._getLayerAttribution(layerId),
extent: this._getLayerExtent(layerId, layer, projection, ignoreError),
}
}
_findTileMatrixSetFromLinks(links) {
let tileMatrixSet = null
links?.forEach((link) => {
tileMatrixSet = this.Contents.TileMatrixSet?.find(
(set) => set.Identifier === link.TileMatrixSet
)
if (tileMatrixSet) {
return
}
})
return tileMatrixSet
}
_getLayerExtent(layerId, layer, projection, ignoreError) {
let layerExtent = null
let extentEpsg = null
// First try to get the extent from the default bounding box
if (layer.WGS84BoundingBox?.length) {
layerExtent = [
[layer.WGS84BoundingBox[0], layer.WGS84BoundingBox[1]],
[layer.WGS84BoundingBox[2], layer.WGS84BoundingBox[3]],
]
extentEpsg = WGS84.epsg
}
// Some provider don't uses the WGS84BoundingBox, but uses the BoundingBox instead
else if (layer.BoundingBox) {
// search for a matching crs bounding box
const matching = layer.BoundingBox.find((bbox) => parseCrs(bbox.crs) === projection)
if (matching) {
layerExtent = [
[matching.extent[0], matching.extent[1]],
[matching.extent[2], matching.extent[3]],
]
} else if (layer.BoundingBox.length === 1 && !layer.BoundingBox[0].crs) {
// if we have only one bounding box without CRS, then take it searching the CRS
// fom the TileMatrixSet
const tileMatrixSet = this._findTileMatrixSetFromLinks(layer.TileMatrixSetLink)
extentEpsg = parseCrs(tileMatrixSet?.SupportedCRS)?.epsg
if (extentEpsg) {
layerExtent = [
[layer.BoundingBox[0].extent[0], layer.BoundingBox[0].extent[1]],
[layer.BoundingBox[0].extent[2], layer.BoundingBox[0].extent[3]],
]
}
} else {
// if we have multiple bounding box search for the one that specify a supported CRS
const supported = layer.BoundingBox.find((bbox) => parseCrs(bbox.crs))
if (supported) {
extentEpsg = parseCrs(supported.crs).epsg
layerExtent = [
[supported.extent[0], supported.extent[1]],
[supported.extent[2], supported.extent[3]],
]
}
}
}
// If we didn't find a valid and supported bounding box in the layer then fallback to the
// linked TileMatrixSet. NOTE: some provider don't specify the bounding box at the layer
// level but on the TileMatrixSet
if (!layerExtent && this.Contents.TileMatrixSet) {
const tileMatrixSet = this._findTileMatrixSetFromLinks(layer.TileMatrixSetLink)
const system = parseCrs(tileMatrixSet?.SupportedCRS)
if (tileMatrixSet && system && tileMatrixSet.BoundingBox) {
layerExtent = [
[tileMatrixSet.BoundingBox[0], tileMatrixSet.BoundingBox[1]],
[tileMatrixSet.BoundingBox[2], tileMatrixSet.BoundingBox[3]],
]
extentEpsg = system.epsg
}
}
// Convert the extent if needed
if (layerExtent && extentEpsg && projection.epsg !== extentEpsg) {
layerExtent = [
proj4(extentEpsg, projection.epsg, layerExtent[0]),
proj4(extentEpsg, projection.epsg, layerExtent[1]),
]
}
if (!layerExtent) {
const msg = `No layer extent found for ${layerId}`
log.error(msg, layer)
if (!ignoreError) {
throw new Error(msg)
}
}
return layerExtent
}
_getLayerAttribution(layerId) {
let title = this.ServiceProvider?.ProviderName
let url = this.ServiceProvider?.ProviderSite
if (!title) {
const msg = `No attribution title for layer ${layerId}`
log.error(msg)
title = new URL(this.originUrl).hostname
}
return [new LayerAttribution(title, url)]
}
}