UNPKG

terriajs

Version:

Geospatial data visualization platform.

101 lines 4.05 kB
import i18next from "i18next"; import { createTransformer } from "mobx-utils"; import defined from "terriajs-cesium/Source/Core/defined"; import loadXML from "../../../Core/loadXML"; import { networkRequestError } from "../../../Core/TerriaError"; import xml2json from "../../../ThirdParty/xml2json"; export default class WebMapTileServiceCapabilities { json; static fromUrl = createTransformer((url) => { return Promise.resolve(loadXML(url)).then(function (capabilitiesXml) { const json = xml2json(capabilitiesXml); if (!capabilitiesXml || !defined(json.ServiceIdentification)) { throw networkRequestError({ title: i18next.t("models.webMapTileServiceCatalogGroup.invalidCapabilitiesTitle"), message: i18next.t("models.webMapTileServiceCatalogGroup.invalidCapabilitiesMessage", { url: url }) }); } return new WebMapTileServiceCapabilities(json); }); }); layers; tileMatrixSets; constructor(json) { this.json = json; this.layers = this.parseLayers(this.json.Contents?.Layer); this.tileMatrixSets = this.parseTileMatrixSets(this.json.Contents?.TileMatrixSet); } get ServiceIdentification() { return this.json.ServiceIdentification; } get OperationsMetadata() { const operationsMetadata = this.json.OperationsMetadata; if (!operationsMetadata) { return undefined; } const operation = Array.isArray(operationsMetadata.Operation) ? operationsMetadata.Operation : [operationsMetadata.Operation]; return operation.reduce((acc, operation) => { if (!acc[operation.name]) { acc[operation.name] = { Get: Array.isArray(operation.DCP.HTTP.Get) ? operation.DCP.HTTP.Get : [operation.DCP.HTTP.Get] }; } return acc; }, {}); } get ServiceProvider() { return this.json.ServiceProvider; } /** * Finds the layer in GetCapabilities corresponding to a given layer name. Names are * resolved as foll * * The layer has the title exact with the name specified. * * The layer name matches the name in the spec if the namespace portion is removed. * * @param {String} name The layer name to resolve. * @returns {LayerType} The resolved layer, or `undefined` if the layer name could not be resolved. */ findLayer(name) { // Look for an exact match on the name. if (this.layers === undefined) { return undefined; } let match = this.layers.find((layer) => layer.Identifier === name || layer.Title === name); if (!match) { const colonIndex = name.indexOf(":"); if (colonIndex >= 0) { // This looks like a namespaced name. Such names will (usually?) show up in GetCapabilities // as just their name without the namespace qualifier. const nameWithoutNamespace = name.substring(colonIndex + 1); match = this.layers.find((layer) => layer.Identifier === nameWithoutNamespace || layer.Title === nameWithoutNamespace); } } return match; } findTileMatrix(set) { if (this.tileMatrixSets === undefined) { return undefined; } return this.tileMatrixSets.find((tileMatrixSet) => tileMatrixSet.Identifier === set); } parseLayers(layers) { if (!layers) { return []; } return Array.isArray(layers) ? layers : [layers]; } parseTileMatrixSets(tileMatrixSets) { if (!tileMatrixSets) { return []; } return Array.isArray(tileMatrixSets) ? tileMatrixSets : [tileMatrixSets]; } } //# sourceMappingURL=WebMapTileServiceCapabilities.js.map